5582
|
1 /* |
|
2 |
7017
|
3 Copyright (C) 2005, 2006, 2007 David Bateman |
7016
|
4 Copyright (C) 2002, 2003, 2004, 2005 Paul Kienzle |
|
5 |
|
6 This file is part of Octave. |
5582
|
7 |
|
8 Octave is free software; you can redistribute it and/or modify it |
|
9 under the terms of the GNU General Public License as published by the |
7016
|
10 Free Software Foundation; either version 3 of the License, or (at your |
|
11 option) any later version. |
5582
|
12 |
|
13 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
16 for more details. |
|
17 |
|
18 You should have received a copy of the GNU General Public License |
7016
|
19 along with Octave; see the file COPYING. If not, see |
|
20 <http://www.gnu.org/licenses/>. |
5582
|
21 |
|
22 */ |
|
23 |
|
24 #ifdef HAVE_CONFIG_H |
|
25 #include <config.h> |
|
26 #endif |
|
27 |
5773
|
28 #include <algorithm> |
5765
|
29 #include <sstream> |
|
30 |
5582
|
31 #include "defun-dld.h" |
|
32 #include "error.h" |
|
33 #include "gripes.h" |
|
34 #include "oct-obj.h" |
|
35 #include "utils.h" |
|
36 |
|
37 #include "Cell.h" |
|
38 #include "oct-map.h" |
|
39 #include "str-vec.h" |
5785
|
40 #include "quit.h" |
|
41 #include "parse.h" |
5582
|
42 |
7173
|
43 #if defined (HAVE_PCRE) |
5582
|
44 #include <pcre.h> |
7117
|
45 #elif defined (HAVE_REGEX) |
|
46 #if defined (__MINGW32__) |
5582
|
47 #define __restrict |
|
48 #endif |
|
49 #include <regex.h> |
|
50 #endif |
|
51 |
5785
|
52 // The regexp is constructed as a linked list to avoid resizing the |
|
53 // return values in arrays at each new match. |
|
54 |
|
55 // FIXME don't bother collecting and composing return values the user |
|
56 // doesn't want. |
|
57 |
|
58 class regexp_elem |
5582
|
59 { |
5785
|
60 public: |
5787
|
61 regexp_elem (const string_vector& _named_token, const Cell& _t, |
|
62 const std::string& _m, const Matrix& _te, double _s, |
|
63 double _e) : |
5785
|
64 named_token (_named_token), t (_t), m (_m), te (_te), s (_s), e (_e) { } |
|
65 |
|
66 regexp_elem (const regexp_elem &a) : named_token (a.named_token), t (a.t), |
|
67 m (a.m), te (a.te), s (a.s), e (a.e) |
|
68 { } |
|
69 |
|
70 string_vector named_token; |
|
71 Cell t; |
|
72 std::string m; |
|
73 Matrix te; |
|
74 double s; |
|
75 double e; |
|
76 }; |
|
77 |
|
78 typedef std::list<regexp_elem>::const_iterator const_iterator; |
|
79 |
|
80 static int |
|
81 octregexp_list (const octave_value_list &args, const std::string &nm, |
|
82 bool case_insensitive, std::list<regexp_elem> &lst, |
|
83 string_vector &named, int &nopts) |
|
84 { |
|
85 int sz = 0; |
5582
|
86 #if defined (HAVE_REGEX) || defined (HAVE_PCRE) |
|
87 int nargin = args.length(); |
|
88 bool once = false; |
5779
|
89 bool lineanchors = false; |
|
90 bool dotexceptnewline = false; |
|
91 bool freespacing = false; |
5582
|
92 |
5785
|
93 nopts = nargin - 2; |
|
94 |
5582
|
95 std::string buffer = args(0).string_value (); |
|
96 if (error_state) |
|
97 { |
|
98 gripe_wrong_type_arg (nm.c_str(), args(0)); |
5785
|
99 return 0; |
5582
|
100 } |
|
101 |
|
102 std::string pattern = args(1).string_value (); |
|
103 if (error_state) |
|
104 { |
|
105 gripe_wrong_type_arg (nm.c_str(), args(1)); |
5785
|
106 return 0; |
5582
|
107 } |
|
108 |
|
109 for (int i = 2; i < nargin; i++) |
|
110 { |
|
111 std::string str = args(i).string_value(); |
|
112 if (error_state) |
|
113 { |
|
114 error ("%s: optional arguments must be strings", nm.c_str()); |
|
115 break; |
|
116 } |
|
117 std::transform (str.begin (), str.end (), str.begin (), tolower); |
|
118 if (str.find("once", 0) == 0) |
|
119 { |
|
120 once = true; |
|
121 nopts--; |
|
122 } |
5779
|
123 else if (str.find("matchcase", 0) == 0) |
|
124 { |
|
125 case_insensitive = false; |
|
126 nopts--; |
|
127 } |
|
128 else if (str.find("ignorecase", 0) == 0) |
|
129 { |
|
130 case_insensitive = true; |
|
131 nopts--; |
|
132 } |
5785
|
133 else if (str.find("dotall", 0) == 0) |
5779
|
134 { |
5785
|
135 dotexceptnewline = false; |
|
136 nopts--; |
|
137 } |
|
138 else if (str.find("stringanchors", 0) == 0) |
|
139 { |
|
140 lineanchors = false; |
5779
|
141 nopts--; |
|
142 } |
|
143 else if (str.find("literalspacing", 0) == 0) |
|
144 { |
|
145 freespacing = false; |
|
146 nopts--; |
|
147 } |
5785
|
148 #if HAVE_PCRE |
|
149 // Only accept these options with pcre |
|
150 else if (str.find("dotexceptnewline", 0) == 0) |
|
151 { |
|
152 dotexceptnewline = true; |
|
153 nopts--; |
|
154 } |
|
155 else if (str.find("lineanchors", 0) == 0) |
|
156 { |
|
157 lineanchors = true; |
|
158 nopts--; |
|
159 } |
|
160 else if (str.find("freespacing", 0) == 0) |
|
161 { |
|
162 freespacing = true; |
|
163 nopts--; |
|
164 } |
5582
|
165 else if (str.find("start", 0) && str.find("end", 0) && |
|
166 str.find("tokenextents", 0) && str.find("match", 0) && |
|
167 str.find("tokens", 0) && str.find("names", 0)) |
|
168 error ("%s: unrecognized option", nm.c_str()); |
|
169 #else |
5779
|
170 else if (str.find("names", 0) == 0 || |
|
171 str.find("dotexceptnewline", 0) == 0 || |
|
172 str.find("lineanchors", 0) == 0 || |
|
173 str.find("freespacing", 0) == 0) |
5785
|
174 error ("%s: %s not implemented in this version", str.c_str(), nm.c_str()); |
5582
|
175 else if (str.find("start", 0) && str.find("end", 0) && |
|
176 str.find("tokenextents", 0) && str.find("match", 0) && |
|
177 str.find("tokens", 0)) |
|
178 error ("%s: unrecognized option", nm.c_str()); |
|
179 #endif |
|
180 } |
|
181 |
|
182 if (!error_state) |
|
183 { |
5785
|
184 Cell t; |
|
185 std::string m; |
|
186 double s, e; |
5582
|
187 |
|
188 // named tokens "(?<name>...)" are only treated with PCRE not regex. |
|
189 #if HAVE_PCRE |
|
190 // The syntax of named tokens in pcre is "(?P<name>...)" while we need |
|
191 // a syntax "(?<name>...)", so fix that here. Also an expression like |
|
192 // "(?<first>\w+)\s+(?<last>\w+)|(?<last>\w+),\s+(?<first>\w+)" should |
|
193 // be perfectly legal, while pcre does not allow the same named token |
5619
|
194 // name on both sides of the alternative. Also fix that here by replacing |
|
195 // name tokens by dummy names, and dealing with the dummy names later. |
5582
|
196 |
5619
|
197 size_t pos = 0; |
|
198 size_t new_pos; |
|
199 int nnames = 0; |
|
200 int inames = 0; |
5765
|
201 std::ostringstream buf; |
5619
|
202 Array<int> named_idx; |
5582
|
203 |
5619
|
204 while ((new_pos = pattern.find ("(?<",pos)) != NPOS) |
|
205 { |
|
206 size_t tmp_pos = pattern.find_first_of ('>',new_pos); |
5582
|
207 |
5619
|
208 if (tmp_pos == NPOS) |
|
209 { |
|
210 error ("syntax error in pattern"); |
|
211 break; |
|
212 } |
|
213 |
|
214 std::string tmp_name = pattern.substr(new_pos+3,tmp_pos-new_pos-3); |
|
215 bool found = false; |
|
216 |
|
217 for (int i = 0; i < nnames; i++) |
|
218 if (named(i) == tmp_name) |
|
219 { |
|
220 named_idx.resize(inames+1); |
|
221 named_idx(inames) = i; |
|
222 found = true; |
|
223 break; |
|
224 } |
|
225 if (! found) |
|
226 { |
|
227 named_idx.resize(inames+1); |
|
228 named_idx(inames) = nnames; |
|
229 named.append(tmp_name); |
|
230 nnames++; |
|
231 } |
|
232 |
|
233 if (new_pos - pos > 0) |
|
234 buf << pattern.substr(pos,new_pos-pos); |
|
235 if (inames < 10) |
|
236 buf << "(?P<n00" << inames++; |
|
237 else if (inames < 100) |
|
238 buf << "(?P<n0" << inames++; |
|
239 else |
|
240 buf << "(?P<n" << inames++; |
|
241 pos = tmp_pos; |
|
242 } |
|
243 |
5765
|
244 buf << pattern.substr(pos); |
5619
|
245 |
|
246 if (error_state) |
5785
|
247 return 0; |
5582
|
248 |
|
249 // Compile expression |
|
250 pcre *re; |
|
251 const char *err; |
|
252 int erroffset; |
5765
|
253 std::string buf_str = buf.str (); |
|
254 re = pcre_compile (buf_str.c_str (), |
5779
|
255 (case_insensitive ? PCRE_CASELESS : 0) | |
|
256 (dotexceptnewline ? 0 : PCRE_DOTALL) | |
|
257 (lineanchors ? PCRE_MULTILINE : 0) | |
|
258 (freespacing ? PCRE_EXTENDED : 0), |
5765
|
259 &err, &erroffset, NULL); |
5582
|
260 |
|
261 if (re == NULL) { |
|
262 error("%s: %s at position %d of expression", nm.c_str(), |
|
263 err, erroffset); |
5785
|
264 return 0; |
5582
|
265 } |
|
266 |
|
267 int subpatterns; |
|
268 int namecount; |
|
269 int nameentrysize; |
|
270 char *nametable; |
|
271 int idx = 0; |
|
272 |
|
273 pcre_fullinfo(re, NULL, PCRE_INFO_CAPTURECOUNT, &subpatterns); |
|
274 pcre_fullinfo(re, NULL, PCRE_INFO_NAMECOUNT, &namecount); |
|
275 pcre_fullinfo(re, NULL, PCRE_INFO_NAMEENTRYSIZE, &nameentrysize); |
|
276 pcre_fullinfo(re, NULL, PCRE_INFO_NAMETABLE, &nametable); |
|
277 |
|
278 OCTAVE_LOCAL_BUFFER(int, ovector, (subpatterns+1)*3); |
|
279 OCTAVE_LOCAL_BUFFER(int, nidx, namecount); |
|
280 |
|
281 for (int i = 0; i < namecount; i++) |
|
282 { |
|
283 // Index of subpattern in first two bytes MSB first of name. |
5619
|
284 // Extract index. |
5779
|
285 nidx[i] = (static_cast<int>(nametable[i*nameentrysize])) << 8 | |
|
286 static_cast<int>(nametable[i*nameentrysize+1]); |
5582
|
287 } |
|
288 |
|
289 while(true) |
|
290 { |
5785
|
291 OCTAVE_QUIT; |
|
292 |
5582
|
293 int matches = pcre_exec(re, NULL, buffer.c_str(), |
|
294 buffer.length(), idx, |
|
295 (idx ? PCRE_NOTBOL : 0), |
|
296 ovector, (subpatterns+1)*3); |
|
297 |
|
298 if (matches < 0 && matches != PCRE_ERROR_NOMATCH) |
|
299 { |
|
300 error ("%s: internal error calling pcre_exec", nm.c_str()); |
|
301 pcre_free(re); |
5785
|
302 return 0; |
5582
|
303 } |
|
304 else if (matches == PCRE_ERROR_NOMATCH) |
|
305 break; |
5779
|
306 else if (ovector[1] <= ovector[0]) |
|
307 break; |
5582
|
308 else |
|
309 { |
5619
|
310 int pos_match = 0; |
5785
|
311 Matrix te(matches-1,2); |
5582
|
312 for (int i = 1; i < matches; i++) |
|
313 { |
5619
|
314 if (ovector[2*i] >= 0 && ovector[2*i+1] > 0) |
|
315 { |
5785
|
316 te(pos_match,0) = double (ovector[2*i]+1); |
|
317 te(pos_match++,1) = double (ovector[2*i+1]); |
5619
|
318 } |
5582
|
319 } |
5785
|
320 te.resize(pos_match,2); |
|
321 s = double (ovector[0]+1); |
|
322 e = double (ovector[1]); |
5582
|
323 |
|
324 const char **listptr; |
|
325 int status = pcre_get_substring_list(buffer.c_str(), ovector, |
|
326 matches, &listptr); |
|
327 |
|
328 if (status == PCRE_ERROR_NOMEMORY) { |
|
329 error("%s: cannot allocate memory in pcre_get_substring_list", |
|
330 nm.c_str()); |
|
331 pcre_free(re); |
5785
|
332 return 0; |
5582
|
333 } |
|
334 |
5619
|
335 Cell cell_t (dim_vector(1,pos_match)); |
|
336 pos_match = 0; |
5582
|
337 for (int i = 1; i < matches; i++) |
5619
|
338 if (ovector[2*i] >= 0 && ovector[2*i+1] > 0) |
|
339 cell_t(pos_match++) = std::string(*(listptr+i)); |
5582
|
340 |
5785
|
341 m = std::string(*listptr); |
|
342 t = cell_t; |
|
343 |
|
344 string_vector named_tokens(nnames); |
5619
|
345 if (namecount > 0) |
|
346 for (int i = 1; i < matches; i++) |
|
347 { |
|
348 if (ovector[2*i] >= 0 && ovector[2*i+1] > 0) |
|
349 { |
5785
|
350 named_tokens(named_idx(i-1)) = |
|
351 std::string(*(listptr+nidx[i-1])); |
5619
|
352 } |
|
353 } |
5582
|
354 |
|
355 pcre_free_substring_list(listptr); |
|
356 |
5785
|
357 regexp_elem new_elem (named_tokens, t, m, te, s, e); |
|
358 lst.push_back (new_elem); |
|
359 idx = ovector[1]; |
|
360 sz++; |
|
361 |
5582
|
362 if (once) |
|
363 break; |
|
364 |
|
365 } |
|
366 } |
|
367 |
|
368 pcre_free(re); |
|
369 #else |
|
370 regex_t compiled; |
|
371 int err=regcomp(&compiled, pattern.c_str(), REG_EXTENDED | |
|
372 (case_insensitive ? REG_ICASE : 0)); |
|
373 if (err) |
|
374 { |
|
375 int len = regerror(err, &compiled, NULL, 0); |
5760
|
376 OCTAVE_LOCAL_BUFFER (char, errmsg, len); |
|
377 regerror(err, &compiled, errmsg, len); |
|
378 error("%s: %s in pattern (%s)", nm.c_str(), errmsg, |
|
379 pattern.c_str()); |
5582
|
380 regfree(&compiled); |
5785
|
381 return 0; |
5582
|
382 } |
|
383 |
|
384 int subexpr = 1; |
|
385 int idx = 0; |
|
386 for (unsigned int i=0; i < pattern.length(); i++) |
|
387 subexpr += ( pattern[i] == '(' ? 1 : 0 ); |
|
388 OCTAVE_LOCAL_BUFFER (regmatch_t, match, subexpr ); |
|
389 |
|
390 while(true) |
|
391 { |
5785
|
392 OCTAVE_QUIT; |
|
393 |
5582
|
394 if (regexec(&compiled, buffer.c_str() + idx, subexpr, |
|
395 match, (idx ? REG_NOTBOL : 0)) == 0) |
|
396 { |
|
397 // Count actual matches |
|
398 int matches = 0; |
|
399 while (matches < subexpr && match[matches].rm_so >= 0) |
|
400 matches++; |
|
401 |
5785
|
402 s = double (match[0].rm_so+1+idx); |
|
403 e = double (match[0].rm_eo+idx); |
|
404 Matrix te(matches-1,2); |
5582
|
405 for (int i = 1; i < matches; i++) |
|
406 { |
5785
|
407 te(i-1,0) = double (match[i].rm_so+1+idx); |
|
408 te(i-1,1) = double (match[i].rm_eo+idx); |
5582
|
409 } |
|
410 |
5785
|
411 m = buffer.substr (match[0].rm_so+idx, |
5582
|
412 match[0].rm_eo-match[0].rm_so); |
|
413 |
|
414 Cell cell_t (dim_vector(1,matches-1)); |
|
415 for (int i = 1; i < matches; i++) |
|
416 cell_t(i-1) = buffer.substr (match[i].rm_so+idx, |
|
417 match[i].rm_eo-match[i].rm_so); |
5785
|
418 t = cell_t; |
5582
|
419 |
|
420 idx += match[0].rm_eo; |
5785
|
421 |
5866
|
422 string_vector sv; |
|
423 regexp_elem new_elem (sv, t, m, te, s, e); |
5785
|
424 lst.push_back (new_elem); |
5582
|
425 sz++; |
|
426 |
|
427 if (once) |
|
428 break; |
|
429 } |
|
430 else |
|
431 break; |
|
432 } |
|
433 regfree(&compiled); |
|
434 #endif |
5785
|
435 } |
|
436 #else |
|
437 error ("%s: not available in this version of Octave", nm.c_str()); |
|
438 #endif |
|
439 return sz; |
|
440 } |
5582
|
441 |
5785
|
442 static octave_value_list |
|
443 octregexp (const octave_value_list &args, int nargout, const std::string &nm, |
|
444 bool case_insensitive) |
|
445 { |
|
446 octave_value_list retval; |
|
447 int nargin = args.length(); |
|
448 std::list<regexp_elem> lst; |
|
449 string_vector named; |
|
450 int nopts; |
|
451 int sz = octregexp_list (args, nm, case_insensitive, lst, named, nopts); |
|
452 |
|
453 if (! error_state) |
|
454 { |
|
455 // Converted the linked list in the correct form for the return values |
|
456 |
|
457 octave_idx_type i = 0; |
|
458 #ifdef HAVE_PCRE |
|
459 Octave_map nmap; |
|
460 if (sz == 1) |
|
461 { |
|
462 for (int j = 0; j < named.length(); j++) |
|
463 nmap.assign (named(j), lst.begin()->named_token(j)); |
|
464 retval(5) = nmap; |
|
465 } |
|
466 else |
|
467 { |
|
468 for (int j = 0; j < named.length (); j++) |
|
469 { |
|
470 i = 0; |
|
471 Cell tmp(dim_vector (1, sz)); |
|
472 for (const_iterator p = lst.begin(); p != lst.end(); p++) |
|
473 tmp(i++) = p->named_token(j); |
|
474 nmap.assign (named(j), octave_value (tmp)); |
|
475 } |
|
476 retval(5) = nmap; |
|
477 } |
|
478 #else |
|
479 retval(5) = Octave_map(); |
|
480 #endif |
|
481 |
|
482 Cell t (dim_vector(1, sz)); |
|
483 i = 0; |
|
484 for (const_iterator p = lst.begin(); p != lst.end(); p++) |
|
485 t(i++) = p->t; |
5582
|
486 retval(4) = t; |
5785
|
487 |
|
488 Cell m (dim_vector(1, sz)); |
|
489 i = 0; |
|
490 for (const_iterator p = lst.begin(); p != lst.end(); p++) |
|
491 m(i++) = p->m; |
5582
|
492 retval(3) = m; |
5785
|
493 |
|
494 |
|
495 Cell te (dim_vector(1, sz)); |
|
496 i = 0; |
|
497 for (const_iterator p = lst.begin(); p != lst.end(); p++) |
|
498 te(i++) = p->te; |
5582
|
499 retval(2) = te; |
5785
|
500 |
|
501 NDArray e (dim_vector(1, sz)); |
|
502 i = 0; |
|
503 for (const_iterator p = lst.begin(); p != lst.end(); p++) |
|
504 e(i++) = p->e; |
5582
|
505 retval(1) = e; |
5785
|
506 |
|
507 NDArray s (dim_vector(1, sz)); |
|
508 i = 0; |
|
509 for (const_iterator p = lst.begin(); p != lst.end(); p++) |
|
510 s(i++) = p->s; |
5582
|
511 retval(0) = s; |
|
512 |
|
513 // Alter the order of the output arguments |
|
514 if (nopts > 0) |
|
515 { |
|
516 int n = 0; |
|
517 octave_value_list new_retval; |
|
518 new_retval.resize(nargout); |
|
519 |
|
520 OCTAVE_LOCAL_BUFFER (int, arg_used, 6); |
5785
|
521 for (int j = 0; j < 6; j++) |
|
522 arg_used[j] = false; |
5582
|
523 |
5785
|
524 for (int j = 2; j < nargin; j++) |
5582
|
525 { |
|
526 int k = 0; |
5785
|
527 std::string str = args(j).string_value(); |
5582
|
528 std::transform (str.begin (), str.end (), str.begin (), tolower); |
5779
|
529 if (str.find("once", 0) == 0 |
|
530 || str.find("stringanchors", 0) == 0 |
|
531 || str.find("lineanchors", 0) == 0 |
|
532 || str.find("matchcase", 0) == 0 |
|
533 || str.find("ignorecase", 0) == 0 |
|
534 || str.find("dotall", 0) == 0 |
|
535 || str.find("dotexceptnewline", 0) == 0 |
|
536 || str.find("literalspacing", 0) == 0 |
|
537 || str.find("freespacing", 0) == 0 |
|
538 ) |
5582
|
539 continue; |
|
540 else if (str.find("start", 0) == 0) |
|
541 k = 0; |
|
542 else if (str.find("end", 0) == 0) |
|
543 k = 1; |
|
544 else if (str.find("tokenextents", 0) == 0) |
|
545 k = 2; |
|
546 else if (str.find("match", 0) == 0) |
|
547 k = 3; |
|
548 else if (str.find("tokens", 0) == 0) |
|
549 k = 4; |
|
550 else if (str.find("names", 0) == 0) |
|
551 k = 5; |
|
552 |
|
553 new_retval(n++) = retval(k); |
|
554 arg_used[k] = true; |
|
555 |
|
556 if (n == nargout) |
|
557 break; |
|
558 } |
|
559 |
|
560 // Fill in the rest of the arguments |
|
561 if (n < nargout) |
|
562 { |
5785
|
563 for (int j = 0; j < 6; j++) |
5582
|
564 { |
5785
|
565 if (! arg_used[j]) |
|
566 new_retval(n++) = retval(j); |
5582
|
567 } |
|
568 } |
|
569 |
|
570 retval = new_retval; |
|
571 } |
|
572 } |
|
573 |
|
574 return retval; |
|
575 } |
|
576 |
6361
|
577 static octave_value_list |
|
578 octcellregexp (const octave_value_list &args, int nargout, const std::string &nm, |
|
579 bool case_insensitive) |
|
580 { |
|
581 octave_value_list retval; |
|
582 |
|
583 if (args(0).is_cell()) |
|
584 { |
|
585 OCTAVE_LOCAL_BUFFER (Cell, newretval, nargout); |
|
586 octave_value_list new_args = args; |
|
587 Cell cellstr = args(0).cell_value(); |
|
588 if (args(1).is_cell()) |
|
589 { |
|
590 Cell cellpat = args(1).cell_value(); |
|
591 |
|
592 if (cellpat.numel() == 1) |
|
593 { |
|
594 for (int j = 0; j < nargout; j++) |
|
595 newretval[j].resize(cellstr.dims()); |
|
596 |
|
597 new_args(1) = cellpat(0); |
|
598 |
|
599 for (octave_idx_type i = 0; i < cellstr.numel (); i++) |
|
600 { |
|
601 new_args(0) = cellstr(i); |
|
602 octave_value_list tmp = octregexp (new_args, nargout, nm, |
|
603 case_insensitive); |
|
604 |
|
605 if (error_state) |
|
606 break; |
|
607 |
|
608 for (int j = 0; j < nargout; j++) |
|
609 newretval[j](i) = tmp(j); |
|
610 } |
|
611 } |
|
612 else if (cellstr.numel() == 1) |
|
613 { |
|
614 for (int j = 0; j < nargout; j++) |
|
615 newretval[j].resize(cellpat.dims()); |
|
616 |
|
617 new_args(0) = cellstr(0); |
|
618 |
|
619 for (octave_idx_type i = 0; i < cellpat.numel (); i++) |
|
620 { |
|
621 new_args(1) = cellpat(i); |
|
622 octave_value_list tmp = octregexp (new_args, nargout, nm, |
|
623 case_insensitive); |
|
624 |
|
625 if (error_state) |
|
626 break; |
|
627 |
|
628 for (int j = 0; j < nargout; j++) |
|
629 newretval[j](i) = tmp(j); |
|
630 } |
|
631 } |
|
632 else if (cellstr.numel() == cellpat.numel()) |
|
633 { |
|
634 |
|
635 if (cellstr.dims() != cellpat.dims()) |
|
636 error ("%s: Inconsistent cell array dimensions", nm.c_str()); |
|
637 else |
|
638 { |
|
639 for (int j = 0; j < nargout; j++) |
|
640 newretval[j].resize(cellstr.dims()); |
|
641 |
|
642 for (octave_idx_type i = 0; i < cellstr.numel (); i++) |
|
643 { |
|
644 new_args(0) = cellstr(i); |
|
645 new_args(1) = cellpat(i); |
|
646 |
|
647 octave_value_list tmp = octregexp (new_args, nargout, nm, |
|
648 case_insensitive); |
|
649 |
|
650 if (error_state) |
|
651 break; |
|
652 |
|
653 for (int j = 0; j < nargout; j++) |
|
654 newretval[j](i) = tmp(j); |
|
655 } |
|
656 } |
|
657 } |
|
658 else |
|
659 error ("regexp: cell array arguments must be scalar or equal size"); |
|
660 } |
|
661 else |
|
662 { |
|
663 for (int j = 0; j < nargout; j++) |
|
664 newretval[j].resize(cellstr.dims()); |
|
665 |
|
666 for (octave_idx_type i = 0; i < cellstr.numel (); i++) |
|
667 { |
|
668 new_args(0) = cellstr(i); |
|
669 octave_value_list tmp = octregexp (new_args, nargout, nm, case_insensitive); |
|
670 |
|
671 if (error_state) |
|
672 break; |
|
673 |
|
674 for (int j = 0; j < nargout; j++) |
|
675 newretval[j](i) = tmp(j); |
|
676 } |
|
677 } |
|
678 |
|
679 if (!error_state) |
|
680 for (int j = 0; j < nargout; j++) |
|
681 retval(j) = octave_value (newretval[j]); |
|
682 } |
|
683 else if (args(1).is_cell()) |
|
684 { |
|
685 OCTAVE_LOCAL_BUFFER (Cell, newretval, nargout); |
|
686 octave_value_list new_args = args; |
|
687 Cell cellpat = args(1).cell_value(); |
|
688 |
|
689 for (int j = 0; j < nargout; j++) |
|
690 newretval[j].resize(cellpat.dims()); |
|
691 |
|
692 for (octave_idx_type i = 0; i < cellpat.numel (); i++) |
|
693 { |
|
694 new_args(1) = cellpat(i); |
|
695 octave_value_list tmp = octregexp (new_args, nargout, nm, case_insensitive); |
|
696 |
|
697 if (error_state) |
|
698 break; |
|
699 |
|
700 for (int j = 0; j < nargout; j++) |
|
701 newretval[j](i) = tmp(j); |
|
702 } |
|
703 |
|
704 if (!error_state) |
|
705 for (int j = 0; j < nargout; j++) |
|
706 retval(j) = octave_value (newretval[j]); |
|
707 } |
|
708 else |
|
709 retval = octregexp (args, nargout, nm, case_insensitive); |
|
710 |
|
711 return retval; |
|
712 |
|
713 } |
|
714 |
5582
|
715 DEFUN_DLD (regexp, args, nargout, |
|
716 "-*- texinfo -*-\n\ |
|
717 @deftypefn {Loadable Function} {[@var{s}, @var{e}, @var{te}, @var{m}, @var{t}, @var{nm}] =} regexp (@var{str}, @var{pat})\n\ |
|
718 @deftypefnx {Loadable Function} {[@dots{}] =} regexp (@var{str}, @var{pat}, @var{opts}, @dots{})\n\ |
|
719 \n\ |
|
720 Regular expression string matching. Matches @var{pat} in @var{str} and\n\ |
|
721 returns the position and matching substrings or empty values if there are\n\ |
|
722 none.\n\ |
|
723 \n\ |
|
724 The matched pattern @var{pat} can include any of the standard regex\n\ |
|
725 operators, including:\n\ |
|
726 \n\ |
|
727 @table @code\n\ |
|
728 @item .\n\ |
|
729 Match any character\n\ |
|
730 @item * + ? @{@}\n\ |
|
731 Repetition operators, representing\n\ |
|
732 @table @code\n\ |
|
733 @item *\n\ |
|
734 Match zero or more times\n\ |
|
735 @item +\n\ |
|
736 Match one or more times\n\ |
|
737 @item ?\n\ |
|
738 Match zero or one times\n\ |
|
739 @item @{@}\n\ |
|
740 Match range operator, which is of the form @code{@{@var{n}@}} to match exactly\n\ |
|
741 @var{n} times, @code{@{@var{m},@}} to match @var{m} or more times,\n\ |
|
742 @code{@{@var{m},@var{n}@}} to match between @var{m} and @var{n} times.\n\ |
|
743 @end table\n\ |
|
744 @item [@dots{}] [^@dots{}]\n\ |
|
745 List operators, where for example @code{[ab]c} matches @code{ac} and @code{bc}\n\ |
|
746 @item ()\n\ |
|
747 Grouping operator\n\ |
|
748 @item |\n\ |
|
749 Alternation operator. Match one of a choice of regular expressions. The\n\ |
7001
|
750 alternatives must be delimited by the grouping operator @code{()} above\n\ |
5582
|
751 @item ^ $\n\ |
|
752 Anchoring operator. @code{^} matches the start of the string @var{str} and\n\ |
|
753 @code{$} the end\n\ |
|
754 @end table\n\ |
|
755 \n\ |
|
756 In addition the following escaped characters have special meaning. It should\n\ |
|
757 be noted that it is recommended to quote @var{pat} in single quotes rather\n\ |
|
758 than double quotes, to avoid the escape sequences being interpreted by octave\n\ |
|
759 before being passed to @code{regexp}.\n\ |
|
760 \n\ |
|
761 @table @code\n\ |
|
762 @item \\b\n\ |
|
763 Match a word boundary\n\ |
|
764 @item \\B\n\ |
|
765 Match within a word\n\ |
|
766 @item \\w\n\ |
|
767 Matches any word character\n\ |
|
768 @item \\W\n\ |
|
769 Matches any non word character\n\ |
|
770 @item \\<\n\ |
|
771 Matches the beginning of a word\n\ |
|
772 @item \\>\n\ |
|
773 Matches the end of a word\n\ |
|
774 @item \\s\n\ |
|
775 Matches any whitespace character\n\ |
|
776 @item \\S\n\ |
|
777 Matches any non whitespace character\n\ |
|
778 @item \\d\n\ |
|
779 Matches any digit\n\ |
|
780 @item \\D\n\ |
|
781 Matches any non-digit\n\ |
|
782 @end table\n\ |
|
783 \n\ |
|
784 The outputs of @code{regexp} by default are in the order as given below\n\ |
|
785 \n\ |
|
786 @table @asis\n\ |
|
787 @item @var{s}\n\ |
|
788 The start indices of each of the matching substrings\n\ |
|
789 \n\ |
|
790 @item @var{e}\n\ |
|
791 The end indices of each matching substring\n\ |
|
792 \n\ |
|
793 @item @var{te}\n\ |
|
794 The extents of each of the matched token surrounded by @code{(@dots{})} in\n\ |
|
795 @var{pat}.\n\ |
|
796 \n\ |
|
797 @item @var{m}\n\ |
|
798 A cell array of the text of each match.\n\ |
|
799 \n\ |
|
800 @item @var{t}\n\ |
|
801 A cell array of the text of each token matched.\n\ |
|
802 \n\ |
|
803 @item @var{nm}\n\ |
|
804 A structure containing the text of each matched named token, with the name\n\ |
|
805 being used as the fieldname. A named token is denoted as\n\ |
|
806 @code{(?<name>@dots{})}\n\ |
|
807 @end table\n\ |
|
808 \n\ |
|
809 Particular output arguments or the order of the output arguments can be\n\ |
|
810 selected by additional @var{opts} arguments. These are strings and the\n\ |
|
811 correspondence between the output arguments and the optional argument\n\ |
|
812 are\n\ |
|
813 \n\ |
|
814 @multitable @columnfractions 0.2 0.3 0.3 0.2\n\ |
|
815 @item @tab 'start' @tab @var{s} @tab\n\ |
|
816 @item @tab 'end' @tab @var{e} @tab\n\ |
|
817 @item @tab 'tokenExtents' @tab @var{te} @tab\n\ |
|
818 @item @tab 'match' @tab @var{m} @tab\n\ |
|
819 @item @tab 'tokens' @tab @var{t} @tab\n\ |
|
820 @item @tab 'names' @tab @var{nm} @tab\n\ |
|
821 @end multitable\n\ |
|
822 \n\ |
|
823 A further optional argument is 'once', that limits the number of returned\n\ |
5779
|
824 matches to the first match. Additional arguments are\n\ |
|
825 \n\ |
|
826 @table @asis\n\ |
|
827 @item matchcase\n\ |
|
828 Make the matching case sensitive.\n\ |
|
829 @item ignorecase\n\ |
|
830 Make the matching case insensitive.\n\ |
|
831 @item stringanchors\n\ |
|
832 Match the anchor characters at the beginning and end of the string.\n\ |
|
833 @item lineanchors\n\ |
|
834 Match the anchor characters at the beginning and end of the line.\n\ |
|
835 @item dotall\n\ |
|
836 The character @code{.} matches the newline character.\n\ |
|
837 @item dotexceptnewline\n\ |
|
838 The character @code{.} matches all but the newline character.\n\ |
|
839 @item freespacing\n\ |
|
840 The pattern can include arbitrary whitespace and comments starting with\n\ |
|
841 @code{#}.\n\ |
|
842 @item literalspacing\n\ |
|
843 The pattern is taken literally.\n\ |
|
844 @end table\n\ |
5582
|
845 @end deftypefn") |
|
846 { |
6361
|
847 octave_value_list retval; |
|
848 int nargin = args.length(); |
|
849 |
|
850 if (nargin < 2) |
|
851 print_usage (); |
|
852 else if (args(0).is_cell() || args(1).is_cell()) |
|
853 retval = octcellregexp (args, nargout, "regexp", false); |
|
854 else |
|
855 retval = octregexp (args, nargout, "regexp", false); |
|
856 |
|
857 return retval; |
5582
|
858 } |
|
859 |
|
860 /* |
|
861 |
|
862 ## seg-fault test |
|
863 %!assert(regexp("abcde","."),[1,2,3,4,5]) |
|
864 |
|
865 ## Check that anchoring of pattern works correctly |
|
866 %!assert(regexp('abcabc','^abc'),1); |
|
867 %!assert(regexp('abcabc','abc$'),4); |
5785
|
868 %!assert(regexp('abcabc','^abc$'),zeros(1,0)); |
5582
|
869 |
|
870 %!test |
|
871 %! [s, e, te, m, t] = regexp(' No Match ', 'f(.*)uck'); |
5785
|
872 %! assert (s,zeros(1,0)) |
|
873 %! assert (e,zeros(1,0)) |
|
874 %! assert (te,cell(1,0)) |
|
875 %! assert (m, cell(1,0)) |
|
876 %! assert (t, cell(1,0)) |
5582
|
877 |
|
878 %!test |
|
879 %! [s, e, te, m, t] = regexp(' FiRetrUck ', 'f(.*)uck'); |
5785
|
880 %! assert (s,zeros(1,0)) |
|
881 %! assert (e,zeros(1,0)) |
|
882 %! assert (te,cell(1,0)) |
|
883 %! assert (m, cell(1,0)) |
|
884 %! assert (t, cell(1,0)) |
5582
|
885 |
|
886 %!test |
|
887 %! [s, e, te, m, t] = regexp(' firetruck ', 'f(.*)uck'); |
|
888 %! assert (s,2) |
|
889 %! assert (e,10) |
|
890 %! assert (te{1},[3,7]) |
|
891 %! assert (m{1}, 'firetruck') |
|
892 %! assert (t{1}{1}, 'iretr') |
|
893 |
|
894 %!test |
|
895 %! [s, e, te, m, t] = regexp('short test string','\w*r\w*'); |
|
896 %! assert (s,[1,12]) |
|
897 %! assert (e,[5,17]) |
|
898 %! assert (size(te), [1,2]) |
|
899 %! assert (isempty(te{1})) |
|
900 %! assert (isempty(te{2})) |
|
901 %! assert (m{1},'short') |
|
902 %! assert (m{2},'string') |
|
903 %! assert (size(t), [1,2]) |
|
904 %! assert (isempty(t{1})) |
|
905 %! assert (isempty(t{2})) |
|
906 |
|
907 %!test |
|
908 %! [s, e, te, m, t] = regexp('short test string','\w*r\w*','once'); |
|
909 %! assert (s,1) |
|
910 %! assert (e,5) |
|
911 %! assert (size(te), [1,1]) |
|
912 %! assert (isempty(te{1})) |
|
913 %! assert (m{1},'short') |
|
914 %! ## Matlab gives [1,0] here but that seems wrong. |
|
915 %! assert (size(t), [1,1]) |
|
916 |
|
917 %!test |
|
918 %! [m, te, e, s, t] = regexp('short test string','\w*r\w*','once', 'match', 'tokenExtents', 'end', 'start', 'tokens'); |
|
919 %! assert (s,1) |
|
920 %! assert (e,5) |
|
921 %! assert (size(te), [1,1]) |
|
922 %! assert (isempty(te{1})) |
|
923 %! assert (m{1},'short') |
|
924 %! ## Matlab gives [1,0] here but that seems wrong. |
|
925 %! assert (size(t), [1,1]) |
|
926 |
5619
|
927 %!test |
5582
|
928 %! ## This test is expected to fail if PCRE is not installed |
5620
|
929 %! if (!isempty(findstr(octave_config_info ("DEFS"),"HAVE_PCRE"))) |
5619
|
930 %! [s, e, te, m, t, nm] = regexp('short test string','(?<word1>\w*t)\s*(?<word2>\w*t)'); |
|
931 %! assert (s,1) |
|
932 %! assert (e,10) |
|
933 %! assert (size(te), [1,1]) |
|
934 %! assert (te{1}, [1 5; 7, 10]) |
|
935 %! assert (m{1},'short test') |
|
936 %! assert (size(t),[1,1]) |
|
937 %! assert (t{1}{1},'short') |
|
938 %! assert (t{1}{2},'test') |
|
939 %! assert (size(nm), [1,1]) |
|
940 %! assert (!isempty(fieldnames(nm))) |
|
941 %! assert (sort(fieldnames(nm)),{'word1';'word2'}) |
|
942 %! assert (nm.word1,'short') |
|
943 %! assert (nm.word2,'test') |
|
944 %! endif |
5582
|
945 |
5619
|
946 %!test |
5582
|
947 %! ## This test is expected to fail if PCRE is not installed |
5620
|
948 %! if (!isempty(findstr(octave_config_info ("DEFS"),"HAVE_PCRE"))) |
5619
|
949 %! [nm, m, te, e, s, t] = regexp('short test string','(?<word1>\w*t)\s*(?<word2>\w*t)', 'names', 'match', 'tokenExtents', 'end', 'start', 'tokens'); |
|
950 %! assert (s,1) |
|
951 %! assert (e,10) |
|
952 %! assert (size(te), [1,1]) |
|
953 %! assert (te{1}, [1 5; 7, 10]) |
|
954 %! assert (m{1},'short test') |
|
955 %! assert (size(t),[1,1]) |
|
956 %! assert (t{1}{1},'short') |
|
957 %! assert (t{1}{2},'test') |
|
958 %! assert (size(nm), [1,1]) |
|
959 %! assert (!isempty(fieldnames(nm))) |
|
960 %! assert (sort(fieldnames(nm)),{'word1';'word2'}) |
|
961 %! assert (nm.word1,'short') |
|
962 %! assert (nm.word2,'test') |
|
963 %! endif |
|
964 |
|
965 %!test |
|
966 %! ## This test is expected to fail if PCRE is not installed |
5620
|
967 %! if (!isempty(findstr(octave_config_info ("DEFS"),"HAVE_PCRE"))) |
5619
|
968 %! [t, nm] = regexp("John Davis\nRogers, James",'(?<first>\w+)\s+(?<last>\w+)|(?<last>\w+),\s+(?<first>\w+)','tokens','names'); |
|
969 %! assert (size(t), [1,2]); |
|
970 %! assert (t{1}{1},'John'); |
|
971 %! assert (t{1}{2},'Davis'); |
|
972 %! assert (t{2}{1},'Rogers'); |
|
973 %! assert (t{2}{2},'James'); |
|
974 %! assert (size(nm), [1,1]); |
|
975 %! assert (nm.first{1},'John'); |
|
976 %! assert (nm.first{2},'James'); |
|
977 %! assert (nm.last{1},'Davis'); |
|
978 %! assert (nm.last{2},'Rogers'); |
|
979 %! endif |
5582
|
980 |
5779
|
981 %!assert(regexp("abc\nabc",'.'),[1:7]) |
|
982 %!assert(regexp("abc\nabc",'.','dotall'),[1:7]) |
|
983 %!test |
|
984 %! if (!isempty(findstr(octave_config_info ("DEFS"),"HAVE_PCRE"))) |
|
985 %! assert(regexp("abc\nabc",'(?s).'),[1:7]) |
|
986 %! assert(regexp("abc\nabc",'.','dotexceptnewline'),[1,2,3,5,6,7]) |
|
987 %! assert(regexp("abc\nabc",'(?-s).'),[1,2,3,5,6,7]) |
|
988 %! endif |
|
989 |
|
990 %!assert(regexp("caseCaSe",'case'),1) |
|
991 %!assert(regexp("caseCaSe",'case',"matchcase"),1) |
|
992 %!assert(regexp("caseCaSe",'case',"ignorecase"),[1,5]) |
|
993 %!test |
|
994 %! if (!isempty(findstr(octave_config_info ("DEFS"),"HAVE_PCRE"))) |
|
995 %! assert(regexp("caseCaSe",'(?-i)case'),1) |
|
996 %! assert(regexp("caseCaSe",'(?i)case'),[1,5]) |
|
997 %! endif |
|
998 |
|
999 %!assert (regexp("abc\nabc",'c$'),7) |
|
1000 %!assert (regexp("abc\nabc",'c$',"stringanchors"),7) |
|
1001 %!test |
|
1002 %! if (!isempty(findstr(octave_config_info ("DEFS"),"HAVE_PCRE"))) |
|
1003 %! assert (regexp("abc\nabc",'(?-m)c$'),7) |
|
1004 %! assert (regexp("abc\nabc",'c$',"lineanchors"),[3,7]) |
|
1005 %! assert (regexp("abc\nabc",'(?m)c$'),[3,7]) |
|
1006 %! endif |
|
1007 |
|
1008 %!assert (regexp("this word",'s w'),4) |
|
1009 %!assert (regexp("this word",'s w','literalspacing'),4) |
|
1010 %!test |
|
1011 %! if (!isempty(findstr(octave_config_info ("DEFS"),"HAVE_PCRE"))) |
|
1012 %! assert (regexp("this word",'(?-x)s w','literalspacing'),4) |
5785
|
1013 %! assert (regexp("this word",'s w','freespacing'),zeros(1,0)) |
|
1014 %! assert (regexp("this word",'(?x)s w'),zeros(1,0)) |
5779
|
1015 %! endif |
|
1016 |
5582
|
1017 %!error regexp('string', 'tri', 'BadArg'); |
|
1018 %!error regexp('string'); |
|
1019 |
6361
|
1020 %!assert(regexp({'asdfg-dfd';'-dfd-dfd-';'qasfdfdaq'},'-'),{6;[1,5,9];zeros(1,0)}) |
|
1021 %!assert(regexp({'asdfg-dfd','-dfd-dfd-','qasfdfdaq'},'-'),{6,[1,5,9],zeros(1,0)}) |
|
1022 %!assert(regexp({'asdfg-dfd';'-dfd-dfd-';'qasfdfdaq'},{'-';'f';'q'}),{6;[3,7];[1,9]}) |
|
1023 %!assert(regexp('Strings',{'t','s'}),{2,7}) |
|
1024 |
5582
|
1025 */ |
|
1026 |
6549
|
1027 DEFUN_DLD (regexpi, args, nargout, |
5582
|
1028 "-*- texinfo -*-\n\ |
|
1029 @deftypefn {Loadable Function} {[@var{s}, @var{e}, @var{te}, @var{m}, @var{t}, @var{nm}] =} regexpi (@var{str}, @var{pat})\n\ |
|
1030 @deftypefnx {Loadable Function} {[@dots{}] =} regexpi (@var{str}, @var{pat}, @var{opts}, @dots{})\n\ |
|
1031 \n\ |
|
1032 Case insensitive regular expression string matching. Matches @var{pat} in\n\ |
|
1033 @var{str} and returns the position and matching substrings or empty values\n\ |
|
1034 if there are none. See @code{regexp} for more details\n\ |
|
1035 @end deftypefn") |
|
1036 { |
6361
|
1037 octave_value_list retval; |
|
1038 int nargin = args.length(); |
|
1039 |
|
1040 if (nargin < 2) |
|
1041 print_usage (); |
|
1042 else if (args(0).is_cell() || args(1).is_cell()) |
|
1043 retval = octcellregexp (args, nargout, "regexpi", true); |
|
1044 else |
|
1045 retval = octregexp (args, nargout, "regexpi", true); |
|
1046 |
|
1047 return retval; |
5582
|
1048 } |
|
1049 |
|
1050 /* |
|
1051 |
|
1052 ## seg-fault test |
|
1053 %!assert(regexpi("abcde","."),[1,2,3,4,5]) |
|
1054 |
|
1055 ## Check that anchoring of pattern works correctly |
|
1056 %!assert(regexpi('abcabc','^abc'),1); |
|
1057 %!assert(regexpi('abcabc','abc$'),4); |
5785
|
1058 %!assert(regexpi('abcabc','^abc$'),zeros(1,0)); |
5582
|
1059 |
|
1060 %!test |
|
1061 %! [s, e, te, m, t] = regexpi(' No Match ', 'f(.*)uck'); |
5785
|
1062 %! assert (s,zeros(1,0)) |
|
1063 %! assert (e,zeros(1,0)) |
|
1064 %! assert (te,cell(1,0)) |
|
1065 %! assert (m, cell(1,0)) |
|
1066 %! assert (t, cell(1,0)) |
5582
|
1067 |
|
1068 %!test |
|
1069 %! [s, e, te, m, t] = regexpi(' FiRetrUck ', 'f(.*)uck'); |
|
1070 %! assert (s,2) |
|
1071 %! assert (e,10) |
|
1072 %! assert (te{1},[3,7]) |
|
1073 %! assert (m{1}, 'FiRetrUck') |
|
1074 %! assert (t{1}{1}, 'iRetr') |
|
1075 |
|
1076 %!test |
|
1077 %! [s, e, te, m, t] = regexpi(' firetruck ', 'f(.*)uck'); |
|
1078 %! assert (s,2) |
|
1079 %! assert (e,10) |
|
1080 %! assert (te{1},[3,7]) |
|
1081 %! assert (m{1}, 'firetruck') |
|
1082 %! assert (t{1}{1}, 'iretr') |
|
1083 |
|
1084 %!test |
|
1085 %! [s, e, te, m, t] = regexpi('ShoRt Test String','\w*r\w*'); |
|
1086 %! assert (s,[1,12]) |
|
1087 %! assert (e,[5,17]) |
|
1088 %! assert (size(te), [1,2]) |
|
1089 %! assert (isempty(te{1})) |
|
1090 %! assert (isempty(te{2})) |
|
1091 %! assert (m{1},'ShoRt') |
|
1092 %! assert (m{2},'String') |
|
1093 %! assert (size(t), [1,2]) |
|
1094 %! assert (isempty(t{1})) |
|
1095 %! assert (isempty(t{2})) |
|
1096 |
|
1097 %!test |
|
1098 %! [s, e, te, m, t] = regexpi('ShoRt Test String','\w*r\w*','once'); |
|
1099 %! assert (s,1) |
|
1100 %! assert (e,5) |
|
1101 %! assert (size(te), [1,1]) |
|
1102 %! assert (isempty(te{1})) |
|
1103 %! assert (m{1},'ShoRt') |
|
1104 %! ## Matlab gives [1,0] here but that seems wrong. |
|
1105 %! assert (size(t), [1,1]) |
|
1106 |
|
1107 %!test |
|
1108 %! [m, te, e, s, t] = regexpi('ShoRt Test String','\w*r\w*','once', 'match', 'tokenExtents', 'end', 'start', 'tokens'); |
|
1109 %! assert (s,1) |
|
1110 %! assert (e,5) |
|
1111 %! assert (size(te), [1,1]) |
|
1112 %! assert (isempty(te{1})) |
|
1113 %! assert (m{1},'ShoRt') |
|
1114 %! ## Matlab gives [1,0] here but that seems wrong. |
|
1115 %! assert (size(t), [1,1]) |
|
1116 |
5619
|
1117 %!test |
5582
|
1118 %! ## This test is expected to fail if PCRE is not installed |
5620
|
1119 %! if (!isempty(findstr(octave_config_info ("DEFS"),"HAVE_PCRE"))) |
5619
|
1120 %! [s, e, te, m, t, nm] = regexpi('ShoRt Test String','(?<word1>\w*t)\s*(?<word2>\w*t)'); |
|
1121 %! assert (s,1) |
|
1122 %! assert (e,10) |
|
1123 %! assert (size(te), [1,1]) |
|
1124 %! assert (te{1}, [1 5; 7, 10]) |
|
1125 %! assert (m{1},'ShoRt Test') |
|
1126 %! assert (size(t),[1,1]) |
|
1127 %! assert (t{1}{1},'ShoRt') |
|
1128 %! assert (t{1}{2},'Test') |
|
1129 %! assert (size(nm), [1,1]) |
|
1130 %! assert (!isempty(fieldnames(nm))) |
|
1131 %! assert (sort(fieldnames(nm)),{'word1';'word2'}) |
|
1132 %! assert (nm.word1,'ShoRt') |
|
1133 %! assert (nm.word2,'Test') |
|
1134 %! endif |
5582
|
1135 |
5619
|
1136 %!test |
5582
|
1137 %! ## This test is expected to fail if PCRE is not installed |
5620
|
1138 %! if (!isempty(findstr(octave_config_info ("DEFS"),"HAVE_PCRE"))) |
5619
|
1139 %! [nm, m, te, e, s, t] = regexpi('ShoRt Test String','(?<word1>\w*t)\s*(?<word2>\w*t)', 'names', 'match', 'tokenExtents', 'end', 'start', 'tokens'); |
|
1140 %! assert (s,1) |
|
1141 %! assert (e,10) |
|
1142 %! assert (size(te), [1,1]) |
|
1143 %! assert (te{1}, [1 5; 7, 10]) |
|
1144 %! assert (m{1},'ShoRt Test') |
|
1145 %! assert (size(t),[1,1]) |
|
1146 %! assert (t{1}{1},'ShoRt') |
|
1147 %! assert (t{1}{2},'Test') |
|
1148 %! assert (size(nm), [1,1]) |
|
1149 %! assert (!isempty(fieldnames(nm))) |
|
1150 %! assert (sort(fieldnames(nm)),{'word1';'word2'}) |
|
1151 %! assert (nm.word1,'ShoRt') |
|
1152 %! assert (nm.word2,'Test') |
|
1153 %! endif |
5582
|
1154 |
5779
|
1155 %!assert(regexpi("abc\nabc",'.'),[1:7]) |
|
1156 %!assert(regexpi("abc\nabc",'.','dotall'),[1:7]) |
|
1157 %!test |
|
1158 %! if (!isempty(findstr(octave_config_info ("DEFS"),"HAVE_PCRE"))) |
|
1159 %! assert(regexpi("abc\nabc",'(?s).'),[1:7]) |
|
1160 %! assert(regexpi("abc\nabc",'.','dotexceptnewline'),[1,2,3,5,6,7]) |
|
1161 %! assert(regexpi("abc\nabc",'(?-s).'),[1,2,3,5,6,7]) |
|
1162 %! endif |
|
1163 |
|
1164 %!assert(regexpi("caseCaSe",'case'),[1,5]) |
|
1165 %!assert(regexpi("caseCaSe",'case',"matchcase"),1) |
|
1166 %!assert(regexpi("caseCaSe",'case',"ignorecase"),[1,5]) |
|
1167 %!test |
|
1168 %! if (!isempty(findstr(octave_config_info ("DEFS"),"HAVE_PCRE"))) |
|
1169 %! assert(regexpi("caseCaSe",'(?-i)case'),1) |
|
1170 %! assert(regexpi("caseCaSe",'(?i)case'),[1,5]) |
|
1171 %! endif |
|
1172 |
|
1173 %!assert (regexpi("abc\nabc",'c$'),7) |
|
1174 %!assert (regexpi("abc\nabc",'c$',"stringanchors"),7) |
|
1175 %!test |
|
1176 %! if (!isempty(findstr(octave_config_info ("DEFS"),"HAVE_PCRE"))) |
|
1177 %! assert (regexpi("abc\nabc",'(?-m)c$'),7) |
|
1178 %! assert (regexpi("abc\nabc",'c$',"lineanchors"),[3,7]) |
|
1179 %! assert (regexpi("abc\nabc",'(?m)c$'),[3,7]) |
|
1180 %! endif |
|
1181 |
|
1182 %!assert (regexpi("this word",'s w'),4) |
|
1183 %!assert (regexpi("this word",'s w','literalspacing'),4) |
|
1184 %!test |
|
1185 %! if (!isempty(findstr(octave_config_info ("DEFS"),"HAVE_PCRE"))) |
|
1186 %! assert (regexpi("this word",'(?-x)s w','literalspacing'),4) |
5785
|
1187 %! assert (regexpi("this word",'s w','freespacing'),zeros(1,0)) |
|
1188 %! assert (regexpi("this word",'(?x)s w'),zeros(1,0)) |
5779
|
1189 %! endif |
|
1190 |
5582
|
1191 %!error regexpi('string', 'tri', 'BadArg'); |
|
1192 %!error regexpi('string'); |
|
1193 |
6361
|
1194 %!assert(regexpi({'asdfg-dfd';'-dfd-dfd-';'qasfdfdaq'},'-'),{6;[1,5,9];zeros(1,0)}) |
|
1195 %!assert(regexpi({'asdfg-dfd','-dfd-dfd-','qasfdfdaq'},'-'),{6,[1,5,9],zeros(1,0)}) |
|
1196 %!assert(regexpi({'asdfg-dfd';'-dfd-dfd-';'qasfdfdaq'},{'-';'f';'q'}),{6;[3,7];[1,9]}) |
|
1197 %!assert(regexpi('Strings',{'t','s'}),{2,[1,7]}) |
|
1198 |
5582
|
1199 */ |
|
1200 |
6361
|
1201 |
|
1202 static octave_value |
|
1203 octregexprep (const octave_value_list &args, const std::string &nm) |
5785
|
1204 { |
6361
|
1205 octave_value retval; |
5785
|
1206 int nargin = args.length(); |
|
1207 |
|
1208 // Make sure we have string,pattern,replacement |
|
1209 const std::string buffer = args(0).string_value (); |
|
1210 if (error_state) return retval; |
|
1211 const std::string pattern = args(1).string_value (); |
|
1212 if (error_state) return retval; |
|
1213 const std::string replacement = args(2).string_value (); |
|
1214 if (error_state) return retval; |
|
1215 |
|
1216 // Pack options excluding 'tokenize' and various output |
|
1217 // reordering strings into regexp arg list |
|
1218 octave_value_list regexpargs(nargin-1,octave_value()); |
|
1219 regexpargs(0) = args(0); |
|
1220 regexpargs(1) = args(1); |
|
1221 int len=2; |
|
1222 for (int i = 3; i < nargin; i++) |
|
1223 { |
|
1224 const std::string opt = args(i).string_value(); |
|
1225 if (opt != "tokenize" && opt != "start" && opt != "end" |
|
1226 && opt != "tokenextents" && opt != "match" && opt != "tokens" |
|
1227 && opt != "names" && opt != "warnings") |
|
1228 { |
|
1229 regexpargs(len++) = args(i); |
|
1230 } |
|
1231 } |
|
1232 regexpargs.resize(len); |
|
1233 |
|
1234 // Identify replacement tokens; build a vector of group numbers in |
|
1235 // the replacement string so that we can quickly calculate the size |
|
1236 // of the replacement. |
|
1237 int tokens = 0; |
|
1238 for (size_t i=1; i < replacement.size(); i++) |
|
1239 { |
|
1240 if (replacement[i-1]=='$' && isdigit(replacement[i])) |
|
1241 { |
|
1242 tokens++, i++; |
|
1243 } |
|
1244 } |
|
1245 std::vector<int> token(tokens); |
|
1246 int kk = 0; |
|
1247 for (size_t i = 1; i < replacement.size(); i++) |
|
1248 { |
|
1249 if (replacement[i-1]=='$' && isdigit(replacement[i])) |
|
1250 { |
|
1251 token[kk++] = replacement[i]-'0'; |
|
1252 i++; |
|
1253 } |
|
1254 } |
|
1255 |
|
1256 // Perform replacement |
|
1257 std::string rep; |
|
1258 if (tokens > 0) |
|
1259 { |
|
1260 std::list<regexp_elem> lst; |
|
1261 string_vector named; |
|
1262 int nopts; |
6361
|
1263 int sz = octregexp_list (regexpargs, nm , false, lst, named, nopts); |
5785
|
1264 |
|
1265 if (error_state) |
|
1266 return retval; |
|
1267 if (sz == 0) |
|
1268 { |
6361
|
1269 retval = args(0); |
5785
|
1270 return retval; |
|
1271 } |
|
1272 |
|
1273 // Determine replacement length |
|
1274 const size_t replen = replacement.size() - 2*tokens; |
|
1275 int delta = 0; |
|
1276 const_iterator p = lst.begin(); |
|
1277 for (int i = 0; i < sz; i++) |
|
1278 { |
|
1279 OCTAVE_QUIT; |
|
1280 |
|
1281 const Matrix pairs(p->te); |
|
1282 size_t pairlen = 0; |
|
1283 for (int j = 0; j < tokens; j++) |
|
1284 { |
|
1285 if (token[j] == 0) |
|
1286 pairlen += static_cast<size_t>(p->e - p->s) + 1; |
|
1287 else if (token[j] <= pairs.rows()) |
|
1288 pairlen += static_cast<size_t>(pairs(token[j]-1,1) - |
|
1289 pairs(token[j]-1,0)) + 1; |
|
1290 } |
|
1291 delta += static_cast<int>(replen + pairlen) - |
|
1292 static_cast<int>(p->e - p->s + 1); |
|
1293 p++; |
|
1294 } |
|
1295 |
|
1296 // Build replacement string |
|
1297 rep.reserve(buffer.size()+delta); |
|
1298 size_t from = 0; |
|
1299 p = lst.begin(); |
|
1300 for (int i=0; i < sz; i++) |
|
1301 { |
|
1302 OCTAVE_QUIT; |
|
1303 |
|
1304 const Matrix pairs(p->te); |
|
1305 rep.append(&buffer[from], static_cast<size_t>(p->s - 1) - from); |
|
1306 from = static_cast<size_t>(p->e - 1) + 1; |
|
1307 for (size_t j = 1; j < replacement.size(); j++) |
|
1308 { |
|
1309 if (replacement[j-1]=='$' && isdigit(replacement[j])) |
|
1310 { |
|
1311 int k = replacement[j]-'0'; |
|
1312 if (k == 0) |
|
1313 { |
|
1314 // replace with entire match |
|
1315 rep.append(&buffer[static_cast<size_t>(p->e - 1)], |
|
1316 static_cast<size_t>(p->e - p->s) + 1); |
|
1317 } |
|
1318 else if (k <= pairs.rows()) |
|
1319 { |
|
1320 // replace with group capture |
|
1321 rep.append(&buffer[static_cast<size_t>(pairs(k-1,0)-1)], |
|
1322 static_cast<size_t>(pairs(k-1,1) - |
|
1323 pairs(k-1,0))+1); |
|
1324 } |
|
1325 else |
|
1326 { |
|
1327 // replace with nothing |
|
1328 } |
|
1329 j++; |
|
1330 } |
|
1331 else |
|
1332 { |
|
1333 rep.append(1,replacement[j-1]); |
|
1334 } |
|
1335 if (j+1 == replacement.size()) |
|
1336 { |
|
1337 rep.append(1,replacement[j]); |
|
1338 } |
|
1339 } |
|
1340 p++; |
|
1341 } |
|
1342 rep.append(&buffer[from],buffer.size()-from); |
|
1343 } |
|
1344 else |
|
1345 { |
|
1346 std::list<regexp_elem> lst; |
|
1347 string_vector named; |
|
1348 int nopts; |
6361
|
1349 int sz = octregexp_list (regexpargs, nm, false, lst, named,nopts); |
5785
|
1350 |
|
1351 if (error_state) |
|
1352 return retval; |
|
1353 if (sz == 0) |
|
1354 { |
6361
|
1355 retval = args(0); |
5785
|
1356 return retval; |
|
1357 } |
|
1358 |
|
1359 // Determine replacement length |
|
1360 const size_t replen = replacement.size(); |
|
1361 int delta = 0; |
|
1362 const_iterator p = lst.begin(); |
|
1363 for (int i = 0; i < sz; i++) |
|
1364 { |
|
1365 OCTAVE_QUIT; |
|
1366 delta += static_cast<int>(replen) - |
|
1367 static_cast<int>(p->e - p->s + 1); |
|
1368 p++; |
|
1369 } |
|
1370 |
|
1371 // Build replacement string |
|
1372 rep.reserve(buffer.size()+delta); |
|
1373 size_t from = 0; |
|
1374 p = lst.begin(); |
|
1375 for (int i=0; i < sz; i++) |
|
1376 { |
|
1377 OCTAVE_QUIT; |
|
1378 rep.append(&buffer[from], static_cast<size_t>(p->s - 1) - from); |
|
1379 from = static_cast<size_t>(p->e - 1) + 1; |
|
1380 rep.append(replacement); |
|
1381 p++; |
|
1382 } |
|
1383 rep.append(&buffer[from],buffer.size()-from); |
|
1384 } |
|
1385 |
6361
|
1386 retval = rep; |
|
1387 return retval; |
|
1388 } |
|
1389 |
6549
|
1390 DEFUN_DLD (regexprep, args, , |
6361
|
1391 "-*- texinfo -*-\n\ |
6678
|
1392 @deftypefn {Loadable Function} {@var{string} =} regexprep (@var{string}, @var{pat}, @var{repstr}, @var{options})\n\ |
6361
|
1393 Replace matches of @var{pat} in @var{string} with @var{repstr}.\n\ |
|
1394 \n\ |
|
1395 \n\ |
7007
|
1396 The replacement can contain @code{$i}, which substitutes\n\ |
6361
|
1397 for the ith set of parentheses in the match string. E.g.,\n\ |
|
1398 @example\n\ |
|
1399 \n\ |
|
1400 regexprep(\"Bill Dunn\",'(\\w+) (\\w+)','$2, $1')\n\ |
|
1401 \n\ |
|
1402 @end example\n\ |
|
1403 returns \"Dunn, Bill\"\n\ |
|
1404 \n\ |
|
1405 @var{options} may be zero or more of\n\ |
|
1406 @table @samp\n\ |
|
1407 \n\ |
|
1408 @item once\n\ |
7001
|
1409 Replace only the first occurrence of @var{pat} in the result.\n\ |
6361
|
1410 \n\ |
|
1411 @item warnings\n\ |
|
1412 This option is present for compatibility but is ignored.\n\ |
|
1413 \n\ |
|
1414 @item ignorecase or matchcase\n\ |
|
1415 Ignore case for the pattern matching (see @code{regexpi}).\n\ |
|
1416 Alternatively, use (?i) or (?-i) in the pattern.\n\ |
|
1417 \n\ |
|
1418 @item lineanchors and stringanchors\n\ |
|
1419 Whether characters ^ and $ match the beginning and ending of lines.\n\ |
|
1420 Alternatively, use (?m) or (?-m) in the pattern.\n\ |
|
1421 \n\ |
|
1422 @item dotexceptnewline and dotall\n\ |
|
1423 Whether . matches newlines in the string.\n\ |
|
1424 Alternatively, use (?s) or (?-s) in the pattern.\n\ |
|
1425 \n\ |
|
1426 @item freespacing or literalspacing\n\ |
|
1427 Whether whitespace and # comments can be used to make the regular expression more readable.\n\ |
|
1428 Alternatively, use (?x) or (?-x) in the pattern.\n\ |
|
1429 \n\ |
|
1430 @end table\n\ |
|
1431 @seealso{regexp,regexpi}\n\ |
|
1432 @end deftypefn") |
|
1433 { |
|
1434 octave_value_list retval; |
|
1435 int nargin = args.length(); |
|
1436 |
|
1437 if (nargin < 3) |
|
1438 { |
|
1439 print_usage (); |
|
1440 return retval; |
|
1441 } |
|
1442 |
|
1443 if (args(0).is_cell() || args(1).is_cell() || args(2).is_cell()) |
|
1444 { |
|
1445 Cell str; |
|
1446 Cell pat; |
|
1447 Cell rep; |
6495
|
1448 dim_vector dv0; |
|
1449 dim_vector dv1(1,1); |
6361
|
1450 |
|
1451 if (args(0).is_cell()) |
|
1452 str = args(0).cell_value(); |
|
1453 else |
|
1454 str = Cell (args(0)); |
|
1455 |
|
1456 if (args(1).is_cell()) |
|
1457 pat = args(1).cell_value(); |
|
1458 else |
|
1459 pat = Cell (args(1)); |
|
1460 |
|
1461 if (args(2).is_cell()) |
|
1462 rep = args(2).cell_value(); |
|
1463 else |
|
1464 rep = Cell (args(2)); |
|
1465 |
6495
|
1466 dv0 = str.dims(); |
|
1467 if (pat.numel() != 1) |
6361
|
1468 { |
6495
|
1469 dv1 = pat.dims(); |
|
1470 if (rep.numel() != 1 && dv1 != rep.dims()) |
6361
|
1471 error ("regexprep: Inconsistent cell array dimensions"); |
|
1472 } |
|
1473 else if (rep.numel() != 1) |
6495
|
1474 dv1 = rep.dims(); |
6361
|
1475 |
|
1476 if (!error_state) |
|
1477 { |
6495
|
1478 Cell ret (dv0); |
6361
|
1479 octave_value_list new_args = args; |
|
1480 |
6495
|
1481 for (octave_idx_type i = 0; i < dv0.numel(); i++) |
|
1482 { |
|
1483 new_args(0) = str(i); |
|
1484 if (pat.numel() == 1) |
|
1485 new_args(1) = pat(0); |
|
1486 if (rep.numel() == 1) |
|
1487 new_args(2) = rep(0); |
|
1488 for (octave_idx_type j = 0; j < dv1.numel(); j++) |
|
1489 { |
|
1490 if (pat.numel() != 1) |
|
1491 new_args(1) = pat(j); |
|
1492 if (rep.numel() != 1) |
|
1493 new_args(2) = rep(j); |
|
1494 new_args(0) = octregexprep (new_args, "regexprep"); |
6361
|
1495 |
6495
|
1496 if (error_state) |
|
1497 break; |
|
1498 } |
6361
|
1499 |
|
1500 if (error_state) |
|
1501 break; |
6495
|
1502 |
|
1503 ret(i) = new_args(0); |
6361
|
1504 } |
|
1505 |
|
1506 if (!error_state) |
|
1507 retval = octave_value (ret); |
|
1508 } |
|
1509 } |
|
1510 else |
|
1511 retval = octregexprep (args, "regexprep"); |
|
1512 |
5785
|
1513 return retval; |
|
1514 } |
|
1515 |
|
1516 /* |
|
1517 %!test # Replace with empty |
|
1518 %! xml = '<!-- This is some XML --> <tag v="hello">some stuff<!-- sample tag--></tag>'; |
|
1519 %! t = regexprep(xml,'<[!?][^>]*>',''); |
|
1520 %! assert(t,' <tag v="hello">some stuff</tag>') |
|
1521 |
|
1522 %!test # Replace with non-empty |
|
1523 %! xml = '<!-- This is some XML --> <tag v="hello">some stuff<!-- sample tag--></tag>'; |
|
1524 %! t = regexprep(xml,'<[!?][^>]*>','?'); |
|
1525 %! assert(t,'? <tag v="hello">some stuff?</tag>') |
|
1526 |
|
1527 %!test # Check that 'tokenize' is ignored |
|
1528 %! xml = '<!-- This is some XML --> <tag v="hello">some stuff<!-- sample tag--></tag>'; |
|
1529 %! t = regexprep(xml,'<[!?][^>]*>','','tokenize'); |
|
1530 %! assert(t,' <tag v="hello">some stuff</tag>') |
|
1531 |
|
1532 %!test # Capture replacement |
|
1533 %! if (!isempty(findstr(octave_config_info ("DEFS"),"HAVE_PCRE"))) |
|
1534 %! data = "Bob Smith\nDavid Hollerith\nSam Jenkins"; |
|
1535 %! result = "Smith, Bob\nHollerith, David\nJenkins, Sam"; |
|
1536 %! t = regexprep(data,'(?m)^(\w+)\s+(\w+)$','$2, $1'); |
|
1537 %! assert(t,result) |
|
1538 %! end |
|
1539 |
|
1540 # Return the original if no match |
|
1541 %!assert(regexprep('hello','world','earth'),'hello') |
|
1542 |
|
1543 ## Test a general replacement |
|
1544 %!assert(regexprep("a[b]c{d}e-f=g", "[^A-Za-z0-9_]", "_"), "a_b_c_d_e_f_g"); |
|
1545 |
|
1546 ## Make sure it works at the beginning and end |
|
1547 %!assert(regexprep("a[b]c{d}e-f=g", "a", "_"), "_[b]c{d}e-f=g"); |
|
1548 %!assert(regexprep("a[b]c{d}e-f=g", "g", "_"), "a[b]c{d}e-f=_"); |
|
1549 |
|
1550 ## Options |
|
1551 %!assert(regexprep("a[b]c{d}e-f=g", "[^A-Za-z0-9_]", "_", "once"), "a_b]c{d}e-f=g"); |
|
1552 %!assert(regexprep("a[b]c{d}e-f=g", "[^A-Z0-9_]", "_", "ignorecase"), "a_b_c_d_e_f_g"); |
|
1553 |
|
1554 ## Option combinations |
|
1555 %!assert(regexprep("a[b]c{d}e-f=g", "[^A-Z0-9_]", "_", "once", "ignorecase"), "a_b]c{d}e-f=g"); |
|
1556 |
|
1557 ## End conditions on replacement |
|
1558 %!assert(regexprep("abc","(b)",".$1"),"a.bc"); |
|
1559 %!assert(regexprep("abc","(b)","$1"),"abc"); |
|
1560 %!assert(regexprep("abc","(b)","$1."),"ab.c"); |
|
1561 %!assert(regexprep("abc","(b)","$1.."),"ab..c"); |
|
1562 |
6361
|
1563 ## Test cell array arguments |
6503
|
1564 %!assert(regexprep("abc",{"b","a"},"?"),{"??c"}) |
6361
|
1565 %!assert(regexprep({"abc","cba"},"b","?"),{"a?c","c?a"}) |
6503
|
1566 %!assert(regexprep({"abc","cba"},{"b","a"},{"?","!"}),{"!?c","c?!"}) |
6361
|
1567 |
5785
|
1568 */ |
|
1569 |
5582
|
1570 /* |
|
1571 ;;; Local Variables: *** |
|
1572 ;;; mode: C++ *** |
|
1573 ;;; End: *** |
|
1574 */ |