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