Mercurial > hg > octave-nkf
annotate scripts/strings/regexptranslate.m @ 8627:fdd1cefd3714
Add missing files to SOURCES in plot/Makefile.in
author | Kai Habel |
---|---|
date | Thu, 29 Jan 2009 12:55:16 -0500 |
parents | 502e58a0d44f |
children | eb63fbe60fab |
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 | |
8442
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7983
diff
changeset
|
29 ## with wildcards that are appropriate for a regular expression. For example: |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7983
diff
changeset
|
30 ## @example |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7983
diff
changeset
|
31 ## @group |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7983
diff
changeset
|
32 ## regexptranslate ("wildcard", "*.m") |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7983
diff
changeset
|
33 ## @result{} ".*\.m" |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7983
diff
changeset
|
34 ## @end group |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7983
diff
changeset
|
35 ## @end example |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7983
diff
changeset
|
36 ## |
7983 | 37 ## @item "escape" |
38 ## The characters @code{$.?[]}, that have special meaning for regular | |
8442
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7983
diff
changeset
|
39 ## expressions are escaped so that they are treated literally. For example: |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7983
diff
changeset
|
40 ## @example |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7983
diff
changeset
|
41 ## @group |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7983
diff
changeset
|
42 ## regexptranslate ("escape", "12.5") |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7983
diff
changeset
|
43 ## @result{} "12\.5" |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7983
diff
changeset
|
44 ## @end group |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7983
diff
changeset
|
45 ## @end example |
7983 | 46 ## @end table |
8442
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7983
diff
changeset
|
47 ## @seealso{regexp, regexpi, regexprep} |
7629
cc31c5002c96
Add the regexptranslate function
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
48 ## @end deftypefn |
cc31c5002c96
Add the regexptranslate function
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
49 |
cc31c5002c96
Add the regexptranslate function
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
50 function y = regexptranslate (op, x) |
cc31c5002c96
Add the regexptranslate function
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
51 |
8442
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7983
diff
changeset
|
52 if nargin != 2 |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7983
diff
changeset
|
53 print_usage (); |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7983
diff
changeset
|
54 endif |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7983
diff
changeset
|
55 |
7629
cc31c5002c96
Add the regexptranslate function
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
56 if (ischar (op)) |
cc31c5002c96
Add the regexptranslate function
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
57 op = tolower (op); |
cc31c5002c96
Add the regexptranslate function
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
58 if (strcmp ("wildcard", op)) |
cc31c5002c96
Add the regexptranslate function
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
59 y = regexprep (regexprep (regexprep (x, "\\.", "\\."), "\\*", |
cc31c5002c96
Add the regexptranslate function
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
60 ".*"), "\\?", "."); |
cc31c5002c96
Add the regexptranslate function
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
61 elseif (strcmp ("escape", op)) |
cc31c5002c96
Add the regexptranslate function
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
62 ch = {'\$', '\.', '\?', '\[', '\]'}; |
cc31c5002c96
Add the regexptranslate function
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
63 y = x; |
cc31c5002c96
Add the regexptranslate function
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
64 for i = 1 : length (ch) |
cc31c5002c96
Add the regexptranslate function
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
65 y = regexprep (y, ch{i}, ch{i}); |
cc31c5002c96
Add the regexptranslate function
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
66 endfor |
cc31c5002c96
Add the regexptranslate function
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
67 else |
cc31c5002c96
Add the regexptranslate function
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
68 error ("regexptranslate: unexpected operation"); |
cc31c5002c96
Add the regexptranslate function
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
69 endif |
cc31c5002c96
Add the regexptranslate function
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
70 else |
cc31c5002c96
Add the regexptranslate function
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
71 error ("regexptranslate: expecting operation to be a string"); |
cc31c5002c96
Add the regexptranslate function
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
72 endif |
cc31c5002c96
Add the regexptranslate function
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
73 endfunction |
cc31c5002c96
Add the regexptranslate function
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
74 |
8442
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7983
diff
changeset
|
75 %!error <Invalid call to regexptranslate> regexptranslate (); |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7983
diff
changeset
|
76 %!error <Invalid call to regexptranslate> regexptranslate ("wildcard"); |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7983
diff
changeset
|
77 %!error <Invalid call to regexptranslate> regexptranslate ("a", "b", "c"); |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7983
diff
changeset
|
78 %!error <unexpected operation> regexptranslate ("foo", "abc"); |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7983
diff
changeset
|
79 %!error <expecting operation to be a string> regexptranslate (10, "abc"); |
7629
cc31c5002c96
Add the regexptranslate function
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
80 %!assert (regexptranslate ("wildcard", "/a*b?c."), "/a.*b.c\\.") |
cc31c5002c96
Add the regexptranslate function
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
81 %!assert (regexptranslate ("escape", '$.?[]'), '\$\.\?\[\]') |