Mercurial > octave-nkf
annotate src/DLD-FUNCTIONS/regexp.cc @ 8442:502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
author | Thorsten Meyer <thorsten.meyier@gmx.de> |
---|---|
date | Mon, 05 Jan 2009 08:11:03 +0100 |
parents | 25bc2d31e1bf |
children | 433e788a015b |
rev | line source |
---|---|
5582 | 1 /* |
2 | |
7017 | 3 Copyright (C) 2005, 2006, 2007 David Bateman |
7016 | 4 Copyright (C) 2002, 2003, 2004, 2005 Paul Kienzle |
5 | |
6 This file is part of Octave. | |
5582 | 7 |
8 Octave is free software; you can redistribute it and/or modify it | |
9 under the terms of the GNU General Public License as published by the | |
7016 | 10 Free Software Foundation; either version 3 of the License, or (at your |
11 option) any later version. | |
5582 | 12 |
13 Octave is distributed in the hope that it will be useful, but WITHOUT | |
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
16 for more details. | |
17 | |
18 You should have received a copy of the GNU General Public License | |
7016 | 19 along with Octave; see the file COPYING. If not, see |
20 <http://www.gnu.org/licenses/>. | |
5582 | 21 |
22 */ | |
23 | |
24 #ifdef HAVE_CONFIG_H | |
25 #include <config.h> | |
26 #endif | |
27 | |
5773 | 28 #include <algorithm> |
5765 | 29 #include <sstream> |
30 | |
5582 | 31 #include "defun-dld.h" |
32 #include "error.h" | |
33 #include "gripes.h" | |
34 #include "oct-obj.h" | |
35 #include "utils.h" | |
36 | |
37 #include "Cell.h" | |
38 #include "oct-map.h" | |
39 #include "str-vec.h" | |
5785 | 40 #include "quit.h" |
41 #include "parse.h" | |
8377
25bc2d31e1bf
improve OCTAVE_LOCAL_BUFFER
Jaroslav Hajek <highegg@gmail.com>
parents:
8140
diff
changeset
|
42 #include "oct-locbuf.h" |
5582 | 43 |
7173 | 44 #if defined (HAVE_PCRE) |
5582 | 45 #include <pcre.h> |
7117 | 46 #elif defined (HAVE_REGEX) |
47 #if defined (__MINGW32__) | |
5582 | 48 #define __restrict |
49 #endif | |
7237 | 50 #if defined (HAVE_SYS_TYPES_H) |
51 #include <sys/types.h> | |
52 #endif | |
5582 | 53 #include <regex.h> |
54 #endif | |
55 | |
8140
cdd05e46f6c9
Increase pcre's match_limit for difficult regexps
Thomas Weber <thomas.weber.mail@gmail.com>
parents:
8093
diff
changeset
|
56 // Define the maximum number of retries for a pattern that |
cdd05e46f6c9
Increase pcre's match_limit for difficult regexps
Thomas Weber <thomas.weber.mail@gmail.com>
parents:
8093
diff
changeset
|
57 // possibly results in an infinite recursion. |
cdd05e46f6c9
Increase pcre's match_limit for difficult regexps
Thomas Weber <thomas.weber.mail@gmail.com>
parents:
8093
diff
changeset
|
58 #define PCRE_MATCHLIMIT_MAX 10 |
cdd05e46f6c9
Increase pcre's match_limit for difficult regexps
Thomas Weber <thomas.weber.mail@gmail.com>
parents:
8093
diff
changeset
|
59 |
5785 | 60 // The regexp is constructed as a linked list to avoid resizing the |
61 // return values in arrays at each new match. | |
62 | |
63 // FIXME don't bother collecting and composing return values the user | |
64 // doesn't want. | |
65 | |
66 class regexp_elem | |
5582 | 67 { |
5785 | 68 public: |
5787 | 69 regexp_elem (const string_vector& _named_token, const Cell& _t, |
70 const std::string& _m, const Matrix& _te, double _s, | |
71 double _e) : | |
5785 | 72 named_token (_named_token), t (_t), m (_m), te (_te), s (_s), e (_e) { } |
73 | |
74 regexp_elem (const regexp_elem &a) : named_token (a.named_token), t (a.t), | |
75 m (a.m), te (a.te), s (a.s), e (a.e) | |
76 { } | |
77 | |
78 string_vector named_token; | |
79 Cell t; | |
80 std::string m; | |
81 Matrix te; | |
82 double s; | |
83 double e; | |
84 }; | |
85 | |
86 typedef std::list<regexp_elem>::const_iterator const_iterator; | |
87 | |
8093
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
88 #define MAXLOOKBEHIND 10 |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
89 static bool lookbehind_warned = false; |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
90 |
5785 | 91 static int |
92 octregexp_list (const octave_value_list &args, const std::string &nm, | |
93 bool case_insensitive, std::list<regexp_elem> &lst, | |
7893
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
94 string_vector &named, int &nopts, bool &once) |
5785 | 95 { |
96 int sz = 0; | |
5582 | 97 #if defined (HAVE_REGEX) || defined (HAVE_PCRE) |
98 int nargin = args.length(); | |
5779 | 99 bool lineanchors = false; |
100 bool dotexceptnewline = false; | |
101 bool freespacing = false; | |
5582 | 102 |
5785 | 103 nopts = nargin - 2; |
7893
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
104 once = false; |
5785 | 105 |
5582 | 106 std::string buffer = args(0).string_value (); |
8093
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
107 size_t max_length = (buffer.length () > MAXLOOKBEHIND ? |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
108 MAXLOOKBEHIND: buffer.length ()); |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
109 |
5582 | 110 if (error_state) |
111 { | |
112 gripe_wrong_type_arg (nm.c_str(), args(0)); | |
5785 | 113 return 0; |
5582 | 114 } |
115 | |
116 std::string pattern = args(1).string_value (); | |
117 if (error_state) | |
118 { | |
119 gripe_wrong_type_arg (nm.c_str(), args(1)); | |
5785 | 120 return 0; |
5582 | 121 } |
122 | |
123 for (int i = 2; i < nargin; i++) | |
124 { | |
125 std::string str = args(i).string_value(); | |
126 if (error_state) | |
127 { | |
128 error ("%s: optional arguments must be strings", nm.c_str()); | |
129 break; | |
130 } | |
131 std::transform (str.begin (), str.end (), str.begin (), tolower); | |
132 if (str.find("once", 0) == 0) | |
133 { | |
134 once = true; | |
135 nopts--; | |
136 } | |
5779 | 137 else if (str.find("matchcase", 0) == 0) |
138 { | |
139 case_insensitive = false; | |
140 nopts--; | |
141 } | |
142 else if (str.find("ignorecase", 0) == 0) | |
143 { | |
144 case_insensitive = true; | |
145 nopts--; | |
146 } | |
5785 | 147 else if (str.find("dotall", 0) == 0) |
5779 | 148 { |
5785 | 149 dotexceptnewline = false; |
150 nopts--; | |
151 } | |
152 else if (str.find("stringanchors", 0) == 0) | |
153 { | |
154 lineanchors = false; | |
5779 | 155 nopts--; |
156 } | |
157 else if (str.find("literalspacing", 0) == 0) | |
158 { | |
159 freespacing = false; | |
160 nopts--; | |
161 } | |
5785 | 162 #if HAVE_PCRE |
163 // Only accept these options with pcre | |
164 else if (str.find("dotexceptnewline", 0) == 0) | |
165 { | |
166 dotexceptnewline = true; | |
167 nopts--; | |
168 } | |
169 else if (str.find("lineanchors", 0) == 0) | |
170 { | |
171 lineanchors = true; | |
172 nopts--; | |
173 } | |
174 else if (str.find("freespacing", 0) == 0) | |
175 { | |
176 freespacing = true; | |
177 nopts--; | |
178 } | |
5582 | 179 else if (str.find("start", 0) && str.find("end", 0) && |
180 str.find("tokenextents", 0) && str.find("match", 0) && | |
181 str.find("tokens", 0) && str.find("names", 0)) | |
182 error ("%s: unrecognized option", nm.c_str()); | |
183 #else | |
5779 | 184 else if (str.find("names", 0) == 0 || |
185 str.find("dotexceptnewline", 0) == 0 || | |
186 str.find("lineanchors", 0) == 0 || | |
187 str.find("freespacing", 0) == 0) | |
5785 | 188 error ("%s: %s not implemented in this version", str.c_str(), nm.c_str()); |
5582 | 189 else if (str.find("start", 0) && str.find("end", 0) && |
190 str.find("tokenextents", 0) && str.find("match", 0) && | |
191 str.find("tokens", 0)) | |
192 error ("%s: unrecognized option", nm.c_str()); | |
193 #endif | |
194 } | |
195 | |
196 if (!error_state) | |
197 { | |
5785 | 198 Cell t; |
199 std::string m; | |
200 double s, e; | |
5582 | 201 |
202 // named tokens "(?<name>...)" are only treated with PCRE not regex. | |
203 #if HAVE_PCRE | |
204 | |
5619 | 205 size_t pos = 0; |
206 size_t new_pos; | |
207 int nnames = 0; | |
208 int inames = 0; | |
5765 | 209 std::ostringstream buf; |
5619 | 210 Array<int> named_idx; |
5582 | 211 |
8093
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
212 while ((new_pos = pattern.find ("(?",pos)) != std::string::npos) |
5619 | 213 { |
8093
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
214 if (pattern.at (new_pos + 2) == '<' && |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
215 !(pattern.at (new_pos + 3) == '=' || |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
216 pattern.at (new_pos + 3) == '!')) |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
217 { |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
218 // The syntax of named tokens in pcre is "(?P<name>...)" while |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
219 // we need a syntax "(?<name>...)", so fix that here. Also an |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
220 // expression like |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
221 // "(?<first>\w+)\s+(?<last>\w+)|(?<last>\w+),\s+(?<first>\w+)" |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
222 // should be perfectly legal, while pcre does not allow the same |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
223 // named token name on both sides of the alternative. Also fix |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
224 // that here by replacing name tokens by dummy names, and dealing |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
225 // with the dummy names later. |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
226 |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
227 size_t tmp_pos = pattern.find_first_of ('>',new_pos); |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
228 |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
229 if (tmp_pos == std::string::npos) |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
230 { |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
231 error ("syntax error in pattern"); |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
232 break; |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
233 } |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
234 |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
235 std::string tmp_name = |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
236 pattern.substr(new_pos+3,tmp_pos-new_pos-3); |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
237 bool found = false; |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
238 |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
239 for (int i = 0; i < nnames; i++) |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
240 if (named(i) == tmp_name) |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
241 { |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
242 named_idx.resize(inames+1); |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
243 named_idx(inames) = i; |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
244 found = true; |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
245 break; |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
246 } |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
247 if (! found) |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
248 { |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
249 named_idx.resize(inames+1); |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
250 named_idx(inames) = nnames; |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
251 named.append(tmp_name); |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
252 nnames++; |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
253 } |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
254 |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
255 if (new_pos - pos > 0) |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
256 buf << pattern.substr(pos,new_pos-pos); |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
257 if (inames < 10) |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
258 buf << "(?P<n00" << inames++; |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
259 else if (inames < 100) |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
260 buf << "(?P<n0" << inames++; |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
261 else |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
262 buf << "(?P<n" << inames++; |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
263 pos = tmp_pos; |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
264 } |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
265 else if (pattern.at (new_pos + 2) == '<') |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
266 { |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
267 // Find lookbehind operators of arbitrary length (ie like |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
268 // "(?<=[a-z]*)") and replace with a maximum length operator |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
269 // as PCRE can not yet handle arbitrary length lookahead |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
270 // operators. Use the string length as the maximum length to |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
271 // avoid issues. |
5582 | 272 |
8093
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
273 int brackets = 1; |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
274 size_t tmp_pos1 = new_pos + 2; |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
275 size_t tmp_pos2 = tmp_pos1; |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
276 while (tmp_pos1 <= pattern.length () && brackets > 0) |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
277 { |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
278 char ch = pattern.at (tmp_pos1); |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
279 if (ch == '(') |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
280 brackets++; |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
281 else if (ch == ')') |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
282 { |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
283 if (brackets > 1) |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
284 tmp_pos2 = tmp_pos1; |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
285 |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
286 brackets--; |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
287 } |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
288 tmp_pos1++; |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
289 } |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
290 |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
291 if (brackets != 0) |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
292 { |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
293 buf << pattern.substr (pos, new_pos - pos) << "(?"; |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
294 pos = new_pos + 2; |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
295 } |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
296 else |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
297 { |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
298 size_t tmp_pos3 = pattern.find_first_of ("*+", tmp_pos2); |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
299 if (tmp_pos3 != std::string::npos && tmp_pos3 < tmp_pos1) |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
300 { |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
301 if (!lookbehind_warned) |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
302 { |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
303 lookbehind_warned = true; |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
304 warning ("%s: arbitrary length lookbehind patterns are only support up to length %d", nm.c_str(), MAXLOOKBEHIND); |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
305 } |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
306 |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
307 buf << pattern.substr (pos, new_pos - pos) << "("; |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
308 |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
309 size_t i; |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
310 if (pattern.at (tmp_pos3) == '*') |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
311 i = 0; |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
312 else |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
313 i = 1; |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
314 |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
315 for (; i < max_length + 1; i++) |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
316 { |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
317 buf <<pattern.substr(new_pos, tmp_pos3 - new_pos) |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
318 << "{" << i << "}"; |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
319 buf << pattern.substr(tmp_pos3 + 1, |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
320 tmp_pos1 - tmp_pos3 - 1); |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
321 if (i != max_length) |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
322 buf << "|"; |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
323 } |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
324 buf << ")"; |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
325 } |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
326 else |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
327 buf << pattern.substr (pos, tmp_pos1 - pos); |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
328 pos = tmp_pos1; |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
329 } |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
330 } |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
331 else |
5619 | 332 { |
8093
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
333 buf << pattern.substr (pos, new_pos - pos) << "(?"; |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
334 pos = new_pos + 2; |
5619 | 335 } |
336 | |
337 } | |
338 | |
5765 | 339 buf << pattern.substr(pos); |
5619 | 340 |
341 if (error_state) | |
5785 | 342 return 0; |
5582 | 343 |
344 // Compile expression | |
345 pcre *re; | |
346 const char *err; | |
347 int erroffset; | |
5765 | 348 std::string buf_str = buf.str (); |
349 re = pcre_compile (buf_str.c_str (), | |
5779 | 350 (case_insensitive ? PCRE_CASELESS : 0) | |
351 (dotexceptnewline ? 0 : PCRE_DOTALL) | | |
352 (lineanchors ? PCRE_MULTILINE : 0) | | |
353 (freespacing ? PCRE_EXTENDED : 0), | |
7520 | 354 &err, &erroffset, 0); |
5582 | 355 |
7520 | 356 if (re == 0) { |
5582 | 357 error("%s: %s at position %d of expression", nm.c_str(), |
358 err, erroffset); | |
5785 | 359 return 0; |
5582 | 360 } |
361 | |
362 int subpatterns; | |
363 int namecount; | |
364 int nameentrysize; | |
365 char *nametable; | |
366 int idx = 0; | |
367 | |
7520 | 368 pcre_fullinfo(re, 0, PCRE_INFO_CAPTURECOUNT, &subpatterns); |
369 pcre_fullinfo(re, 0, PCRE_INFO_NAMECOUNT, &namecount); | |
370 pcre_fullinfo(re, 0, PCRE_INFO_NAMEENTRYSIZE, &nameentrysize); | |
371 pcre_fullinfo(re, 0, PCRE_INFO_NAMETABLE, &nametable); | |
5582 | 372 |
373 OCTAVE_LOCAL_BUFFER(int, ovector, (subpatterns+1)*3); | |
374 OCTAVE_LOCAL_BUFFER(int, nidx, namecount); | |
375 | |
376 for (int i = 0; i < namecount; i++) | |
377 { | |
378 // Index of subpattern in first two bytes MSB first of name. | |
5619 | 379 // Extract index. |
5779 | 380 nidx[i] = (static_cast<int>(nametable[i*nameentrysize])) << 8 | |
381 static_cast<int>(nametable[i*nameentrysize+1]); | |
5582 | 382 } |
383 | |
384 while(true) | |
385 { | |
5785 | 386 OCTAVE_QUIT; |
387 | |
7520 | 388 int matches = pcre_exec(re, 0, buffer.c_str(), |
5582 | 389 buffer.length(), idx, |
390 (idx ? PCRE_NOTBOL : 0), | |
391 ovector, (subpatterns+1)*3); | |
392 | |
8140
cdd05e46f6c9
Increase pcre's match_limit for difficult regexps
Thomas Weber <thomas.weber.mail@gmail.com>
parents:
8093
diff
changeset
|
393 if (matches == PCRE_ERROR_MATCHLIMIT) |
cdd05e46f6c9
Increase pcre's match_limit for difficult regexps
Thomas Weber <thomas.weber.mail@gmail.com>
parents:
8093
diff
changeset
|
394 { |
cdd05e46f6c9
Increase pcre's match_limit for difficult regexps
Thomas Weber <thomas.weber.mail@gmail.com>
parents:
8093
diff
changeset
|
395 // try harder; start with default value for MATCH_LIMIT and increase it |
cdd05e46f6c9
Increase pcre's match_limit for difficult regexps
Thomas Weber <thomas.weber.mail@gmail.com>
parents:
8093
diff
changeset
|
396 warning("Your pattern caused PCRE to hit its MATCH_LIMIT.\nTrying harder now, but this will be slow."); |
cdd05e46f6c9
Increase pcre's match_limit for difficult regexps
Thomas Weber <thomas.weber.mail@gmail.com>
parents:
8093
diff
changeset
|
397 pcre_extra pe; |
cdd05e46f6c9
Increase pcre's match_limit for difficult regexps
Thomas Weber <thomas.weber.mail@gmail.com>
parents:
8093
diff
changeset
|
398 pcre_config(PCRE_CONFIG_MATCH_LIMIT, static_cast <void *> (&pe.match_limit)); |
cdd05e46f6c9
Increase pcre's match_limit for difficult regexps
Thomas Weber <thomas.weber.mail@gmail.com>
parents:
8093
diff
changeset
|
399 pe.flags = PCRE_EXTRA_MATCH_LIMIT; |
cdd05e46f6c9
Increase pcre's match_limit for difficult regexps
Thomas Weber <thomas.weber.mail@gmail.com>
parents:
8093
diff
changeset
|
400 |
cdd05e46f6c9
Increase pcre's match_limit for difficult regexps
Thomas Weber <thomas.weber.mail@gmail.com>
parents:
8093
diff
changeset
|
401 int i = 0; |
cdd05e46f6c9
Increase pcre's match_limit for difficult regexps
Thomas Weber <thomas.weber.mail@gmail.com>
parents:
8093
diff
changeset
|
402 while (matches == PCRE_ERROR_MATCHLIMIT && |
cdd05e46f6c9
Increase pcre's match_limit for difficult regexps
Thomas Weber <thomas.weber.mail@gmail.com>
parents:
8093
diff
changeset
|
403 i++ < PCRE_MATCHLIMIT_MAX) |
cdd05e46f6c9
Increase pcre's match_limit for difficult regexps
Thomas Weber <thomas.weber.mail@gmail.com>
parents:
8093
diff
changeset
|
404 { |
cdd05e46f6c9
Increase pcre's match_limit for difficult regexps
Thomas Weber <thomas.weber.mail@gmail.com>
parents:
8093
diff
changeset
|
405 OCTAVE_QUIT; |
cdd05e46f6c9
Increase pcre's match_limit for difficult regexps
Thomas Weber <thomas.weber.mail@gmail.com>
parents:
8093
diff
changeset
|
406 |
cdd05e46f6c9
Increase pcre's match_limit for difficult regexps
Thomas Weber <thomas.weber.mail@gmail.com>
parents:
8093
diff
changeset
|
407 pe.match_limit *= 10; |
cdd05e46f6c9
Increase pcre's match_limit for difficult regexps
Thomas Weber <thomas.weber.mail@gmail.com>
parents:
8093
diff
changeset
|
408 matches = pcre_exec(re, &pe, buffer.c_str(), |
cdd05e46f6c9
Increase pcre's match_limit for difficult regexps
Thomas Weber <thomas.weber.mail@gmail.com>
parents:
8093
diff
changeset
|
409 buffer.length(), idx, |
cdd05e46f6c9
Increase pcre's match_limit for difficult regexps
Thomas Weber <thomas.weber.mail@gmail.com>
parents:
8093
diff
changeset
|
410 (idx ? PCRE_NOTBOL : 0), |
cdd05e46f6c9
Increase pcre's match_limit for difficult regexps
Thomas Weber <thomas.weber.mail@gmail.com>
parents:
8093
diff
changeset
|
411 ovector, (subpatterns+1)*3); |
cdd05e46f6c9
Increase pcre's match_limit for difficult regexps
Thomas Weber <thomas.weber.mail@gmail.com>
parents:
8093
diff
changeset
|
412 } |
cdd05e46f6c9
Increase pcre's match_limit for difficult regexps
Thomas Weber <thomas.weber.mail@gmail.com>
parents:
8093
diff
changeset
|
413 } |
cdd05e46f6c9
Increase pcre's match_limit for difficult regexps
Thomas Weber <thomas.weber.mail@gmail.com>
parents:
8093
diff
changeset
|
414 |
5582 | 415 if (matches < 0 && matches != PCRE_ERROR_NOMATCH) |
416 { | |
8140
cdd05e46f6c9
Increase pcre's match_limit for difficult regexps
Thomas Weber <thomas.weber.mail@gmail.com>
parents:
8093
diff
changeset
|
417 error ("%s: internal error calling pcre_exec\nError code from pcre_exec is %i", nm.c_str(), matches); |
5582 | 418 pcre_free(re); |
5785 | 419 return 0; |
5582 | 420 } |
421 else if (matches == PCRE_ERROR_NOMATCH) | |
422 break; | |
5779 | 423 else if (ovector[1] <= ovector[0]) |
424 break; | |
5582 | 425 else |
426 { | |
5619 | 427 int pos_match = 0; |
5785 | 428 Matrix te(matches-1,2); |
5582 | 429 for (int i = 1; i < matches; i++) |
430 { | |
5619 | 431 if (ovector[2*i] >= 0 && ovector[2*i+1] > 0) |
432 { | |
5785 | 433 te(pos_match,0) = double (ovector[2*i]+1); |
434 te(pos_match++,1) = double (ovector[2*i+1]); | |
5619 | 435 } |
5582 | 436 } |
5785 | 437 te.resize(pos_match,2); |
438 s = double (ovector[0]+1); | |
439 e = double (ovector[1]); | |
5582 | 440 |
441 const char **listptr; | |
442 int status = pcre_get_substring_list(buffer.c_str(), ovector, | |
443 matches, &listptr); | |
444 | |
445 if (status == PCRE_ERROR_NOMEMORY) { | |
446 error("%s: cannot allocate memory in pcre_get_substring_list", | |
447 nm.c_str()); | |
448 pcre_free(re); | |
5785 | 449 return 0; |
5582 | 450 } |
451 | |
5619 | 452 Cell cell_t (dim_vector(1,pos_match)); |
453 pos_match = 0; | |
5582 | 454 for (int i = 1; i < matches; i++) |
5619 | 455 if (ovector[2*i] >= 0 && ovector[2*i+1] > 0) |
456 cell_t(pos_match++) = std::string(*(listptr+i)); | |
5582 | 457 |
5785 | 458 m = std::string(*listptr); |
459 t = cell_t; | |
460 | |
461 string_vector named_tokens(nnames); | |
5619 | 462 if (namecount > 0) |
463 for (int i = 1; i < matches; i++) | |
464 { | |
465 if (ovector[2*i] >= 0 && ovector[2*i+1] > 0) | |
466 { | |
5785 | 467 named_tokens(named_idx(i-1)) = |
468 std::string(*(listptr+nidx[i-1])); | |
5619 | 469 } |
470 } | |
5582 | 471 |
472 pcre_free_substring_list(listptr); | |
473 | |
5785 | 474 regexp_elem new_elem (named_tokens, t, m, te, s, e); |
475 lst.push_back (new_elem); | |
476 idx = ovector[1]; | |
477 sz++; | |
478 | |
5582 | 479 if (once) |
480 break; | |
481 | |
482 } | |
483 } | |
484 | |
485 pcre_free(re); | |
486 #else | |
487 regex_t compiled; | |
488 int err=regcomp(&compiled, pattern.c_str(), REG_EXTENDED | | |
489 (case_insensitive ? REG_ICASE : 0)); | |
490 if (err) | |
491 { | |
7520 | 492 int len = regerror(err, &compiled, 0, 0); |
5760 | 493 OCTAVE_LOCAL_BUFFER (char, errmsg, len); |
494 regerror(err, &compiled, errmsg, len); | |
495 error("%s: %s in pattern (%s)", nm.c_str(), errmsg, | |
496 pattern.c_str()); | |
5582 | 497 regfree(&compiled); |
5785 | 498 return 0; |
5582 | 499 } |
500 | |
501 int subexpr = 1; | |
502 int idx = 0; | |
503 for (unsigned int i=0; i < pattern.length(); i++) | |
504 subexpr += ( pattern[i] == '(' ? 1 : 0 ); | |
505 OCTAVE_LOCAL_BUFFER (regmatch_t, match, subexpr ); | |
506 | |
507 while(true) | |
508 { | |
5785 | 509 OCTAVE_QUIT; |
510 | |
5582 | 511 if (regexec(&compiled, buffer.c_str() + idx, subexpr, |
512 match, (idx ? REG_NOTBOL : 0)) == 0) | |
513 { | |
514 // Count actual matches | |
515 int matches = 0; | |
516 while (matches < subexpr && match[matches].rm_so >= 0) | |
517 matches++; | |
518 | |
5785 | 519 s = double (match[0].rm_so+1+idx); |
520 e = double (match[0].rm_eo+idx); | |
521 Matrix te(matches-1,2); | |
5582 | 522 for (int i = 1; i < matches; i++) |
523 { | |
5785 | 524 te(i-1,0) = double (match[i].rm_so+1+idx); |
525 te(i-1,1) = double (match[i].rm_eo+idx); | |
5582 | 526 } |
527 | |
5785 | 528 m = buffer.substr (match[0].rm_so+idx, |
5582 | 529 match[0].rm_eo-match[0].rm_so); |
530 | |
531 Cell cell_t (dim_vector(1,matches-1)); | |
532 for (int i = 1; i < matches; i++) | |
533 cell_t(i-1) = buffer.substr (match[i].rm_so+idx, | |
534 match[i].rm_eo-match[i].rm_so); | |
5785 | 535 t = cell_t; |
5582 | 536 |
537 idx += match[0].rm_eo; | |
5785 | 538 |
5866 | 539 string_vector sv; |
540 regexp_elem new_elem (sv, t, m, te, s, e); | |
5785 | 541 lst.push_back (new_elem); |
5582 | 542 sz++; |
543 | |
544 if (once) | |
545 break; | |
546 } | |
547 else | |
548 break; | |
549 } | |
550 regfree(&compiled); | |
551 #endif | |
5785 | 552 } |
553 #else | |
554 error ("%s: not available in this version of Octave", nm.c_str()); | |
555 #endif | |
556 return sz; | |
557 } | |
5582 | 558 |
5785 | 559 static octave_value_list |
560 octregexp (const octave_value_list &args, int nargout, const std::string &nm, | |
561 bool case_insensitive) | |
562 { | |
563 octave_value_list retval; | |
564 int nargin = args.length(); | |
565 std::list<regexp_elem> lst; | |
566 string_vector named; | |
567 int nopts; | |
7893
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
568 bool once; |
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
569 int sz = octregexp_list (args, nm, case_insensitive, lst, named, nopts, once); |
5785 | 570 |
571 if (! error_state) | |
572 { | |
573 // Converted the linked list in the correct form for the return values | |
574 | |
575 octave_idx_type i = 0; | |
576 #ifdef HAVE_PCRE | |
577 Octave_map nmap; | |
578 if (sz == 1) | |
579 { | |
580 for (int j = 0; j < named.length(); j++) | |
581 nmap.assign (named(j), lst.begin()->named_token(j)); | |
582 retval(5) = nmap; | |
583 } | |
584 else | |
585 { | |
586 for (int j = 0; j < named.length (); j++) | |
587 { | |
588 i = 0; | |
589 Cell tmp(dim_vector (1, sz)); | |
590 for (const_iterator p = lst.begin(); p != lst.end(); p++) | |
591 tmp(i++) = p->named_token(j); | |
592 nmap.assign (named(j), octave_value (tmp)); | |
593 } | |
594 retval(5) = nmap; | |
595 } | |
596 #else | |
597 retval(5) = Octave_map(); | |
598 #endif | |
599 | |
7893
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
600 if (once) |
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
601 retval(4) = sz ? lst.front ().t : Cell(); |
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
602 else |
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
603 { |
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
604 Cell t (dim_vector(1, sz)); |
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
605 i = 0; |
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
606 for (const_iterator p = lst.begin(); p != lst.end(); p++) |
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
607 t(i++) = p->t; |
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
608 retval(4) = t; |
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
609 } |
5785 | 610 |
7893
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
611 if (once) |
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
612 retval(3) = sz ? lst.front ().m : std::string(); |
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
613 else |
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
614 { |
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
615 Cell m (dim_vector(1, sz)); |
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
616 i = 0; |
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
617 for (const_iterator p = lst.begin(); p != lst.end(); p++) |
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
618 m(i++) = p->m; |
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
619 retval(3) = m; |
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
620 } |
5785 | 621 |
7893
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
622 if (once) |
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
623 retval(2) = sz ? lst.front ().te : Matrix(); |
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
624 else |
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
625 { |
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
626 Cell te (dim_vector(1, sz)); |
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
627 i = 0; |
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
628 for (const_iterator p = lst.begin(); p != lst.end(); p++) |
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
629 te(i++) = p->te; |
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
630 retval(2) = te; |
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
631 } |
5785 | 632 |
7893
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
633 if (once) |
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
634 { |
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
635 if (sz) |
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
636 retval(1) = lst.front ().e; |
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
637 else |
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
638 retval(1) = Matrix(); |
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
639 } |
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
640 else |
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
641 { |
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
642 NDArray e (dim_vector(1, sz)); |
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
643 i = 0; |
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
644 for (const_iterator p = lst.begin(); p != lst.end(); p++) |
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
645 e(i++) = p->e; |
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
646 retval(1) = e; |
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
647 } |
5785 | 648 |
7893
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
649 if (once) |
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
650 { |
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
651 if (sz) |
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
652 retval(0) = lst.front ().s; |
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
653 else |
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
654 retval(0) = Matrix(); |
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
655 } |
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
656 else |
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
657 { |
5785 | 658 NDArray s (dim_vector(1, sz)); |
659 i = 0; | |
660 for (const_iterator p = lst.begin(); p != lst.end(); p++) | |
661 s(i++) = p->s; | |
5582 | 662 retval(0) = s; |
7893
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
663 } |
5582 | 664 |
665 // Alter the order of the output arguments | |
666 if (nopts > 0) | |
667 { | |
668 int n = 0; | |
669 octave_value_list new_retval; | |
670 new_retval.resize(nargout); | |
671 | |
672 OCTAVE_LOCAL_BUFFER (int, arg_used, 6); | |
5785 | 673 for (int j = 0; j < 6; j++) |
674 arg_used[j] = false; | |
5582 | 675 |
5785 | 676 for (int j = 2; j < nargin; j++) |
5582 | 677 { |
678 int k = 0; | |
5785 | 679 std::string str = args(j).string_value(); |
5582 | 680 std::transform (str.begin (), str.end (), str.begin (), tolower); |
5779 | 681 if (str.find("once", 0) == 0 |
682 || str.find("stringanchors", 0) == 0 | |
683 || str.find("lineanchors", 0) == 0 | |
684 || str.find("matchcase", 0) == 0 | |
685 || str.find("ignorecase", 0) == 0 | |
686 || str.find("dotall", 0) == 0 | |
687 || str.find("dotexceptnewline", 0) == 0 | |
688 || str.find("literalspacing", 0) == 0 | |
689 || str.find("freespacing", 0) == 0 | |
690 ) | |
5582 | 691 continue; |
692 else if (str.find("start", 0) == 0) | |
693 k = 0; | |
694 else if (str.find("end", 0) == 0) | |
695 k = 1; | |
696 else if (str.find("tokenextents", 0) == 0) | |
697 k = 2; | |
698 else if (str.find("match", 0) == 0) | |
699 k = 3; | |
700 else if (str.find("tokens", 0) == 0) | |
701 k = 4; | |
702 else if (str.find("names", 0) == 0) | |
703 k = 5; | |
704 | |
705 new_retval(n++) = retval(k); | |
706 arg_used[k] = true; | |
707 | |
708 if (n == nargout) | |
709 break; | |
710 } | |
711 | |
712 // Fill in the rest of the arguments | |
713 if (n < nargout) | |
714 { | |
5785 | 715 for (int j = 0; j < 6; j++) |
5582 | 716 { |
5785 | 717 if (! arg_used[j]) |
718 new_retval(n++) = retval(j); | |
5582 | 719 } |
720 } | |
721 | |
722 retval = new_retval; | |
723 } | |
724 } | |
725 | |
726 return retval; | |
727 } | |
728 | |
6361 | 729 static octave_value_list |
730 octcellregexp (const octave_value_list &args, int nargout, const std::string &nm, | |
731 bool case_insensitive) | |
732 { | |
733 octave_value_list retval; | |
734 | |
735 if (args(0).is_cell()) | |
736 { | |
737 OCTAVE_LOCAL_BUFFER (Cell, newretval, nargout); | |
738 octave_value_list new_args = args; | |
739 Cell cellstr = args(0).cell_value(); | |
740 if (args(1).is_cell()) | |
741 { | |
742 Cell cellpat = args(1).cell_value(); | |
743 | |
744 if (cellpat.numel() == 1) | |
745 { | |
746 for (int j = 0; j < nargout; j++) | |
747 newretval[j].resize(cellstr.dims()); | |
748 | |
749 new_args(1) = cellpat(0); | |
750 | |
751 for (octave_idx_type i = 0; i < cellstr.numel (); i++) | |
752 { | |
753 new_args(0) = cellstr(i); | |
754 octave_value_list tmp = octregexp (new_args, nargout, nm, | |
755 case_insensitive); | |
756 | |
757 if (error_state) | |
758 break; | |
759 | |
760 for (int j = 0; j < nargout; j++) | |
761 newretval[j](i) = tmp(j); | |
762 } | |
763 } | |
764 else if (cellstr.numel() == 1) | |
765 { | |
766 for (int j = 0; j < nargout; j++) | |
767 newretval[j].resize(cellpat.dims()); | |
768 | |
769 new_args(0) = cellstr(0); | |
770 | |
771 for (octave_idx_type i = 0; i < cellpat.numel (); i++) | |
772 { | |
773 new_args(1) = cellpat(i); | |
774 octave_value_list tmp = octregexp (new_args, nargout, nm, | |
775 case_insensitive); | |
776 | |
777 if (error_state) | |
778 break; | |
779 | |
780 for (int j = 0; j < nargout; j++) | |
781 newretval[j](i) = tmp(j); | |
782 } | |
783 } | |
784 else if (cellstr.numel() == cellpat.numel()) | |
785 { | |
786 | |
787 if (cellstr.dims() != cellpat.dims()) | |
788 error ("%s: Inconsistent cell array dimensions", nm.c_str()); | |
789 else | |
790 { | |
791 for (int j = 0; j < nargout; j++) | |
792 newretval[j].resize(cellstr.dims()); | |
793 | |
794 for (octave_idx_type i = 0; i < cellstr.numel (); i++) | |
795 { | |
796 new_args(0) = cellstr(i); | |
797 new_args(1) = cellpat(i); | |
798 | |
799 octave_value_list tmp = octregexp (new_args, nargout, nm, | |
800 case_insensitive); | |
801 | |
802 if (error_state) | |
803 break; | |
804 | |
805 for (int j = 0; j < nargout; j++) | |
806 newretval[j](i) = tmp(j); | |
807 } | |
808 } | |
809 } | |
810 else | |
811 error ("regexp: cell array arguments must be scalar or equal size"); | |
812 } | |
813 else | |
814 { | |
815 for (int j = 0; j < nargout; j++) | |
816 newretval[j].resize(cellstr.dims()); | |
817 | |
818 for (octave_idx_type i = 0; i < cellstr.numel (); i++) | |
819 { | |
820 new_args(0) = cellstr(i); | |
821 octave_value_list tmp = octregexp (new_args, nargout, nm, case_insensitive); | |
822 | |
823 if (error_state) | |
824 break; | |
825 | |
826 for (int j = 0; j < nargout; j++) | |
827 newretval[j](i) = tmp(j); | |
828 } | |
829 } | |
830 | |
831 if (!error_state) | |
832 for (int j = 0; j < nargout; j++) | |
833 retval(j) = octave_value (newretval[j]); | |
834 } | |
835 else if (args(1).is_cell()) | |
836 { | |
837 OCTAVE_LOCAL_BUFFER (Cell, newretval, nargout); | |
838 octave_value_list new_args = args; | |
839 Cell cellpat = args(1).cell_value(); | |
840 | |
841 for (int j = 0; j < nargout; j++) | |
842 newretval[j].resize(cellpat.dims()); | |
843 | |
844 for (octave_idx_type i = 0; i < cellpat.numel (); i++) | |
845 { | |
846 new_args(1) = cellpat(i); | |
847 octave_value_list tmp = octregexp (new_args, nargout, nm, case_insensitive); | |
848 | |
849 if (error_state) | |
850 break; | |
851 | |
852 for (int j = 0; j < nargout; j++) | |
853 newretval[j](i) = tmp(j); | |
854 } | |
855 | |
856 if (!error_state) | |
857 for (int j = 0; j < nargout; j++) | |
858 retval(j) = octave_value (newretval[j]); | |
859 } | |
860 else | |
861 retval = octregexp (args, nargout, nm, case_insensitive); | |
862 | |
863 return retval; | |
864 | |
865 } | |
866 | |
5582 | 867 DEFUN_DLD (regexp, args, nargout, |
868 "-*- texinfo -*-\n\ | |
869 @deftypefn {Loadable Function} {[@var{s}, @var{e}, @var{te}, @var{m}, @var{t}, @var{nm}] =} regexp (@var{str}, @var{pat})\n\ | |
870 @deftypefnx {Loadable Function} {[@dots{}] =} regexp (@var{str}, @var{pat}, @var{opts}, @dots{})\n\ | |
871 \n\ | |
872 Regular expression string matching. Matches @var{pat} in @var{str} and\n\ | |
873 returns the position and matching substrings or empty values if there are\n\ | |
874 none.\n\ | |
875 \n\ | |
876 The matched pattern @var{pat} can include any of the standard regex\n\ | |
877 operators, including:\n\ | |
878 \n\ | |
879 @table @code\n\ | |
880 @item .\n\ | |
881 Match any character\n\ | |
882 @item * + ? @{@}\n\ | |
883 Repetition operators, representing\n\ | |
884 @table @code\n\ | |
885 @item *\n\ | |
886 Match zero or more times\n\ | |
887 @item +\n\ | |
888 Match one or more times\n\ | |
889 @item ?\n\ | |
890 Match zero or one times\n\ | |
891 @item @{@}\n\ | |
892 Match range operator, which is of the form @code{@{@var{n}@}} to match exactly\n\ | |
893 @var{n} times, @code{@{@var{m},@}} to match @var{m} or more times,\n\ | |
894 @code{@{@var{m},@var{n}@}} to match between @var{m} and @var{n} times.\n\ | |
895 @end table\n\ | |
896 @item [@dots{}] [^@dots{}]\n\ | |
897 List operators, where for example @code{[ab]c} matches @code{ac} and @code{bc}\n\ | |
898 @item ()\n\ | |
899 Grouping operator\n\ | |
900 @item |\n\ | |
901 Alternation operator. Match one of a choice of regular expressions. The\n\ | |
7001 | 902 alternatives must be delimited by the grouping operator @code{()} above\n\ |
5582 | 903 @item ^ $\n\ |
904 Anchoring operator. @code{^} matches the start of the string @var{str} and\n\ | |
905 @code{$} the end\n\ | |
906 @end table\n\ | |
907 \n\ | |
908 In addition the following escaped characters have special meaning. It should\n\ | |
909 be noted that it is recommended to quote @var{pat} in single quotes rather\n\ | |
910 than double quotes, to avoid the escape sequences being interpreted by octave\n\ | |
911 before being passed to @code{regexp}.\n\ | |
912 \n\ | |
913 @table @code\n\ | |
914 @item \\b\n\ | |
915 Match a word boundary\n\ | |
916 @item \\B\n\ | |
917 Match within a word\n\ | |
918 @item \\w\n\ | |
919 Matches any word character\n\ | |
920 @item \\W\n\ | |
921 Matches any non word character\n\ | |
922 @item \\<\n\ | |
923 Matches the beginning of a word\n\ | |
924 @item \\>\n\ | |
925 Matches the end of a word\n\ | |
926 @item \\s\n\ | |
927 Matches any whitespace character\n\ | |
928 @item \\S\n\ | |
929 Matches any non whitespace character\n\ | |
930 @item \\d\n\ | |
931 Matches any digit\n\ | |
932 @item \\D\n\ | |
933 Matches any non-digit\n\ | |
934 @end table\n\ | |
935 \n\ | |
936 The outputs of @code{regexp} by default are in the order as given below\n\ | |
937 \n\ | |
938 @table @asis\n\ | |
939 @item @var{s}\n\ | |
940 The start indices of each of the matching substrings\n\ | |
941 \n\ | |
942 @item @var{e}\n\ | |
943 The end indices of each matching substring\n\ | |
944 \n\ | |
945 @item @var{te}\n\ | |
946 The extents of each of the matched token surrounded by @code{(@dots{})} in\n\ | |
947 @var{pat}.\n\ | |
948 \n\ | |
949 @item @var{m}\n\ | |
950 A cell array of the text of each match.\n\ | |
951 \n\ | |
952 @item @var{t}\n\ | |
953 A cell array of the text of each token matched.\n\ | |
954 \n\ | |
955 @item @var{nm}\n\ | |
956 A structure containing the text of each matched named token, with the name\n\ | |
957 being used as the fieldname. A named token is denoted as\n\ | |
958 @code{(?<name>@dots{})}\n\ | |
959 @end table\n\ | |
960 \n\ | |
961 Particular output arguments or the order of the output arguments can be\n\ | |
962 selected by additional @var{opts} arguments. These are strings and the\n\ | |
963 correspondence between the output arguments and the optional argument\n\ | |
964 are\n\ | |
965 \n\ | |
966 @multitable @columnfractions 0.2 0.3 0.3 0.2\n\ | |
967 @item @tab 'start' @tab @var{s} @tab\n\ | |
968 @item @tab 'end' @tab @var{e} @tab\n\ | |
969 @item @tab 'tokenExtents' @tab @var{te} @tab\n\ | |
970 @item @tab 'match' @tab @var{m} @tab\n\ | |
971 @item @tab 'tokens' @tab @var{t} @tab\n\ | |
972 @item @tab 'names' @tab @var{nm} @tab\n\ | |
973 @end multitable\n\ | |
974 \n\ | |
975 A further optional argument is 'once', that limits the number of returned\n\ | |
5779 | 976 matches to the first match. Additional arguments are\n\ |
977 \n\ | |
978 @table @asis\n\ | |
979 @item matchcase\n\ | |
980 Make the matching case sensitive.\n\ | |
981 @item ignorecase\n\ | |
982 Make the matching case insensitive.\n\ | |
983 @item stringanchors\n\ | |
984 Match the anchor characters at the beginning and end of the string.\n\ | |
985 @item lineanchors\n\ | |
986 Match the anchor characters at the beginning and end of the line.\n\ | |
987 @item dotall\n\ | |
988 The character @code{.} matches the newline character.\n\ | |
989 @item dotexceptnewline\n\ | |
990 The character @code{.} matches all but the newline character.\n\ | |
991 @item freespacing\n\ | |
992 The pattern can include arbitrary whitespace and comments starting with\n\ | |
993 @code{#}.\n\ | |
994 @item literalspacing\n\ | |
995 The pattern is taken literally.\n\ | |
996 @end table\n\ | |
8442
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8377
diff
changeset
|
997 @seealso{regexpi, regexprep}\n\ |
5582 | 998 @end deftypefn") |
999 { | |
6361 | 1000 octave_value_list retval; |
1001 int nargin = args.length(); | |
1002 | |
1003 if (nargin < 2) | |
1004 print_usage (); | |
1005 else if (args(0).is_cell() || args(1).is_cell()) | |
1006 retval = octcellregexp (args, nargout, "regexp", false); | |
1007 else | |
1008 retval = octregexp (args, nargout, "regexp", false); | |
1009 | |
1010 return retval; | |
5582 | 1011 } |
1012 | |
1013 /* | |
1014 | |
8140
cdd05e46f6c9
Increase pcre's match_limit for difficult regexps
Thomas Weber <thomas.weber.mail@gmail.com>
parents:
8093
diff
changeset
|
1015 ## PCRE_ERROR_MATCHLIMIT test |
cdd05e46f6c9
Increase pcre's match_limit for difficult regexps
Thomas Weber <thomas.weber.mail@gmail.com>
parents:
8093
diff
changeset
|
1016 %!test |
cdd05e46f6c9
Increase pcre's match_limit for difficult regexps
Thomas Weber <thomas.weber.mail@gmail.com>
parents:
8093
diff
changeset
|
1017 %! s=sprintf('\t4\n0000\t-0.00\t-0.0000\t4\t-0.00\t-0.0000\t4\n0000\t-0.00\t-0.0000\t0\t-0.00\t-'); |
cdd05e46f6c9
Increase pcre's match_limit for difficult regexps
Thomas Weber <thomas.weber.mail@gmail.com>
parents:
8093
diff
changeset
|
1018 %! ws = warning("query"); |
cdd05e46f6c9
Increase pcre's match_limit for difficult regexps
Thomas Weber <thomas.weber.mail@gmail.com>
parents:
8093
diff
changeset
|
1019 %! unwind_protect |
cdd05e46f6c9
Increase pcre's match_limit for difficult regexps
Thomas Weber <thomas.weber.mail@gmail.com>
parents:
8093
diff
changeset
|
1020 %! warning("off"); |
cdd05e46f6c9
Increase pcre's match_limit for difficult regexps
Thomas Weber <thomas.weber.mail@gmail.com>
parents:
8093
diff
changeset
|
1021 %! regexp(s, '(\s*-*\d+[.]*\d*\s*)+\n'); |
cdd05e46f6c9
Increase pcre's match_limit for difficult regexps
Thomas Weber <thomas.weber.mail@gmail.com>
parents:
8093
diff
changeset
|
1022 %! unwind_protect_cleanup |
cdd05e46f6c9
Increase pcre's match_limit for difficult regexps
Thomas Weber <thomas.weber.mail@gmail.com>
parents:
8093
diff
changeset
|
1023 %! warning(ws); |
cdd05e46f6c9
Increase pcre's match_limit for difficult regexps
Thomas Weber <thomas.weber.mail@gmail.com>
parents:
8093
diff
changeset
|
1024 %! end_unwind_protect |
cdd05e46f6c9
Increase pcre's match_limit for difficult regexps
Thomas Weber <thomas.weber.mail@gmail.com>
parents:
8093
diff
changeset
|
1025 |
5582 | 1026 ## seg-fault test |
1027 %!assert(regexp("abcde","."),[1,2,3,4,5]) | |
1028 | |
1029 ## Check that anchoring of pattern works correctly | |
1030 %!assert(regexp('abcabc','^abc'),1); | |
1031 %!assert(regexp('abcabc','abc$'),4); | |
5785 | 1032 %!assert(regexp('abcabc','^abc$'),zeros(1,0)); |
5582 | 1033 |
1034 %!test | |
1035 %! [s, e, te, m, t] = regexp(' No Match ', 'f(.*)uck'); | |
5785 | 1036 %! assert (s,zeros(1,0)) |
1037 %! assert (e,zeros(1,0)) | |
1038 %! assert (te,cell(1,0)) | |
1039 %! assert (m, cell(1,0)) | |
1040 %! assert (t, cell(1,0)) | |
5582 | 1041 |
1042 %!test | |
1043 %! [s, e, te, m, t] = regexp(' FiRetrUck ', 'f(.*)uck'); | |
5785 | 1044 %! assert (s,zeros(1,0)) |
1045 %! assert (e,zeros(1,0)) | |
1046 %! assert (te,cell(1,0)) | |
1047 %! assert (m, cell(1,0)) | |
1048 %! assert (t, cell(1,0)) | |
5582 | 1049 |
1050 %!test | |
1051 %! [s, e, te, m, t] = regexp(' firetruck ', 'f(.*)uck'); | |
1052 %! assert (s,2) | |
1053 %! assert (e,10) | |
1054 %! assert (te{1},[3,7]) | |
1055 %! assert (m{1}, 'firetruck') | |
1056 %! assert (t{1}{1}, 'iretr') | |
1057 | |
1058 %!test | |
1059 %! [s, e, te, m, t] = regexp('short test string','\w*r\w*'); | |
1060 %! assert (s,[1,12]) | |
1061 %! assert (e,[5,17]) | |
1062 %! assert (size(te), [1,2]) | |
1063 %! assert (isempty(te{1})) | |
1064 %! assert (isempty(te{2})) | |
1065 %! assert (m{1},'short') | |
1066 %! assert (m{2},'string') | |
1067 %! assert (size(t), [1,2]) | |
1068 %! assert (isempty(t{1})) | |
1069 %! assert (isempty(t{2})) | |
1070 | |
1071 %!test | |
1072 %! [s, e, te, m, t] = regexp('short test string','\w*r\w*','once'); | |
1073 %! assert (s,1) | |
1074 %! assert (e,5) | |
7893
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
1075 %! assert (isempty(te)) |
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
1076 %! assert (m,'short') |
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
1077 %! assert (isempty(t)) |
5582 | 1078 |
1079 %!test | |
1080 %! [m, te, e, s, t] = regexp('short test string','\w*r\w*','once', 'match', 'tokenExtents', 'end', 'start', 'tokens'); | |
1081 %! assert (s,1) | |
1082 %! assert (e,5) | |
7893
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
1083 %! assert (isempty(te)) |
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
1084 %! assert (m,'short') |
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
1085 %! assert (isempty(t)) |
5582 | 1086 |
7242 | 1087 %!testif HAVE_PCRE |
5582 | 1088 %! ## This test is expected to fail if PCRE is not installed |
7242 | 1089 %! [s, e, te, m, t, nm] = regexp('short test string','(?<word1>\w*t)\s*(?<word2>\w*t)'); |
1090 %! assert (s,1) | |
1091 %! assert (e,10) | |
1092 %! assert (size(te), [1,1]) | |
1093 %! assert (te{1}, [1 5; 7, 10]) | |
1094 %! assert (m{1},'short test') | |
1095 %! assert (size(t),[1,1]) | |
1096 %! assert (t{1}{1},'short') | |
1097 %! assert (t{1}{2},'test') | |
1098 %! assert (size(nm), [1,1]) | |
1099 %! assert (!isempty(fieldnames(nm))) | |
1100 %! assert (sort(fieldnames(nm)),{'word1';'word2'}) | |
1101 %! assert (nm.word1,'short') | |
1102 %! assert (nm.word2,'test') | |
5582 | 1103 |
7242 | 1104 %!testif HAVE_PCRE |
5582 | 1105 %! ## This test is expected to fail if PCRE is not installed |
7242 | 1106 %! [nm, m, te, e, s, t] = regexp('short test string','(?<word1>\w*t)\s*(?<word2>\w*t)', 'names', 'match', 'tokenExtents', 'end', 'start', 'tokens'); |
1107 %! assert (s,1) | |
1108 %! assert (e,10) | |
1109 %! assert (size(te), [1,1]) | |
1110 %! assert (te{1}, [1 5; 7, 10]) | |
1111 %! assert (m{1},'short test') | |
1112 %! assert (size(t),[1,1]) | |
1113 %! assert (t{1}{1},'short') | |
1114 %! assert (t{1}{2},'test') | |
1115 %! assert (size(nm), [1,1]) | |
1116 %! assert (!isempty(fieldnames(nm))) | |
1117 %! assert (sort(fieldnames(nm)),{'word1';'word2'}) | |
1118 %! assert (nm.word1,'short') | |
1119 %! assert (nm.word2,'test') | |
5619 | 1120 |
7242 | 1121 %!testif HAVE_PCRE |
5619 | 1122 %! ## This test is expected to fail if PCRE is not installed |
7242 | 1123 %! [t, nm] = regexp("John Davis\nRogers, James",'(?<first>\w+)\s+(?<last>\w+)|(?<last>\w+),\s+(?<first>\w+)','tokens','names'); |
1124 %! assert (size(t), [1,2]); | |
1125 %! assert (t{1}{1},'John'); | |
1126 %! assert (t{1}{2},'Davis'); | |
1127 %! assert (t{2}{1},'Rogers'); | |
1128 %! assert (t{2}{2},'James'); | |
1129 %! assert (size(nm), [1,1]); | |
1130 %! assert (nm.first{1},'John'); | |
1131 %! assert (nm.first{2},'James'); | |
1132 %! assert (nm.last{1},'Davis'); | |
1133 %! assert (nm.last{2},'Rogers'); | |
5582 | 1134 |
5779 | 1135 %!assert(regexp("abc\nabc",'.'),[1:7]) |
1136 %!assert(regexp("abc\nabc",'.','dotall'),[1:7]) | |
7242 | 1137 %!testif HAVE_PCRE |
1138 %! assert(regexp("abc\nabc",'(?s).'),[1:7]) | |
1139 %! assert(regexp("abc\nabc",'.','dotexceptnewline'),[1,2,3,5,6,7]) | |
1140 %! assert(regexp("abc\nabc",'(?-s).'),[1,2,3,5,6,7]) | |
5779 | 1141 |
1142 %!assert(regexp("caseCaSe",'case'),1) | |
1143 %!assert(regexp("caseCaSe",'case',"matchcase"),1) | |
1144 %!assert(regexp("caseCaSe",'case',"ignorecase"),[1,5]) | |
7242 | 1145 %!testif HAVE_PCRE |
1146 %! assert(regexp("caseCaSe",'(?-i)case'),1) | |
1147 %! assert(regexp("caseCaSe",'(?i)case'),[1,5]) | |
5779 | 1148 |
1149 %!assert (regexp("abc\nabc",'c$'),7) | |
1150 %!assert (regexp("abc\nabc",'c$',"stringanchors"),7) | |
7242 | 1151 %!testif HAVE_PCRE |
1152 %! assert (regexp("abc\nabc",'(?-m)c$'),7) | |
1153 %! assert (regexp("abc\nabc",'c$',"lineanchors"),[3,7]) | |
1154 %! assert (regexp("abc\nabc",'(?m)c$'),[3,7]) | |
5779 | 1155 |
1156 %!assert (regexp("this word",'s w'),4) | |
1157 %!assert (regexp("this word",'s w','literalspacing'),4) | |
7242 | 1158 %!testif HAVE_PCRE |
1159 %! assert (regexp("this word",'(?-x)s w','literalspacing'),4) | |
1160 %! assert (regexp("this word",'s w','freespacing'),zeros(1,0)) | |
1161 %! assert (regexp("this word",'(?x)s w'),zeros(1,0)) | |
5779 | 1162 |
5582 | 1163 %!error regexp('string', 'tri', 'BadArg'); |
1164 %!error regexp('string'); | |
1165 | |
6361 | 1166 %!assert(regexp({'asdfg-dfd';'-dfd-dfd-';'qasfdfdaq'},'-'),{6;[1,5,9];zeros(1,0)}) |
1167 %!assert(regexp({'asdfg-dfd','-dfd-dfd-','qasfdfdaq'},'-'),{6,[1,5,9],zeros(1,0)}) | |
1168 %!assert(regexp({'asdfg-dfd';'-dfd-dfd-';'qasfdfdaq'},{'-';'f';'q'}),{6;[3,7];[1,9]}) | |
1169 %!assert(regexp('Strings',{'t','s'}),{2,7}) | |
1170 | |
8093
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
1171 ## Test case for lookaround operators |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
1172 %!assert(regexp('Iraq','q(?!u)'),4) |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
1173 %!assert(regexp('quit','q(?!u)'), zeros(1,0)) |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
1174 %!assert(regexp('quit','q(?=u)','match'), {'q'}) |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
1175 %!assert(regexp("quit",'q(?=u+)','match'), {'q'}) |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
1176 %!assert(regexp("qit",'q(?=u+)','match'), cell(1,0)) |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
1177 %!assert(regexp("qit",'q(?=u*)','match'), {'q'}) |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
1178 |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
1179 %!assert(regexp('thingamabob','(?<=a)b'), 9) |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
1180 |
5582 | 1181 */ |
1182 | |
6549 | 1183 DEFUN_DLD (regexpi, args, nargout, |
5582 | 1184 "-*- texinfo -*-\n\ |
1185 @deftypefn {Loadable Function} {[@var{s}, @var{e}, @var{te}, @var{m}, @var{t}, @var{nm}] =} regexpi (@var{str}, @var{pat})\n\ | |
1186 @deftypefnx {Loadable Function} {[@dots{}] =} regexpi (@var{str}, @var{pat}, @var{opts}, @dots{})\n\ | |
1187 \n\ | |
1188 Case insensitive regular expression string matching. Matches @var{pat} in\n\ | |
1189 @var{str} and returns the position and matching substrings or empty values\n\ | |
8442
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8377
diff
changeset
|
1190 if there are none. @xref{doc-regexp,,regexp}, for more details\n\ |
5582 | 1191 @end deftypefn") |
1192 { | |
6361 | 1193 octave_value_list retval; |
1194 int nargin = args.length(); | |
1195 | |
1196 if (nargin < 2) | |
1197 print_usage (); | |
1198 else if (args(0).is_cell() || args(1).is_cell()) | |
1199 retval = octcellregexp (args, nargout, "regexpi", true); | |
1200 else | |
1201 retval = octregexp (args, nargout, "regexpi", true); | |
1202 | |
1203 return retval; | |
5582 | 1204 } |
1205 | |
1206 /* | |
1207 | |
1208 ## seg-fault test | |
1209 %!assert(regexpi("abcde","."),[1,2,3,4,5]) | |
1210 | |
1211 ## Check that anchoring of pattern works correctly | |
1212 %!assert(regexpi('abcabc','^abc'),1); | |
1213 %!assert(regexpi('abcabc','abc$'),4); | |
5785 | 1214 %!assert(regexpi('abcabc','^abc$'),zeros(1,0)); |
5582 | 1215 |
1216 %!test | |
1217 %! [s, e, te, m, t] = regexpi(' No Match ', 'f(.*)uck'); | |
5785 | 1218 %! assert (s,zeros(1,0)) |
1219 %! assert (e,zeros(1,0)) | |
1220 %! assert (te,cell(1,0)) | |
1221 %! assert (m, cell(1,0)) | |
1222 %! assert (t, cell(1,0)) | |
5582 | 1223 |
1224 %!test | |
1225 %! [s, e, te, m, t] = regexpi(' FiRetrUck ', 'f(.*)uck'); | |
1226 %! assert (s,2) | |
1227 %! assert (e,10) | |
1228 %! assert (te{1},[3,7]) | |
1229 %! assert (m{1}, 'FiRetrUck') | |
1230 %! assert (t{1}{1}, 'iRetr') | |
1231 | |
1232 %!test | |
1233 %! [s, e, te, m, t] = regexpi(' firetruck ', 'f(.*)uck'); | |
1234 %! assert (s,2) | |
1235 %! assert (e,10) | |
1236 %! assert (te{1},[3,7]) | |
1237 %! assert (m{1}, 'firetruck') | |
1238 %! assert (t{1}{1}, 'iretr') | |
1239 | |
1240 %!test | |
1241 %! [s, e, te, m, t] = regexpi('ShoRt Test String','\w*r\w*'); | |
1242 %! assert (s,[1,12]) | |
1243 %! assert (e,[5,17]) | |
1244 %! assert (size(te), [1,2]) | |
1245 %! assert (isempty(te{1})) | |
1246 %! assert (isempty(te{2})) | |
1247 %! assert (m{1},'ShoRt') | |
1248 %! assert (m{2},'String') | |
1249 %! assert (size(t), [1,2]) | |
1250 %! assert (isempty(t{1})) | |
1251 %! assert (isempty(t{2})) | |
1252 | |
1253 %!test | |
1254 %! [s, e, te, m, t] = regexpi('ShoRt Test String','\w*r\w*','once'); | |
1255 %! assert (s,1) | |
1256 %! assert (e,5) | |
7893
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
1257 %! assert (isempty(te)) |
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
1258 %! assert (m,'ShoRt') |
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
1259 %! assert (isempty(t)) |
5582 | 1260 |
1261 %!test | |
1262 %! [m, te, e, s, t] = regexpi('ShoRt Test String','\w*r\w*','once', 'match', 'tokenExtents', 'end', 'start', 'tokens'); | |
1263 %! assert (s,1) | |
1264 %! assert (e,5) | |
7893
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
1265 %! assert (isempty(te)) |
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
1266 %! assert (m,'ShoRt') |
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
1267 %! assert (isempty(t)) |
5582 | 1268 |
7242 | 1269 %!testif HAVE_PCRE |
5582 | 1270 %! ## This test is expected to fail if PCRE is not installed |
7242 | 1271 %! [s, e, te, m, t, nm] = regexpi('ShoRt Test String','(?<word1>\w*t)\s*(?<word2>\w*t)'); |
1272 %! assert (s,1) | |
1273 %! assert (e,10) | |
1274 %! assert (size(te), [1,1]) | |
1275 %! assert (te{1}, [1 5; 7, 10]) | |
1276 %! assert (m{1},'ShoRt Test') | |
1277 %! assert (size(t),[1,1]) | |
1278 %! assert (t{1}{1},'ShoRt') | |
1279 %! assert (t{1}{2},'Test') | |
1280 %! assert (size(nm), [1,1]) | |
1281 %! assert (!isempty(fieldnames(nm))) | |
1282 %! assert (sort(fieldnames(nm)),{'word1';'word2'}) | |
1283 %! assert (nm.word1,'ShoRt') | |
1284 %! assert (nm.word2,'Test') | |
5582 | 1285 |
7242 | 1286 %!testif HAVE_PCRE |
5582 | 1287 %! ## This test is expected to fail if PCRE is not installed |
7242 | 1288 %! [nm, m, te, e, s, t] = regexpi('ShoRt Test String','(?<word1>\w*t)\s*(?<word2>\w*t)', 'names', 'match', 'tokenExtents', 'end', 'start', 'tokens'); |
1289 %! assert (s,1) | |
1290 %! assert (e,10) | |
1291 %! assert (size(te), [1,1]) | |
1292 %! assert (te{1}, [1 5; 7, 10]) | |
1293 %! assert (m{1},'ShoRt Test') | |
1294 %! assert (size(t),[1,1]) | |
1295 %! assert (t{1}{1},'ShoRt') | |
1296 %! assert (t{1}{2},'Test') | |
1297 %! assert (size(nm), [1,1]) | |
1298 %! assert (!isempty(fieldnames(nm))) | |
1299 %! assert (sort(fieldnames(nm)),{'word1';'word2'}) | |
1300 %! assert (nm.word1,'ShoRt') | |
1301 %! assert (nm.word2,'Test') | |
5582 | 1302 |
5779 | 1303 %!assert(regexpi("abc\nabc",'.'),[1:7]) |
1304 %!assert(regexpi("abc\nabc",'.','dotall'),[1:7]) | |
7242 | 1305 %!testif HAVE_PCRE |
1306 %! assert(regexpi("abc\nabc",'(?s).'),[1:7]) | |
1307 %! assert(regexpi("abc\nabc",'.','dotexceptnewline'),[1,2,3,5,6,7]) | |
1308 %! assert(regexpi("abc\nabc",'(?-s).'),[1,2,3,5,6,7]) | |
5779 | 1309 |
1310 %!assert(regexpi("caseCaSe",'case'),[1,5]) | |
1311 %!assert(regexpi("caseCaSe",'case',"matchcase"),1) | |
1312 %!assert(regexpi("caseCaSe",'case',"ignorecase"),[1,5]) | |
7242 | 1313 %!testif HAVE_PCRE |
1314 %! assert(regexpi("caseCaSe",'(?-i)case'),1) | |
1315 %! assert(regexpi("caseCaSe",'(?i)case'),[1,5]) | |
5779 | 1316 |
1317 %!assert (regexpi("abc\nabc",'c$'),7) | |
1318 %!assert (regexpi("abc\nabc",'c$',"stringanchors"),7) | |
7242 | 1319 %!testif HAVE_PCRE |
1320 %! assert (regexpi("abc\nabc",'(?-m)c$'),7) | |
1321 %! assert (regexpi("abc\nabc",'c$',"lineanchors"),[3,7]) | |
1322 %! assert (regexpi("abc\nabc",'(?m)c$'),[3,7]) | |
5779 | 1323 |
1324 %!assert (regexpi("this word",'s w'),4) | |
1325 %!assert (regexpi("this word",'s w','literalspacing'),4) | |
7242 | 1326 %!testif HAVE_PCRE |
1327 %! assert (regexpi("this word",'(?-x)s w','literalspacing'),4) | |
1328 %! assert (regexpi("this word",'s w','freespacing'),zeros(1,0)) | |
1329 %! assert (regexpi("this word",'(?x)s w'),zeros(1,0)) | |
5779 | 1330 |
5582 | 1331 %!error regexpi('string', 'tri', 'BadArg'); |
1332 %!error regexpi('string'); | |
1333 | |
6361 | 1334 %!assert(regexpi({'asdfg-dfd';'-dfd-dfd-';'qasfdfdaq'},'-'),{6;[1,5,9];zeros(1,0)}) |
1335 %!assert(regexpi({'asdfg-dfd','-dfd-dfd-','qasfdfdaq'},'-'),{6,[1,5,9],zeros(1,0)}) | |
1336 %!assert(regexpi({'asdfg-dfd';'-dfd-dfd-';'qasfdfdaq'},{'-';'f';'q'}),{6;[3,7];[1,9]}) | |
1337 %!assert(regexpi('Strings',{'t','s'}),{2,[1,7]}) | |
1338 | |
5582 | 1339 */ |
1340 | |
6361 | 1341 |
1342 static octave_value | |
1343 octregexprep (const octave_value_list &args, const std::string &nm) | |
5785 | 1344 { |
6361 | 1345 octave_value retval; |
5785 | 1346 int nargin = args.length(); |
1347 | |
1348 // Make sure we have string,pattern,replacement | |
1349 const std::string buffer = args(0).string_value (); | |
1350 if (error_state) return retval; | |
1351 const std::string pattern = args(1).string_value (); | |
1352 if (error_state) return retval; | |
1353 const std::string replacement = args(2).string_value (); | |
1354 if (error_state) return retval; | |
1355 | |
1356 // Pack options excluding 'tokenize' and various output | |
1357 // reordering strings into regexp arg list | |
1358 octave_value_list regexpargs(nargin-1,octave_value()); | |
1359 regexpargs(0) = args(0); | |
1360 regexpargs(1) = args(1); | |
1361 int len=2; | |
1362 for (int i = 3; i < nargin; i++) | |
1363 { | |
1364 const std::string opt = args(i).string_value(); | |
1365 if (opt != "tokenize" && opt != "start" && opt != "end" | |
1366 && opt != "tokenextents" && opt != "match" && opt != "tokens" | |
1367 && opt != "names" && opt != "warnings") | |
1368 { | |
1369 regexpargs(len++) = args(i); | |
1370 } | |
1371 } | |
1372 regexpargs.resize(len); | |
1373 | |
1374 // Identify replacement tokens; build a vector of group numbers in | |
1375 // the replacement string so that we can quickly calculate the size | |
1376 // of the replacement. | |
1377 int tokens = 0; | |
1378 for (size_t i=1; i < replacement.size(); i++) | |
1379 { | |
1380 if (replacement[i-1]=='$' && isdigit(replacement[i])) | |
1381 { | |
1382 tokens++, i++; | |
1383 } | |
1384 } | |
1385 std::vector<int> token(tokens); | |
1386 int kk = 0; | |
1387 for (size_t i = 1; i < replacement.size(); i++) | |
1388 { | |
1389 if (replacement[i-1]=='$' && isdigit(replacement[i])) | |
1390 { | |
1391 token[kk++] = replacement[i]-'0'; | |
1392 i++; | |
1393 } | |
1394 } | |
1395 | |
1396 // Perform replacement | |
1397 std::string rep; | |
1398 if (tokens > 0) | |
1399 { | |
1400 std::list<regexp_elem> lst; | |
1401 string_vector named; | |
1402 int nopts; | |
7893
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
1403 bool once; |
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
1404 int sz = octregexp_list (regexpargs, nm , false, lst, named, nopts, once); |
5785 | 1405 |
1406 if (error_state) | |
1407 return retval; | |
1408 if (sz == 0) | |
1409 { | |
6361 | 1410 retval = args(0); |
5785 | 1411 return retval; |
1412 } | |
1413 | |
1414 // Determine replacement length | |
1415 const size_t replen = replacement.size() - 2*tokens; | |
1416 int delta = 0; | |
1417 const_iterator p = lst.begin(); | |
1418 for (int i = 0; i < sz; i++) | |
1419 { | |
1420 OCTAVE_QUIT; | |
1421 | |
1422 const Matrix pairs(p->te); | |
1423 size_t pairlen = 0; | |
1424 for (int j = 0; j < tokens; j++) | |
1425 { | |
1426 if (token[j] == 0) | |
1427 pairlen += static_cast<size_t>(p->e - p->s) + 1; | |
1428 else if (token[j] <= pairs.rows()) | |
1429 pairlen += static_cast<size_t>(pairs(token[j]-1,1) - | |
1430 pairs(token[j]-1,0)) + 1; | |
1431 } | |
1432 delta += static_cast<int>(replen + pairlen) - | |
1433 static_cast<int>(p->e - p->s + 1); | |
1434 p++; | |
1435 } | |
1436 | |
1437 // Build replacement string | |
1438 rep.reserve(buffer.size()+delta); | |
1439 size_t from = 0; | |
1440 p = lst.begin(); | |
1441 for (int i=0; i < sz; i++) | |
1442 { | |
1443 OCTAVE_QUIT; | |
1444 | |
1445 const Matrix pairs(p->te); | |
1446 rep.append(&buffer[from], static_cast<size_t>(p->s - 1) - from); | |
1447 from = static_cast<size_t>(p->e - 1) + 1; | |
1448 for (size_t j = 1; j < replacement.size(); j++) | |
1449 { | |
1450 if (replacement[j-1]=='$' && isdigit(replacement[j])) | |
1451 { | |
1452 int k = replacement[j]-'0'; | |
1453 if (k == 0) | |
1454 { | |
1455 // replace with entire match | |
1456 rep.append(&buffer[static_cast<size_t>(p->e - 1)], | |
1457 static_cast<size_t>(p->e - p->s) + 1); | |
1458 } | |
1459 else if (k <= pairs.rows()) | |
1460 { | |
1461 // replace with group capture | |
1462 rep.append(&buffer[static_cast<size_t>(pairs(k-1,0)-1)], | |
1463 static_cast<size_t>(pairs(k-1,1) - | |
1464 pairs(k-1,0))+1); | |
1465 } | |
1466 else | |
1467 { | |
1468 // replace with nothing | |
1469 } | |
1470 j++; | |
1471 } | |
1472 else | |
1473 { | |
1474 rep.append(1,replacement[j-1]); | |
1475 } | |
1476 if (j+1 == replacement.size()) | |
1477 { | |
1478 rep.append(1,replacement[j]); | |
1479 } | |
1480 } | |
1481 p++; | |
1482 } | |
1483 rep.append(&buffer[from],buffer.size()-from); | |
1484 } | |
1485 else | |
1486 { | |
1487 std::list<regexp_elem> lst; | |
1488 string_vector named; | |
1489 int nopts; | |
7893
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
1490 bool once; |
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
1491 int sz = octregexp_list (regexpargs, nm, false, lst, named, nopts, once); |
5785 | 1492 |
1493 if (error_state) | |
1494 return retval; | |
1495 if (sz == 0) | |
1496 { | |
6361 | 1497 retval = args(0); |
5785 | 1498 return retval; |
1499 } | |
1500 | |
1501 // Determine replacement length | |
1502 const size_t replen = replacement.size(); | |
1503 int delta = 0; | |
1504 const_iterator p = lst.begin(); | |
1505 for (int i = 0; i < sz; i++) | |
1506 { | |
1507 OCTAVE_QUIT; | |
1508 delta += static_cast<int>(replen) - | |
1509 static_cast<int>(p->e - p->s + 1); | |
1510 p++; | |
1511 } | |
1512 | |
1513 // Build replacement string | |
1514 rep.reserve(buffer.size()+delta); | |
1515 size_t from = 0; | |
1516 p = lst.begin(); | |
1517 for (int i=0; i < sz; i++) | |
1518 { | |
1519 OCTAVE_QUIT; | |
1520 rep.append(&buffer[from], static_cast<size_t>(p->s - 1) - from); | |
1521 from = static_cast<size_t>(p->e - 1) + 1; | |
1522 rep.append(replacement); | |
1523 p++; | |
1524 } | |
1525 rep.append(&buffer[from],buffer.size()-from); | |
1526 } | |
1527 | |
6361 | 1528 retval = rep; |
1529 return retval; | |
1530 } | |
1531 | |
6549 | 1532 DEFUN_DLD (regexprep, args, , |
6361 | 1533 "-*- texinfo -*-\n\ |
6678 | 1534 @deftypefn {Loadable Function} {@var{string} =} regexprep (@var{string}, @var{pat}, @var{repstr}, @var{options})\n\ |
6361 | 1535 Replace matches of @var{pat} in @var{string} with @var{repstr}.\n\ |
1536 \n\ | |
1537 \n\ | |
7007 | 1538 The replacement can contain @code{$i}, which substitutes\n\ |
6361 | 1539 for the ith set of parentheses in the match string. E.g.,\n\ |
1540 @example\n\ | |
1541 \n\ | |
1542 regexprep(\"Bill Dunn\",'(\\w+) (\\w+)','$2, $1')\n\ | |
1543 \n\ | |
1544 @end example\n\ | |
1545 returns \"Dunn, Bill\"\n\ | |
1546 \n\ | |
1547 @var{options} may be zero or more of\n\ | |
1548 @table @samp\n\ | |
1549 \n\ | |
1550 @item once\n\ | |
7001 | 1551 Replace only the first occurrence of @var{pat} in the result.\n\ |
6361 | 1552 \n\ |
1553 @item warnings\n\ | |
1554 This option is present for compatibility but is ignored.\n\ | |
1555 \n\ | |
1556 @item ignorecase or matchcase\n\ | |
1557 Ignore case for the pattern matching (see @code{regexpi}).\n\ | |
1558 Alternatively, use (?i) or (?-i) in the pattern.\n\ | |
1559 \n\ | |
1560 @item lineanchors and stringanchors\n\ | |
1561 Whether characters ^ and $ match the beginning and ending of lines.\n\ | |
1562 Alternatively, use (?m) or (?-m) in the pattern.\n\ | |
1563 \n\ | |
1564 @item dotexceptnewline and dotall\n\ | |
1565 Whether . matches newlines in the string.\n\ | |
1566 Alternatively, use (?s) or (?-s) in the pattern.\n\ | |
1567 \n\ | |
1568 @item freespacing or literalspacing\n\ | |
1569 Whether whitespace and # comments can be used to make the regular expression more readable.\n\ | |
1570 Alternatively, use (?x) or (?-x) in the pattern.\n\ | |
1571 \n\ | |
1572 @end table\n\ | |
8442
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8377
diff
changeset
|
1573 @seealso{regexp,regexpi,strrep}\n\ |
6361 | 1574 @end deftypefn") |
1575 { | |
1576 octave_value_list retval; | |
1577 int nargin = args.length(); | |
1578 | |
1579 if (nargin < 3) | |
1580 { | |
1581 print_usage (); | |
1582 return retval; | |
1583 } | |
1584 | |
1585 if (args(0).is_cell() || args(1).is_cell() || args(2).is_cell()) | |
1586 { | |
1587 Cell str; | |
1588 Cell pat; | |
1589 Cell rep; | |
6495 | 1590 dim_vector dv0; |
1591 dim_vector dv1(1,1); | |
6361 | 1592 |
1593 if (args(0).is_cell()) | |
1594 str = args(0).cell_value(); | |
1595 else | |
1596 str = Cell (args(0)); | |
1597 | |
1598 if (args(1).is_cell()) | |
1599 pat = args(1).cell_value(); | |
1600 else | |
1601 pat = Cell (args(1)); | |
1602 | |
1603 if (args(2).is_cell()) | |
1604 rep = args(2).cell_value(); | |
1605 else | |
1606 rep = Cell (args(2)); | |
1607 | |
6495 | 1608 dv0 = str.dims(); |
1609 if (pat.numel() != 1) | |
6361 | 1610 { |
6495 | 1611 dv1 = pat.dims(); |
1612 if (rep.numel() != 1 && dv1 != rep.dims()) | |
6361 | 1613 error ("regexprep: Inconsistent cell array dimensions"); |
1614 } | |
1615 else if (rep.numel() != 1) | |
6495 | 1616 dv1 = rep.dims(); |
6361 | 1617 |
1618 if (!error_state) | |
1619 { | |
6495 | 1620 Cell ret (dv0); |
6361 | 1621 octave_value_list new_args = args; |
1622 | |
6495 | 1623 for (octave_idx_type i = 0; i < dv0.numel(); i++) |
1624 { | |
1625 new_args(0) = str(i); | |
1626 if (pat.numel() == 1) | |
1627 new_args(1) = pat(0); | |
1628 if (rep.numel() == 1) | |
1629 new_args(2) = rep(0); | |
1630 for (octave_idx_type j = 0; j < dv1.numel(); j++) | |
1631 { | |
1632 if (pat.numel() != 1) | |
1633 new_args(1) = pat(j); | |
1634 if (rep.numel() != 1) | |
1635 new_args(2) = rep(j); | |
1636 new_args(0) = octregexprep (new_args, "regexprep"); | |
6361 | 1637 |
6495 | 1638 if (error_state) |
1639 break; | |
1640 } | |
6361 | 1641 |
1642 if (error_state) | |
1643 break; | |
6495 | 1644 |
1645 ret(i) = new_args(0); | |
6361 | 1646 } |
1647 | |
1648 if (!error_state) | |
1649 retval = octave_value (ret); | |
1650 } | |
1651 } | |
1652 else | |
1653 retval = octregexprep (args, "regexprep"); | |
1654 | |
5785 | 1655 return retval; |
1656 } | |
1657 | |
1658 /* | |
1659 %!test # Replace with empty | |
1660 %! xml = '<!-- This is some XML --> <tag v="hello">some stuff<!-- sample tag--></tag>'; | |
1661 %! t = regexprep(xml,'<[!?][^>]*>',''); | |
1662 %! assert(t,' <tag v="hello">some stuff</tag>') | |
1663 | |
1664 %!test # Replace with non-empty | |
1665 %! xml = '<!-- This is some XML --> <tag v="hello">some stuff<!-- sample tag--></tag>'; | |
1666 %! t = regexprep(xml,'<[!?][^>]*>','?'); | |
1667 %! assert(t,'? <tag v="hello">some stuff?</tag>') | |
1668 | |
1669 %!test # Check that 'tokenize' is ignored | |
1670 %! xml = '<!-- This is some XML --> <tag v="hello">some stuff<!-- sample tag--></tag>'; | |
1671 %! t = regexprep(xml,'<[!?][^>]*>','','tokenize'); | |
1672 %! assert(t,' <tag v="hello">some stuff</tag>') | |
1673 | |
7242 | 1674 %!testif HAVE_PCRE # Capture replacement |
1675 %! data = "Bob Smith\nDavid Hollerith\nSam Jenkins"; | |
1676 %! result = "Smith, Bob\nHollerith, David\nJenkins, Sam"; | |
1677 %! t = regexprep(data,'(?m)^(\w+)\s+(\w+)$','$2, $1'); | |
1678 %! assert(t,result) | |
5785 | 1679 |
1680 # Return the original if no match | |
1681 %!assert(regexprep('hello','world','earth'),'hello') | |
1682 | |
1683 ## Test a general replacement | |
1684 %!assert(regexprep("a[b]c{d}e-f=g", "[^A-Za-z0-9_]", "_"), "a_b_c_d_e_f_g"); | |
1685 | |
1686 ## Make sure it works at the beginning and end | |
1687 %!assert(regexprep("a[b]c{d}e-f=g", "a", "_"), "_[b]c{d}e-f=g"); | |
1688 %!assert(regexprep("a[b]c{d}e-f=g", "g", "_"), "a[b]c{d}e-f=_"); | |
1689 | |
1690 ## Options | |
1691 %!assert(regexprep("a[b]c{d}e-f=g", "[^A-Za-z0-9_]", "_", "once"), "a_b]c{d}e-f=g"); | |
1692 %!assert(regexprep("a[b]c{d}e-f=g", "[^A-Z0-9_]", "_", "ignorecase"), "a_b_c_d_e_f_g"); | |
1693 | |
1694 ## Option combinations | |
1695 %!assert(regexprep("a[b]c{d}e-f=g", "[^A-Z0-9_]", "_", "once", "ignorecase"), "a_b]c{d}e-f=g"); | |
1696 | |
1697 ## End conditions on replacement | |
1698 %!assert(regexprep("abc","(b)",".$1"),"a.bc"); | |
1699 %!assert(regexprep("abc","(b)","$1"),"abc"); | |
1700 %!assert(regexprep("abc","(b)","$1."),"ab.c"); | |
1701 %!assert(regexprep("abc","(b)","$1.."),"ab..c"); | |
1702 | |
6361 | 1703 ## Test cell array arguments |
6503 | 1704 %!assert(regexprep("abc",{"b","a"},"?"),{"??c"}) |
6361 | 1705 %!assert(regexprep({"abc","cba"},"b","?"),{"a?c","c?a"}) |
6503 | 1706 %!assert(regexprep({"abc","cba"},{"b","a"},{"?","!"}),{"!?c","c?!"}) |
6361 | 1707 |
8093
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
1708 # Nasty lookbehind expression |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
1709 %!assert(regexprep('x^(-1)+y(-1)+z(-1)=0','(?<=[a-z]+)\(\-[1-9]*\)','_minus1'),'x^(-1)+y_minus1+z_minus1=0') |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
1710 |
5785 | 1711 */ |
1712 | |
5582 | 1713 /* |
1714 ;;; Local Variables: *** | |
1715 ;;; mode: C++ *** | |
1716 ;;; End: *** | |
1717 */ |