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