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