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