5582
|
1 /* |
|
2 |
|
3 Copyright (C) 2005 David Bateman |
|
4 Copyright (C) 2002-2005 Paul Kienzle |
|
5 |
|
6 Octave is free software; you can redistribute it and/or modify it |
|
7 under the terms of the GNU General Public License as published by the |
|
8 Free Software Foundation; either version 2, or (at your option) any |
|
9 later version. |
|
10 |
|
11 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 for more details. |
|
15 |
|
16 You should have received a copy of the GNU General Public License |
|
17 along with this program; see the file COPYING. If not, write to the |
|
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
|
19 Boston, MA 02110-1301, USA. |
|
20 |
|
21 */ |
|
22 |
5775
|
23 // FIXME |
5582
|
24 // regexprep should be written as an m-file based on regexp |
|
25 |
|
26 #ifdef HAVE_CONFIG_H |
|
27 #include <config.h> |
|
28 #endif |
|
29 |
5773
|
30 #include <algorithm> |
5765
|
31 #include <sstream> |
|
32 |
5582
|
33 #include "defun-dld.h" |
|
34 #include "error.h" |
|
35 #include "gripes.h" |
|
36 #include "oct-obj.h" |
|
37 #include "utils.h" |
|
38 |
|
39 #include "Cell.h" |
|
40 #include "oct-map.h" |
|
41 #include "str-vec.h" |
|
42 |
|
43 #ifdef HAVE_PCRE |
|
44 #include <pcre.h> |
|
45 #else |
|
46 #ifdef HAVE_REGEX |
|
47 #ifdef __MINGW32__ |
|
48 #define __restrict |
|
49 #endif |
|
50 #include <regex.h> |
|
51 #endif |
|
52 #endif |
|
53 |
|
54 static octave_value_list |
|
55 octregexp (const octave_value_list &args, int nargout, const std::string &nm, |
|
56 bool case_insensitive) |
|
57 { |
|
58 octave_value_list retval; |
|
59 #if defined (HAVE_REGEX) || defined (HAVE_PCRE) |
|
60 int nargin = args.length(); |
|
61 int nopts = nargin - 2; |
|
62 bool once = false; |
5779
|
63 bool lineanchors = false; |
|
64 bool dotexceptnewline = false; |
|
65 bool freespacing = false; |
5582
|
66 |
|
67 if (nargin < 2) |
|
68 { |
|
69 print_usage(nm); |
|
70 return retval; |
|
71 } |
|
72 |
|
73 std::string buffer = args(0).string_value (); |
|
74 if (error_state) |
|
75 { |
|
76 gripe_wrong_type_arg (nm.c_str(), args(0)); |
|
77 return retval; |
|
78 } |
|
79 |
|
80 std::string pattern = args(1).string_value (); |
|
81 if (error_state) |
|
82 { |
|
83 gripe_wrong_type_arg (nm.c_str(), args(1)); |
|
84 return retval; |
|
85 } |
|
86 |
|
87 for (int i = 2; i < nargin; i++) |
|
88 { |
|
89 std::string str = args(i).string_value(); |
|
90 if (error_state) |
|
91 { |
|
92 error ("%s: optional arguments must be strings", nm.c_str()); |
|
93 break; |
|
94 } |
|
95 std::transform (str.begin (), str.end (), str.begin (), tolower); |
|
96 if (str.find("once", 0) == 0) |
|
97 { |
|
98 once = true; |
|
99 nopts--; |
|
100 } |
5779
|
101 #if HAVE_PCRE |
|
102 // Only accept these options with pcre |
|
103 else if (str.find("dotall", 0) == 0) |
|
104 { |
|
105 dotexceptnewline = false; |
|
106 nopts--; |
|
107 } |
|
108 else if (str.find("dotexceptnewline", 0) == 0) |
|
109 { |
|
110 dotexceptnewline = true; |
|
111 nopts--; |
|
112 } |
|
113 else if (str.find("stringanchors", 0) == 0) |
|
114 { |
|
115 lineanchors = false; |
|
116 nopts--; |
|
117 } |
|
118 else if (str.find("lineanchors", 0) == 0) |
|
119 { |
|
120 lineanchors = true; |
|
121 nopts--; |
|
122 } |
|
123 else if (str.find("matchcase", 0) == 0) |
|
124 { |
|
125 case_insensitive = false; |
|
126 nopts--; |
|
127 } |
|
128 else if (str.find("ignorecase", 0) == 0) |
|
129 { |
|
130 case_insensitive = true; |
|
131 nopts--; |
|
132 } |
|
133 else if (str.find("freespacing", 0) == 0) |
|
134 { |
|
135 freespacing = true; |
|
136 nopts--; |
|
137 } |
|
138 else if (str.find("literalspacing", 0) == 0) |
|
139 { |
|
140 freespacing = false; |
|
141 nopts--; |
|
142 } |
5582
|
143 else if (str.find("start", 0) && str.find("end", 0) && |
|
144 str.find("tokenextents", 0) && str.find("match", 0) && |
|
145 str.find("tokens", 0) && str.find("names", 0)) |
|
146 error ("%s: unrecognized option", nm.c_str()); |
|
147 #else |
5779
|
148 else if (str.find("names", 0) == 0 || |
|
149 str.find("dotexceptnewline", 0) == 0 || |
|
150 str.find("lineanchors", 0) == 0 || |
|
151 str.find("freespacing", 0) == 0) |
5582
|
152 error ("%s: named tokens not implemented in this version", nm.c_str()); |
|
153 else if (str.find("start", 0) && str.find("end", 0) && |
|
154 str.find("tokenextents", 0) && str.find("match", 0) && |
|
155 str.find("tokens", 0)) |
|
156 error ("%s: unrecognized option", nm.c_str()); |
|
157 #endif |
|
158 } |
|
159 |
|
160 if (!error_state) |
|
161 { |
|
162 Octave_map nmap; |
|
163 Cell t, m, te; |
|
164 NDArray s, e; |
|
165 |
|
166 // named tokens "(?<name>...)" are only treated with PCRE not regex. |
|
167 #if HAVE_PCRE |
|
168 // The syntax of named tokens in pcre is "(?P<name>...)" while we need |
|
169 // a syntax "(?<name>...)", so fix that here. Also an expression like |
|
170 // "(?<first>\w+)\s+(?<last>\w+)|(?<last>\w+),\s+(?<first>\w+)" should |
|
171 // be perfectly legal, while pcre does not allow the same named token |
5619
|
172 // name on both sides of the alternative. Also fix that here by replacing |
|
173 // name tokens by dummy names, and dealing with the dummy names later. |
5582
|
174 |
5619
|
175 size_t pos = 0; |
|
176 size_t new_pos; |
|
177 string_vector named; |
|
178 int nnames = 0; |
|
179 int inames = 0; |
5765
|
180 std::ostringstream buf; |
5619
|
181 Array<int> named_idx; |
5582
|
182 |
5779
|
183 // Add mode flags |
5619
|
184 while ((new_pos = pattern.find ("(?<",pos)) != NPOS) |
|
185 { |
|
186 size_t tmp_pos = pattern.find_first_of ('>',new_pos); |
5582
|
187 |
5619
|
188 if (tmp_pos == NPOS) |
|
189 { |
|
190 error ("syntax error in pattern"); |
|
191 break; |
|
192 } |
|
193 |
|
194 std::string tmp_name = pattern.substr(new_pos+3,tmp_pos-new_pos-3); |
|
195 bool found = false; |
|
196 |
|
197 for (int i = 0; i < nnames; i++) |
|
198 if (named(i) == tmp_name) |
|
199 { |
|
200 named_idx.resize(inames+1); |
|
201 named_idx(inames) = i; |
|
202 found = true; |
|
203 break; |
|
204 } |
|
205 if (! found) |
|
206 { |
|
207 named_idx.resize(inames+1); |
|
208 named_idx(inames) = nnames; |
|
209 named.append(tmp_name); |
|
210 nnames++; |
|
211 } |
|
212 |
|
213 if (new_pos - pos > 0) |
|
214 buf << pattern.substr(pos,new_pos-pos); |
|
215 if (inames < 10) |
|
216 buf << "(?P<n00" << inames++; |
|
217 else if (inames < 100) |
|
218 buf << "(?P<n0" << inames++; |
|
219 else |
|
220 buf << "(?P<n" << inames++; |
|
221 pos = tmp_pos; |
|
222 } |
|
223 |
5765
|
224 buf << pattern.substr(pos); |
5619
|
225 |
|
226 if (error_state) |
|
227 return retval; |
5582
|
228 |
|
229 // Compile expression |
|
230 pcre *re; |
|
231 const char *err; |
|
232 int erroffset; |
5765
|
233 std::string buf_str = buf.str (); |
|
234 re = pcre_compile (buf_str.c_str (), |
5779
|
235 (case_insensitive ? PCRE_CASELESS : 0) | |
|
236 (dotexceptnewline ? 0 : PCRE_DOTALL) | |
|
237 (lineanchors ? PCRE_MULTILINE : 0) | |
|
238 (freespacing ? PCRE_EXTENDED : 0), |
5765
|
239 &err, &erroffset, NULL); |
5582
|
240 |
|
241 if (re == NULL) { |
|
242 error("%s: %s at position %d of expression", nm.c_str(), |
|
243 err, erroffset); |
|
244 return retval; |
|
245 } |
|
246 |
|
247 int subpatterns; |
|
248 int namecount; |
|
249 int nameentrysize; |
|
250 char *nametable; |
|
251 int idx = 0; |
|
252 int sz = 0; |
|
253 |
|
254 pcre_fullinfo(re, NULL, PCRE_INFO_CAPTURECOUNT, &subpatterns); |
|
255 pcre_fullinfo(re, NULL, PCRE_INFO_NAMECOUNT, &namecount); |
|
256 pcre_fullinfo(re, NULL, PCRE_INFO_NAMEENTRYSIZE, &nameentrysize); |
|
257 pcre_fullinfo(re, NULL, PCRE_INFO_NAMETABLE, &nametable); |
|
258 |
|
259 OCTAVE_LOCAL_BUFFER(int, ovector, (subpatterns+1)*3); |
|
260 OCTAVE_LOCAL_BUFFER(int, nidx, namecount); |
|
261 |
|
262 for (int i = 0; i < namecount; i++) |
|
263 { |
|
264 // Index of subpattern in first two bytes MSB first of name. |
5619
|
265 // Extract index. |
5779
|
266 nidx[i] = (static_cast<int>(nametable[i*nameentrysize])) << 8 | |
|
267 static_cast<int>(nametable[i*nameentrysize+1]); |
5582
|
268 } |
|
269 |
5619
|
270 Cell named_tokens(dim_vector(nnames,1)); |
5582
|
271 |
|
272 while(true) |
|
273 { |
|
274 int matches = pcre_exec(re, NULL, buffer.c_str(), |
|
275 buffer.length(), idx, |
|
276 (idx ? PCRE_NOTBOL : 0), |
|
277 ovector, (subpatterns+1)*3); |
|
278 |
|
279 if (matches < 0 && matches != PCRE_ERROR_NOMATCH) |
|
280 { |
|
281 error ("%s: internal error calling pcre_exec", nm.c_str()); |
|
282 pcre_free(re); |
|
283 return retval; |
|
284 } |
|
285 else if (matches == PCRE_ERROR_NOMATCH) |
|
286 break; |
5779
|
287 else if (ovector[1] <= ovector[0]) |
|
288 break; |
5582
|
289 else |
|
290 { |
5779
|
291 // FIXME Should collect arguments in a linked structure and |
|
292 // resize and assign the return value a single time to make |
|
293 // this function O(n) rather than O(n^2) as it currently is. |
5619
|
294 int pos_match = 0; |
5582
|
295 s.resize (dim_vector(1, sz+1)); |
|
296 s(sz) = double (ovector[0]+1); |
|
297 e.resize (dim_vector(1, sz+1)); |
|
298 e(sz) = double (ovector[1]); |
|
299 te.resize(dim_vector(1, sz+1)); |
|
300 Matrix mat_te(matches-1,2); |
|
301 for (int i = 1; i < matches; i++) |
|
302 { |
5619
|
303 if (ovector[2*i] >= 0 && ovector[2*i+1] > 0) |
|
304 { |
|
305 mat_te(pos_match,0) = double (ovector[2*i]+1); |
|
306 mat_te(pos_match++,1) = double (ovector[2*i+1]); |
|
307 } |
5582
|
308 } |
5619
|
309 mat_te.resize(pos_match,2); |
5582
|
310 te(sz) = mat_te; |
|
311 |
|
312 const char **listptr; |
|
313 int status = pcre_get_substring_list(buffer.c_str(), ovector, |
|
314 matches, &listptr); |
|
315 |
|
316 if (status == PCRE_ERROR_NOMEMORY) { |
|
317 error("%s: cannot allocate memory in pcre_get_substring_list", |
|
318 nm.c_str()); |
|
319 pcre_free(re); |
|
320 return retval; |
|
321 } |
|
322 |
|
323 m.resize (dim_vector(1, sz+1)); |
|
324 m(sz) = std::string(*listptr); |
|
325 |
|
326 t.resize (dim_vector(1, sz+1)); |
5619
|
327 Cell cell_t (dim_vector(1,pos_match)); |
|
328 pos_match = 0; |
5582
|
329 for (int i = 1; i < matches; i++) |
5619
|
330 if (ovector[2*i] >= 0 && ovector[2*i+1] > 0) |
|
331 cell_t(pos_match++) = std::string(*(listptr+i)); |
5582
|
332 t(sz) = cell_t; |
|
333 |
5619
|
334 if (namecount > 0) |
|
335 for (int i = 1; i < matches; i++) |
|
336 { |
|
337 if (ovector[2*i] >= 0 && ovector[2*i+1] > 0) |
|
338 { |
|
339 if (sz == 0) |
|
340 { |
|
341 named_tokens(named_idx(i-1)) = |
|
342 std::string(*(listptr+nidx[i-1])); |
|
343 } |
|
344 else |
|
345 { |
|
346 Cell tmp = named_tokens(named_idx(i-1)); |
|
347 tmp.resize(dim_vector(1,sz+1)); |
|
348 tmp(sz) = std::string(*(listptr+nidx[i-1])); |
|
349 named_tokens(named_idx(i-1)) = tmp; |
|
350 } |
|
351 } |
|
352 } |
5582
|
353 |
|
354 pcre_free_substring_list(listptr); |
|
355 |
|
356 if (once) |
|
357 break; |
|
358 |
|
359 idx = ovector[1]; |
|
360 sz++; |
|
361 } |
|
362 } |
|
363 |
5619
|
364 for (int i = 0; i < nnames; i++) |
|
365 nmap.assign (named(i), named_tokens(i)); |
5582
|
366 |
|
367 pcre_free(re); |
|
368 #else |
|
369 regex_t compiled; |
|
370 int err=regcomp(&compiled, pattern.c_str(), REG_EXTENDED | |
|
371 (case_insensitive ? REG_ICASE : 0)); |
|
372 if (err) |
|
373 { |
|
374 int len = regerror(err, &compiled, NULL, 0); |
5760
|
375 OCTAVE_LOCAL_BUFFER (char, errmsg, len); |
|
376 regerror(err, &compiled, errmsg, len); |
|
377 error("%s: %s in pattern (%s)", nm.c_str(), errmsg, |
|
378 pattern.c_str()); |
5582
|
379 regfree(&compiled); |
|
380 return retval; |
|
381 } |
|
382 |
|
383 int subexpr = 1; |
|
384 int idx = 0; |
|
385 int sz = 0; |
|
386 for (unsigned int i=0; i < pattern.length(); i++) |
|
387 subexpr += ( pattern[i] == '(' ? 1 : 0 ); |
|
388 OCTAVE_LOCAL_BUFFER (regmatch_t, match, subexpr ); |
|
389 |
|
390 while(true) |
|
391 { |
|
392 if (regexec(&compiled, buffer.c_str() + idx, subexpr, |
|
393 match, (idx ? REG_NOTBOL : 0)) == 0) |
|
394 { |
|
395 // Count actual matches |
|
396 int matches = 0; |
|
397 while (matches < subexpr && match[matches].rm_so >= 0) |
|
398 matches++; |
|
399 |
|
400 s.resize (dim_vector(1, sz+1)); |
|
401 s(sz) = double (match[0].rm_so+1+idx); |
|
402 e.resize (dim_vector(1, sz+1)); |
|
403 e(sz) = double (match[0].rm_eo+idx); |
|
404 te.resize(dim_vector(1, sz+1)); |
|
405 Matrix mat_te(matches-1,2); |
|
406 for (int i = 1; i < matches; i++) |
|
407 { |
|
408 mat_te(i-1,0) = double (match[i].rm_so+1+idx); |
|
409 mat_te(i-1,1) = double (match[i].rm_eo+idx); |
|
410 } |
|
411 te(sz) = mat_te; |
|
412 |
|
413 m.resize (dim_vector(1, sz+1)); |
|
414 m(sz) = buffer.substr (match[0].rm_so+idx, |
|
415 match[0].rm_eo-match[0].rm_so); |
|
416 |
|
417 t.resize (dim_vector(1, sz+1)); |
|
418 Cell cell_t (dim_vector(1,matches-1)); |
|
419 for (int i = 1; i < matches; i++) |
|
420 cell_t(i-1) = buffer.substr (match[i].rm_so+idx, |
|
421 match[i].rm_eo-match[i].rm_so); |
|
422 t(sz) = cell_t; |
|
423 |
|
424 idx += match[0].rm_eo; |
|
425 sz++; |
|
426 |
|
427 if (once) |
|
428 break; |
|
429 } |
|
430 else |
|
431 break; |
|
432 } |
|
433 regfree(&compiled); |
|
434 #endif |
|
435 |
|
436 retval(5) = nmap; |
|
437 retval(4) = t; |
|
438 retval(3) = m; |
|
439 retval(2) = te; |
|
440 retval(1) = e; |
|
441 retval(0) = s; |
|
442 |
|
443 // Alter the order of the output arguments |
|
444 if (nopts > 0) |
|
445 { |
|
446 int n = 0; |
|
447 octave_value_list new_retval; |
|
448 new_retval.resize(nargout); |
|
449 |
|
450 OCTAVE_LOCAL_BUFFER (int, arg_used, 6); |
|
451 for (int i = 0; i < 6; i++) |
|
452 arg_used[i] = false; |
|
453 |
|
454 for (int i = 2; i < nargin; i++) |
|
455 { |
|
456 int k = 0; |
|
457 std::string str = args(i).string_value(); |
|
458 std::transform (str.begin (), str.end (), str.begin (), tolower); |
5779
|
459 if (str.find("once", 0) == 0 |
|
460 #if HAVE_PCRE |
|
461 || str.find("stringanchors", 0) == 0 |
|
462 || str.find("lineanchors", 0) == 0 |
|
463 || str.find("matchcase", 0) == 0 |
|
464 || str.find("ignorecase", 0) == 0 |
|
465 || str.find("dotall", 0) == 0 |
|
466 || str.find("dotexceptnewline", 0) == 0 |
|
467 || str.find("literalspacing", 0) == 0 |
|
468 || str.find("freespacing", 0) == 0 |
|
469 #endif |
|
470 ) |
5582
|
471 continue; |
|
472 else if (str.find("start", 0) == 0) |
|
473 k = 0; |
|
474 else if (str.find("end", 0) == 0) |
|
475 k = 1; |
|
476 else if (str.find("tokenextents", 0) == 0) |
|
477 k = 2; |
|
478 else if (str.find("match", 0) == 0) |
|
479 k = 3; |
|
480 else if (str.find("tokens", 0) == 0) |
|
481 k = 4; |
|
482 else if (str.find("names", 0) == 0) |
|
483 k = 5; |
|
484 |
|
485 new_retval(n++) = retval(k); |
|
486 arg_used[k] = true; |
|
487 |
|
488 if (n == nargout) |
|
489 break; |
|
490 } |
|
491 |
|
492 // Fill in the rest of the arguments |
|
493 if (n < nargout) |
|
494 { |
|
495 for (int i = 0; i < 6; i++) |
|
496 { |
|
497 if (! arg_used[i]) |
|
498 new_retval(n++) = retval(i); |
|
499 } |
|
500 } |
|
501 |
|
502 retval = new_retval; |
|
503 } |
|
504 } |
|
505 |
|
506 #else |
5779
|
507 error ("%s: not available in this version of Octave", nm.c_str()); |
5582
|
508 #endif |
|
509 return retval; |
|
510 } |
|
511 |
|
512 DEFUN_DLD (regexp, args, nargout, |
|
513 "-*- texinfo -*-\n\ |
|
514 @deftypefn {Loadable Function} {[@var{s}, @var{e}, @var{te}, @var{m}, @var{t}, @var{nm}] =} regexp (@var{str}, @var{pat})\n\ |
|
515 @deftypefnx {Loadable Function} {[@dots{}] =} regexp (@var{str}, @var{pat}, @var{opts}, @dots{})\n\ |
|
516 \n\ |
|
517 Regular expression string matching. Matches @var{pat} in @var{str} and\n\ |
|
518 returns the position and matching substrings or empty values if there are\n\ |
|
519 none.\n\ |
|
520 \n\ |
|
521 The matched pattern @var{pat} can include any of the standard regex\n\ |
|
522 operators, including:\n\ |
|
523 \n\ |
|
524 @table @code\n\ |
|
525 @item .\n\ |
|
526 Match any character\n\ |
|
527 @item * + ? @{@}\n\ |
|
528 Repetition operators, representing\n\ |
|
529 @table @code\n\ |
|
530 @item *\n\ |
|
531 Match zero or more times\n\ |
|
532 @item +\n\ |
|
533 Match one or more times\n\ |
|
534 @item ?\n\ |
|
535 Match zero or one times\n\ |
|
536 @item @{@}\n\ |
|
537 Match range operator, which is of the form @code{@{@var{n}@}} to match exactly\n\ |
|
538 @var{n} times, @code{@{@var{m},@}} to match @var{m} or more times,\n\ |
|
539 @code{@{@var{m},@var{n}@}} to match between @var{m} and @var{n} times.\n\ |
|
540 @end table\n\ |
|
541 @item [@dots{}] [^@dots{}]\n\ |
|
542 List operators, where for example @code{[ab]c} matches @code{ac} and @code{bc}\n\ |
|
543 @item ()\n\ |
|
544 Grouping operator\n\ |
|
545 @item |\n\ |
|
546 Alternation operator. Match one of a choice of regular expressions. The\n\ |
|
547 alternatives must be delimited by the grouoing operator @code{()} above\n\ |
|
548 @item ^ $\n\ |
|
549 Anchoring operator. @code{^} matches the start of the string @var{str} and\n\ |
|
550 @code{$} the end\n\ |
|
551 @end table\n\ |
|
552 \n\ |
|
553 In addition the following escaped characters have special meaning. It should\n\ |
|
554 be noted that it is recommended to quote @var{pat} in single quotes rather\n\ |
|
555 than double quotes, to avoid the escape sequences being interpreted by octave\n\ |
|
556 before being passed to @code{regexp}.\n\ |
|
557 \n\ |
|
558 @table @code\n\ |
|
559 @item \\b\n\ |
|
560 Match a word boundary\n\ |
|
561 @item \\B\n\ |
|
562 Match within a word\n\ |
|
563 @item \\w\n\ |
|
564 Matches any word character\n\ |
|
565 @item \\W\n\ |
|
566 Matches any non word character\n\ |
|
567 @item \\<\n\ |
|
568 Matches the beginning of a word\n\ |
|
569 @item \\>\n\ |
|
570 Matches the end of a word\n\ |
|
571 @item \\s\n\ |
|
572 Matches any whitespace character\n\ |
|
573 @item \\S\n\ |
|
574 Matches any non whitespace character\n\ |
|
575 @item \\d\n\ |
|
576 Matches any digit\n\ |
|
577 @item \\D\n\ |
|
578 Matches any non-digit\n\ |
|
579 @end table\n\ |
|
580 \n\ |
|
581 The outputs of @code{regexp} by default are in the order as given below\n\ |
|
582 \n\ |
|
583 @table @asis\n\ |
|
584 @item @var{s}\n\ |
|
585 The start indices of each of the matching substrings\n\ |
|
586 \n\ |
|
587 @item @var{e}\n\ |
|
588 The end indices of each matching substring\n\ |
|
589 \n\ |
|
590 @item @var{te}\n\ |
|
591 The extents of each of the matched token surrounded by @code{(@dots{})} in\n\ |
|
592 @var{pat}.\n\ |
|
593 \n\ |
|
594 @item @var{m}\n\ |
|
595 A cell array of the text of each match.\n\ |
|
596 \n\ |
|
597 @item @var{t}\n\ |
|
598 A cell array of the text of each token matched.\n\ |
|
599 \n\ |
|
600 @item @var{nm}\n\ |
|
601 A structure containing the text of each matched named token, with the name\n\ |
|
602 being used as the fieldname. A named token is denoted as\n\ |
|
603 @code{(?<name>@dots{})}\n\ |
|
604 @end table\n\ |
|
605 \n\ |
|
606 Particular output arguments or the order of the output arguments can be\n\ |
|
607 selected by additional @var{opts} arguments. These are strings and the\n\ |
|
608 correspondence between the output arguments and the optional argument\n\ |
|
609 are\n\ |
|
610 \n\ |
|
611 @multitable @columnfractions 0.2 0.3 0.3 0.2\n\ |
|
612 @item @tab 'start' @tab @var{s} @tab\n\ |
|
613 @item @tab 'end' @tab @var{e} @tab\n\ |
|
614 @item @tab 'tokenExtents' @tab @var{te} @tab\n\ |
|
615 @item @tab 'match' @tab @var{m} @tab\n\ |
|
616 @item @tab 'tokens' @tab @var{t} @tab\n\ |
|
617 @item @tab 'names' @tab @var{nm} @tab\n\ |
|
618 @end multitable\n\ |
|
619 \n\ |
|
620 A further optional argument is 'once', that limits the number of returned\n\ |
5779
|
621 matches to the first match. Additional arguments are\n\ |
|
622 \n\ |
|
623 @table @asis\n\ |
|
624 @item matchcase\n\ |
|
625 Make the matching case sensitive.\n\ |
|
626 @item ignorecase\n\ |
|
627 Make the matching case insensitive.\n\ |
|
628 @item stringanchors\n\ |
|
629 Match the anchor characters at the beginning and end of the string.\n\ |
|
630 @item lineanchors\n\ |
|
631 Match the anchor characters at the beginning and end of the line.\n\ |
|
632 @item dotall\n\ |
|
633 The character @code{.} matches the newline character.\n\ |
|
634 @item dotexceptnewline\n\ |
|
635 The character @code{.} matches all but the newline character.\n\ |
|
636 @item freespacing\n\ |
|
637 The pattern can include arbitrary whitespace and comments starting with\n\ |
|
638 @code{#}.\n\ |
|
639 @item literalspacing\n\ |
|
640 The pattern is taken literally.\n\ |
|
641 @end table\n\ |
5582
|
642 @end deftypefn") |
|
643 { |
|
644 return octregexp (args, nargout, "regexp", false); |
|
645 } |
|
646 |
|
647 /* |
|
648 |
|
649 ## seg-fault test |
|
650 %!assert(regexp("abcde","."),[1,2,3,4,5]) |
|
651 |
|
652 ## Check that anchoring of pattern works correctly |
|
653 %!assert(regexp('abcabc','^abc'),1); |
|
654 %!assert(regexp('abcabc','abc$'),4); |
|
655 %!assert(regexp('abcabc','^abc$'),[]); |
|
656 |
|
657 %!test |
|
658 %! [s, e, te, m, t] = regexp(' No Match ', 'f(.*)uck'); |
|
659 %! assert (s,[]) |
|
660 %! assert (e,[]) |
|
661 %! assert (te,{}) |
|
662 %! assert (m, {}) |
|
663 %! assert (t, {}) |
|
664 |
|
665 %!test |
|
666 %! [s, e, te, m, t] = regexp(' FiRetrUck ', 'f(.*)uck'); |
|
667 %! assert (s,[]) |
|
668 %! assert (e,[]) |
|
669 %! assert (te,{}) |
|
670 %! assert (m, {}) |
|
671 %! assert (t, {}) |
|
672 |
|
673 %!test |
|
674 %! [s, e, te, m, t] = regexp(' firetruck ', 'f(.*)uck'); |
|
675 %! assert (s,2) |
|
676 %! assert (e,10) |
|
677 %! assert (te{1},[3,7]) |
|
678 %! assert (m{1}, 'firetruck') |
|
679 %! assert (t{1}{1}, 'iretr') |
|
680 |
|
681 %!test |
|
682 %! [s, e, te, m, t] = regexp('short test string','\w*r\w*'); |
|
683 %! assert (s,[1,12]) |
|
684 %! assert (e,[5,17]) |
|
685 %! assert (size(te), [1,2]) |
|
686 %! assert (isempty(te{1})) |
|
687 %! assert (isempty(te{2})) |
|
688 %! assert (m{1},'short') |
|
689 %! assert (m{2},'string') |
|
690 %! assert (size(t), [1,2]) |
|
691 %! assert (isempty(t{1})) |
|
692 %! assert (isempty(t{2})) |
|
693 |
|
694 %!test |
|
695 %! [s, e, te, m, t] = regexp('short test string','\w*r\w*','once'); |
|
696 %! assert (s,1) |
|
697 %! assert (e,5) |
|
698 %! assert (size(te), [1,1]) |
|
699 %! assert (isempty(te{1})) |
|
700 %! assert (m{1},'short') |
|
701 %! ## Matlab gives [1,0] here but that seems wrong. |
|
702 %! assert (size(t), [1,1]) |
|
703 |
|
704 %!test |
|
705 %! [m, te, e, s, t] = regexp('short test string','\w*r\w*','once', 'match', 'tokenExtents', 'end', 'start', 'tokens'); |
|
706 %! assert (s,1) |
|
707 %! assert (e,5) |
|
708 %! assert (size(te), [1,1]) |
|
709 %! assert (isempty(te{1})) |
|
710 %! assert (m{1},'short') |
|
711 %! ## Matlab gives [1,0] here but that seems wrong. |
|
712 %! assert (size(t), [1,1]) |
|
713 |
5619
|
714 %!test |
5582
|
715 %! ## This test is expected to fail if PCRE is not installed |
5620
|
716 %! if (!isempty(findstr(octave_config_info ("DEFS"),"HAVE_PCRE"))) |
5619
|
717 %! [s, e, te, m, t, nm] = regexp('short test string','(?<word1>\w*t)\s*(?<word2>\w*t)'); |
|
718 %! assert (s,1) |
|
719 %! assert (e,10) |
|
720 %! assert (size(te), [1,1]) |
|
721 %! assert (te{1}, [1 5; 7, 10]) |
|
722 %! assert (m{1},'short test') |
|
723 %! assert (size(t),[1,1]) |
|
724 %! assert (t{1}{1},'short') |
|
725 %! assert (t{1}{2},'test') |
|
726 %! assert (size(nm), [1,1]) |
|
727 %! assert (!isempty(fieldnames(nm))) |
|
728 %! assert (sort(fieldnames(nm)),{'word1';'word2'}) |
|
729 %! assert (nm.word1,'short') |
|
730 %! assert (nm.word2,'test') |
|
731 %! endif |
5582
|
732 |
5619
|
733 %!test |
5582
|
734 %! ## This test is expected to fail if PCRE is not installed |
5620
|
735 %! if (!isempty(findstr(octave_config_info ("DEFS"),"HAVE_PCRE"))) |
5619
|
736 %! [nm, m, te, e, s, t] = regexp('short test string','(?<word1>\w*t)\s*(?<word2>\w*t)', 'names', 'match', 'tokenExtents', 'end', 'start', 'tokens'); |
|
737 %! assert (s,1) |
|
738 %! assert (e,10) |
|
739 %! assert (size(te), [1,1]) |
|
740 %! assert (te{1}, [1 5; 7, 10]) |
|
741 %! assert (m{1},'short test') |
|
742 %! assert (size(t),[1,1]) |
|
743 %! assert (t{1}{1},'short') |
|
744 %! assert (t{1}{2},'test') |
|
745 %! assert (size(nm), [1,1]) |
|
746 %! assert (!isempty(fieldnames(nm))) |
|
747 %! assert (sort(fieldnames(nm)),{'word1';'word2'}) |
|
748 %! assert (nm.word1,'short') |
|
749 %! assert (nm.word2,'test') |
|
750 %! endif |
|
751 |
|
752 %!test |
|
753 %! ## This test is expected to fail if PCRE is not installed |
5620
|
754 %! if (!isempty(findstr(octave_config_info ("DEFS"),"HAVE_PCRE"))) |
5619
|
755 %! [t, nm] = regexp("John Davis\nRogers, James",'(?<first>\w+)\s+(?<last>\w+)|(?<last>\w+),\s+(?<first>\w+)','tokens','names'); |
|
756 %! assert (size(t), [1,2]); |
|
757 %! assert (t{1}{1},'John'); |
|
758 %! assert (t{1}{2},'Davis'); |
|
759 %! assert (t{2}{1},'Rogers'); |
|
760 %! assert (t{2}{2},'James'); |
|
761 %! assert (size(nm), [1,1]); |
|
762 %! assert (nm.first{1},'John'); |
|
763 %! assert (nm.first{2},'James'); |
|
764 %! assert (nm.last{1},'Davis'); |
|
765 %! assert (nm.last{2},'Rogers'); |
|
766 %! endif |
5582
|
767 |
5779
|
768 %!assert(regexp("abc\nabc",'.'),[1:7]) |
|
769 %!assert(regexp("abc\nabc",'.','dotall'),[1:7]) |
|
770 %!test |
|
771 %! if (!isempty(findstr(octave_config_info ("DEFS"),"HAVE_PCRE"))) |
|
772 %! assert(regexp("abc\nabc",'(?s).'),[1:7]) |
|
773 %! assert(regexp("abc\nabc",'.','dotexceptnewline'),[1,2,3,5,6,7]) |
|
774 %! assert(regexp("abc\nabc",'(?-s).'),[1,2,3,5,6,7]) |
|
775 %! endif |
|
776 |
|
777 %!assert(regexp("caseCaSe",'case'),1) |
|
778 %!assert(regexp("caseCaSe",'case',"matchcase"),1) |
|
779 %!assert(regexp("caseCaSe",'case',"ignorecase"),[1,5]) |
|
780 %!test |
|
781 %! if (!isempty(findstr(octave_config_info ("DEFS"),"HAVE_PCRE"))) |
|
782 %! assert(regexp("caseCaSe",'(?-i)case'),1) |
|
783 %! assert(regexp("caseCaSe",'(?i)case'),[1,5]) |
|
784 %! endif |
|
785 |
|
786 %!assert (regexp("abc\nabc",'c$'),7) |
|
787 %!assert (regexp("abc\nabc",'c$',"stringanchors"),7) |
|
788 %!test |
|
789 %! if (!isempty(findstr(octave_config_info ("DEFS"),"HAVE_PCRE"))) |
|
790 %! assert (regexp("abc\nabc",'(?-m)c$'),7) |
|
791 %! assert (regexp("abc\nabc",'c$',"lineanchors"),[3,7]) |
|
792 %! assert (regexp("abc\nabc",'(?m)c$'),[3,7]) |
|
793 %! endif |
|
794 |
|
795 %!assert (regexp("this word",'s w'),4) |
|
796 %!assert (regexp("this word",'s w','literalspacing'),4) |
|
797 %!test |
|
798 %! if (!isempty(findstr(octave_config_info ("DEFS"),"HAVE_PCRE"))) |
|
799 %! assert (regexp("this word",'(?-x)s w','literalspacing'),4) |
|
800 %! assert (regexp("this word",'s w','freespacing'),[]) |
|
801 %! assert (regexp("this word",'(?x)s w'),[]) |
|
802 %! endif |
|
803 |
5582
|
804 %!error regexp('string', 'tri', 'BadArg'); |
|
805 %!error regexp('string'); |
|
806 |
|
807 */ |
|
808 |
|
809 DEFUN_DLD(regexpi, args, nargout, |
|
810 "-*- texinfo -*-\n\ |
|
811 @deftypefn {Loadable Function} {[@var{s}, @var{e}, @var{te}, @var{m}, @var{t}, @var{nm}] =} regexpi (@var{str}, @var{pat})\n\ |
|
812 @deftypefnx {Loadable Function} {[@dots{}] =} regexpi (@var{str}, @var{pat}, @var{opts}, @dots{})\n\ |
|
813 \n\ |
|
814 Case insensitive regular expression string matching. Matches @var{pat} in\n\ |
|
815 @var{str} and returns the position and matching substrings or empty values\n\ |
|
816 if there are none. See @code{regexp} for more details\n\ |
|
817 @end deftypefn") |
|
818 { |
|
819 return octregexp (args, nargout, "regexp", true); |
|
820 } |
|
821 |
|
822 /* |
|
823 |
|
824 ## seg-fault test |
|
825 %!assert(regexpi("abcde","."),[1,2,3,4,5]) |
|
826 |
|
827 ## Check that anchoring of pattern works correctly |
|
828 %!assert(regexpi('abcabc','^abc'),1); |
|
829 %!assert(regexpi('abcabc','abc$'),4); |
|
830 %!assert(regexpi('abcabc','^abc$'),[]); |
|
831 |
|
832 %!test |
|
833 %! [s, e, te, m, t] = regexpi(' No Match ', 'f(.*)uck'); |
|
834 %! assert (s,[]) |
|
835 %! assert (e,[]) |
|
836 %! assert (te,{}) |
|
837 %! assert (m, {}) |
|
838 %! assert (t, {}) |
|
839 |
|
840 %!test |
|
841 %! [s, e, te, m, t] = regexpi(' FiRetrUck ', 'f(.*)uck'); |
|
842 %! assert (s,2) |
|
843 %! assert (e,10) |
|
844 %! assert (te{1},[3,7]) |
|
845 %! assert (m{1}, 'FiRetrUck') |
|
846 %! assert (t{1}{1}, 'iRetr') |
|
847 |
|
848 %!test |
|
849 %! [s, e, te, m, t] = regexpi(' firetruck ', 'f(.*)uck'); |
|
850 %! assert (s,2) |
|
851 %! assert (e,10) |
|
852 %! assert (te{1},[3,7]) |
|
853 %! assert (m{1}, 'firetruck') |
|
854 %! assert (t{1}{1}, 'iretr') |
|
855 |
|
856 %!test |
|
857 %! [s, e, te, m, t] = regexpi('ShoRt Test String','\w*r\w*'); |
|
858 %! assert (s,[1,12]) |
|
859 %! assert (e,[5,17]) |
|
860 %! assert (size(te), [1,2]) |
|
861 %! assert (isempty(te{1})) |
|
862 %! assert (isempty(te{2})) |
|
863 %! assert (m{1},'ShoRt') |
|
864 %! assert (m{2},'String') |
|
865 %! assert (size(t), [1,2]) |
|
866 %! assert (isempty(t{1})) |
|
867 %! assert (isempty(t{2})) |
|
868 |
|
869 %!test |
|
870 %! [s, e, te, m, t] = regexpi('ShoRt Test String','\w*r\w*','once'); |
|
871 %! assert (s,1) |
|
872 %! assert (e,5) |
|
873 %! assert (size(te), [1,1]) |
|
874 %! assert (isempty(te{1})) |
|
875 %! assert (m{1},'ShoRt') |
|
876 %! ## Matlab gives [1,0] here but that seems wrong. |
|
877 %! assert (size(t), [1,1]) |
|
878 |
|
879 %!test |
|
880 %! [m, te, e, s, t] = regexpi('ShoRt Test String','\w*r\w*','once', 'match', 'tokenExtents', 'end', 'start', 'tokens'); |
|
881 %! assert (s,1) |
|
882 %! assert (e,5) |
|
883 %! assert (size(te), [1,1]) |
|
884 %! assert (isempty(te{1})) |
|
885 %! assert (m{1},'ShoRt') |
|
886 %! ## Matlab gives [1,0] here but that seems wrong. |
|
887 %! assert (size(t), [1,1]) |
|
888 |
5619
|
889 %!test |
5582
|
890 %! ## This test is expected to fail if PCRE is not installed |
5620
|
891 %! if (!isempty(findstr(octave_config_info ("DEFS"),"HAVE_PCRE"))) |
5619
|
892 %! [s, e, te, m, t, nm] = regexpi('ShoRt Test String','(?<word1>\w*t)\s*(?<word2>\w*t)'); |
|
893 %! assert (s,1) |
|
894 %! assert (e,10) |
|
895 %! assert (size(te), [1,1]) |
|
896 %! assert (te{1}, [1 5; 7, 10]) |
|
897 %! assert (m{1},'ShoRt Test') |
|
898 %! assert (size(t),[1,1]) |
|
899 %! assert (t{1}{1},'ShoRt') |
|
900 %! assert (t{1}{2},'Test') |
|
901 %! assert (size(nm), [1,1]) |
|
902 %! assert (!isempty(fieldnames(nm))) |
|
903 %! assert (sort(fieldnames(nm)),{'word1';'word2'}) |
|
904 %! assert (nm.word1,'ShoRt') |
|
905 %! assert (nm.word2,'Test') |
|
906 %! endif |
5582
|
907 |
5619
|
908 %!test |
5582
|
909 %! ## This test is expected to fail if PCRE is not installed |
5620
|
910 %! if (!isempty(findstr(octave_config_info ("DEFS"),"HAVE_PCRE"))) |
5619
|
911 %! [nm, m, te, e, s, t] = regexpi('ShoRt Test String','(?<word1>\w*t)\s*(?<word2>\w*t)', 'names', 'match', 'tokenExtents', 'end', 'start', 'tokens'); |
|
912 %! assert (s,1) |
|
913 %! assert (e,10) |
|
914 %! assert (size(te), [1,1]) |
|
915 %! assert (te{1}, [1 5; 7, 10]) |
|
916 %! assert (m{1},'ShoRt Test') |
|
917 %! assert (size(t),[1,1]) |
|
918 %! assert (t{1}{1},'ShoRt') |
|
919 %! assert (t{1}{2},'Test') |
|
920 %! assert (size(nm), [1,1]) |
|
921 %! assert (!isempty(fieldnames(nm))) |
|
922 %! assert (sort(fieldnames(nm)),{'word1';'word2'}) |
|
923 %! assert (nm.word1,'ShoRt') |
|
924 %! assert (nm.word2,'Test') |
|
925 %! endif |
5582
|
926 |
5779
|
927 %!assert(regexpi("abc\nabc",'.'),[1:7]) |
|
928 %!assert(regexpi("abc\nabc",'.','dotall'),[1:7]) |
|
929 %!test |
|
930 %! if (!isempty(findstr(octave_config_info ("DEFS"),"HAVE_PCRE"))) |
|
931 %! assert(regexpi("abc\nabc",'(?s).'),[1:7]) |
|
932 %! assert(regexpi("abc\nabc",'.','dotexceptnewline'),[1,2,3,5,6,7]) |
|
933 %! assert(regexpi("abc\nabc",'(?-s).'),[1,2,3,5,6,7]) |
|
934 %! endif |
|
935 |
|
936 %!assert(regexpi("caseCaSe",'case'),[1,5]) |
|
937 %!assert(regexpi("caseCaSe",'case',"matchcase"),1) |
|
938 %!assert(regexpi("caseCaSe",'case',"ignorecase"),[1,5]) |
|
939 %!test |
|
940 %! if (!isempty(findstr(octave_config_info ("DEFS"),"HAVE_PCRE"))) |
|
941 %! assert(regexpi("caseCaSe",'(?-i)case'),1) |
|
942 %! assert(regexpi("caseCaSe",'(?i)case'),[1,5]) |
|
943 %! endif |
|
944 |
|
945 %!assert (regexpi("abc\nabc",'c$'),7) |
|
946 %!assert (regexpi("abc\nabc",'c$',"stringanchors"),7) |
|
947 %!test |
|
948 %! if (!isempty(findstr(octave_config_info ("DEFS"),"HAVE_PCRE"))) |
|
949 %! assert (regexpi("abc\nabc",'(?-m)c$'),7) |
|
950 %! assert (regexpi("abc\nabc",'c$',"lineanchors"),[3,7]) |
|
951 %! assert (regexpi("abc\nabc",'(?m)c$'),[3,7]) |
|
952 %! endif |
|
953 |
|
954 %!assert (regexpi("this word",'s w'),4) |
|
955 %!assert (regexpi("this word",'s w','literalspacing'),4) |
|
956 %!test |
|
957 %! if (!isempty(findstr(octave_config_info ("DEFS"),"HAVE_PCRE"))) |
|
958 %! assert (regexpi("this word",'(?-x)s w','literalspacing'),4) |
|
959 %! assert (regexpi("this word",'s w','freespacing'),[]) |
|
960 %! assert (regexpi("this word",'(?x)s w'),[]) |
|
961 %! endif |
|
962 |
5582
|
963 %!error regexpi('string', 'tri', 'BadArg'); |
|
964 %!error regexpi('string'); |
|
965 |
|
966 */ |
|
967 |
|
968 /* |
|
969 ;;; Local Variables: *** |
|
970 ;;; mode: C++ *** |
|
971 ;;; End: *** |
|
972 */ |