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