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