Mercurial > hg > octave-lyh
annotate scripts/strings/regexptranslate.m @ 7983:91d020444da7
Document regexptranslate
author | David Bateman <dbateman@free.fr> |
---|---|
date | Sun, 27 Jul 2008 02:42:57 +0200 |
parents | cc31c5002c96 |
children | 502e58a0d44f |
rev | line source |
---|---|
7629
cc31c5002c96
Add the regexptranslate function
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
1 ## Copyright (C) 2008 David Bateman |
cc31c5002c96
Add the regexptranslate function
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
2 ## |
cc31c5002c96
Add the regexptranslate function
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
3 ## This file is part of Octave. |
cc31c5002c96
Add the regexptranslate function
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
4 ## |
cc31c5002c96
Add the regexptranslate function
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
5 ## Octave is free software; you can redistribute it and/or modify it |
cc31c5002c96
Add the regexptranslate function
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
6 ## under the terms of the GNU General Public License as published by |
cc31c5002c96
Add the regexptranslate function
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
7 ## the Free Software Foundation; either version 3 of the License, or (at |
cc31c5002c96
Add the regexptranslate function
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
8 ## your option) any later version. |
cc31c5002c96
Add the regexptranslate function
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
9 ## |
cc31c5002c96
Add the regexptranslate function
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
10 ## Octave is distributed in the hope that it will be useful, but |
cc31c5002c96
Add the regexptranslate function
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of |
cc31c5002c96
Add the regexptranslate function
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
cc31c5002c96
Add the regexptranslate function
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
13 ## General Public License for more details. |
cc31c5002c96
Add the regexptranslate function
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
14 ## |
cc31c5002c96
Add the regexptranslate function
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
15 ## You should have received a copy of the GNU General Public License |
cc31c5002c96
Add the regexptranslate function
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
16 ## along with Octave; see the file COPYING. If not, see |
cc31c5002c96
Add the regexptranslate function
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
17 ## <http://www.gnu.org/licenses/>. |
cc31c5002c96
Add the regexptranslate function
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
18 |
cc31c5002c96
Add the regexptranslate function
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
19 ## -*- texinfo -*- |
7983 | 20 ## @deftypefn {Function File} {} regexptranslate (@var{op}, @var{s}) |
21 ## Translate a string for use in a regular expression. This might | |
22 ## include either wildcard replacement or special character escaping. | |
23 ## The behavior can be controlled by the @var{op} that can have the | |
24 ## values | |
25 ## | |
26 ## @table @asis | |
27 ## @item "wildcard" | |
28 ## The wildcard characters @code{.}, @code{*} and @code{?} are replaced | |
29 ## with wildcards that are appropriate for a regular expression. | |
30 ## | |
31 ## @item "escape" | |
32 ## The characters @code{$.?[]}, that have special meaning for regular | |
33 ## expressions are escaped so that they are treated literally. | |
34 ## @end table | |
7629
cc31c5002c96
Add the regexptranslate function
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
35 ## @end deftypefn |
cc31c5002c96
Add the regexptranslate function
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
36 |
cc31c5002c96
Add the regexptranslate function
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
37 function y = regexptranslate (op, x) |
cc31c5002c96
Add the regexptranslate function
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
38 |
cc31c5002c96
Add the regexptranslate function
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
39 if (ischar (op)) |
cc31c5002c96
Add the regexptranslate function
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
40 op = tolower (op); |
cc31c5002c96
Add the regexptranslate function
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
41 if (strcmp ("wildcard", op)) |
cc31c5002c96
Add the regexptranslate function
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
42 y = regexprep (regexprep (regexprep (x, "\\.", "\\."), "\\*", |
cc31c5002c96
Add the regexptranslate function
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
43 ".*"), "\\?", "."); |
cc31c5002c96
Add the regexptranslate function
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
44 elseif (strcmp ("escape", op)) |
cc31c5002c96
Add the regexptranslate function
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
45 ch = {'\$', '\.', '\?', '\[', '\]'}; |
cc31c5002c96
Add the regexptranslate function
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
46 y = x; |
cc31c5002c96
Add the regexptranslate function
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
47 for i = 1 : length (ch) |
cc31c5002c96
Add the regexptranslate function
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
48 y = regexprep (y, ch{i}, ch{i}); |
cc31c5002c96
Add the regexptranslate function
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
49 endfor |
cc31c5002c96
Add the regexptranslate function
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
50 else |
cc31c5002c96
Add the regexptranslate function
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
51 error ("regexptranslate: unexpected operation"); |
cc31c5002c96
Add the regexptranslate function
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
52 endif |
cc31c5002c96
Add the regexptranslate function
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
53 else |
cc31c5002c96
Add the regexptranslate function
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
54 error ("regexptranslate: expecting operation to be a string"); |
cc31c5002c96
Add the regexptranslate function
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
55 endif |
cc31c5002c96
Add the regexptranslate function
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
56 endfunction |
cc31c5002c96
Add the regexptranslate function
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
57 |
cc31c5002c96
Add the regexptranslate function
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
58 %!assert (regexptranslate ("wildcard", "/a*b?c."), "/a.*b.c\\.") |
cc31c5002c96
Add the regexptranslate function
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
59 %!assert (regexptranslate ("escape", '$.?[]'), '\$\.\?\[\]') |