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: |
5787
|
62 regexp_elem (const string_vector& _named_token, const Cell& _t, |
|
63 const std::string& _m, const Matrix& _te, double _s, |
|
64 double _e) : |
5785
|
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 { |
5823
|
98 print_usage (); |
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 |
5866
|
429 string_vector sv; |
|
430 regexp_elem new_elem (sv, t, m, te, s, e); |
5785
|
431 lst.push_back (new_elem); |
5582
|
432 sz++; |
|
433 |
|
434 if (once) |
|
435 break; |
|
436 } |
|
437 else |
|
438 break; |
|
439 } |
|
440 regfree(&compiled); |
|
441 #endif |
5785
|
442 } |
|
443 #else |
|
444 error ("%s: not available in this version of Octave", nm.c_str()); |
|
445 #endif |
|
446 return sz; |
|
447 } |
5582
|
448 |
5785
|
449 static octave_value_list |
|
450 octregexp (const octave_value_list &args, int nargout, const std::string &nm, |
|
451 bool case_insensitive) |
|
452 { |
|
453 octave_value_list retval; |
|
454 int nargin = args.length(); |
|
455 std::list<regexp_elem> lst; |
|
456 string_vector named; |
|
457 int nopts; |
|
458 int sz = octregexp_list (args, nm, case_insensitive, lst, named, nopts); |
|
459 |
|
460 if (! error_state) |
|
461 { |
|
462 // Converted the linked list in the correct form for the return values |
|
463 |
|
464 octave_idx_type i = 0; |
|
465 #ifdef HAVE_PCRE |
|
466 Octave_map nmap; |
|
467 if (sz == 1) |
|
468 { |
|
469 for (int j = 0; j < named.length(); j++) |
|
470 nmap.assign (named(j), lst.begin()->named_token(j)); |
|
471 retval(5) = nmap; |
|
472 } |
|
473 else |
|
474 { |
|
475 for (int j = 0; j < named.length (); j++) |
|
476 { |
|
477 i = 0; |
|
478 Cell tmp(dim_vector (1, sz)); |
|
479 for (const_iterator p = lst.begin(); p != lst.end(); p++) |
|
480 tmp(i++) = p->named_token(j); |
|
481 nmap.assign (named(j), octave_value (tmp)); |
|
482 } |
|
483 retval(5) = nmap; |
|
484 } |
|
485 #else |
|
486 retval(5) = Octave_map(); |
|
487 #endif |
|
488 |
|
489 Cell t (dim_vector(1, sz)); |
|
490 i = 0; |
|
491 for (const_iterator p = lst.begin(); p != lst.end(); p++) |
|
492 t(i++) = p->t; |
5582
|
493 retval(4) = t; |
5785
|
494 |
|
495 Cell m (dim_vector(1, sz)); |
|
496 i = 0; |
|
497 for (const_iterator p = lst.begin(); p != lst.end(); p++) |
|
498 m(i++) = p->m; |
5582
|
499 retval(3) = m; |
5785
|
500 |
|
501 |
|
502 Cell te (dim_vector(1, sz)); |
|
503 i = 0; |
|
504 for (const_iterator p = lst.begin(); p != lst.end(); p++) |
|
505 te(i++) = p->te; |
5582
|
506 retval(2) = te; |
5785
|
507 |
|
508 NDArray e (dim_vector(1, sz)); |
|
509 i = 0; |
|
510 for (const_iterator p = lst.begin(); p != lst.end(); p++) |
|
511 e(i++) = p->e; |
5582
|
512 retval(1) = e; |
5785
|
513 |
|
514 NDArray s (dim_vector(1, sz)); |
|
515 i = 0; |
|
516 for (const_iterator p = lst.begin(); p != lst.end(); p++) |
|
517 s(i++) = p->s; |
5582
|
518 retval(0) = s; |
|
519 |
|
520 // Alter the order of the output arguments |
|
521 if (nopts > 0) |
|
522 { |
|
523 int n = 0; |
|
524 octave_value_list new_retval; |
|
525 new_retval.resize(nargout); |
|
526 |
|
527 OCTAVE_LOCAL_BUFFER (int, arg_used, 6); |
5785
|
528 for (int j = 0; j < 6; j++) |
|
529 arg_used[j] = false; |
5582
|
530 |
5785
|
531 for (int j = 2; j < nargin; j++) |
5582
|
532 { |
|
533 int k = 0; |
5785
|
534 std::string str = args(j).string_value(); |
5582
|
535 std::transform (str.begin (), str.end (), str.begin (), tolower); |
5779
|
536 if (str.find("once", 0) == 0 |
|
537 || str.find("stringanchors", 0) == 0 |
|
538 || str.find("lineanchors", 0) == 0 |
|
539 || str.find("matchcase", 0) == 0 |
|
540 || str.find("ignorecase", 0) == 0 |
|
541 || str.find("dotall", 0) == 0 |
|
542 || str.find("dotexceptnewline", 0) == 0 |
|
543 || str.find("literalspacing", 0) == 0 |
|
544 || str.find("freespacing", 0) == 0 |
|
545 ) |
5582
|
546 continue; |
|
547 else if (str.find("start", 0) == 0) |
|
548 k = 0; |
|
549 else if (str.find("end", 0) == 0) |
|
550 k = 1; |
|
551 else if (str.find("tokenextents", 0) == 0) |
|
552 k = 2; |
|
553 else if (str.find("match", 0) == 0) |
|
554 k = 3; |
|
555 else if (str.find("tokens", 0) == 0) |
|
556 k = 4; |
|
557 else if (str.find("names", 0) == 0) |
|
558 k = 5; |
|
559 |
|
560 new_retval(n++) = retval(k); |
|
561 arg_used[k] = true; |
|
562 |
|
563 if (n == nargout) |
|
564 break; |
|
565 } |
|
566 |
|
567 // Fill in the rest of the arguments |
|
568 if (n < nargout) |
|
569 { |
5785
|
570 for (int j = 0; j < 6; j++) |
5582
|
571 { |
5785
|
572 if (! arg_used[j]) |
|
573 new_retval(n++) = retval(j); |
5582
|
574 } |
|
575 } |
|
576 |
|
577 retval = new_retval; |
|
578 } |
|
579 } |
|
580 |
|
581 return retval; |
|
582 } |
|
583 |
|
584 DEFUN_DLD (regexp, args, nargout, |
|
585 "-*- texinfo -*-\n\ |
|
586 @deftypefn {Loadable Function} {[@var{s}, @var{e}, @var{te}, @var{m}, @var{t}, @var{nm}] =} regexp (@var{str}, @var{pat})\n\ |
|
587 @deftypefnx {Loadable Function} {[@dots{}] =} regexp (@var{str}, @var{pat}, @var{opts}, @dots{})\n\ |
|
588 \n\ |
|
589 Regular expression string matching. Matches @var{pat} in @var{str} and\n\ |
|
590 returns the position and matching substrings or empty values if there are\n\ |
|
591 none.\n\ |
|
592 \n\ |
|
593 The matched pattern @var{pat} can include any of the standard regex\n\ |
|
594 operators, including:\n\ |
|
595 \n\ |
|
596 @table @code\n\ |
|
597 @item .\n\ |
|
598 Match any character\n\ |
|
599 @item * + ? @{@}\n\ |
|
600 Repetition operators, representing\n\ |
|
601 @table @code\n\ |
|
602 @item *\n\ |
|
603 Match zero or more times\n\ |
|
604 @item +\n\ |
|
605 Match one or more times\n\ |
|
606 @item ?\n\ |
|
607 Match zero or one times\n\ |
|
608 @item @{@}\n\ |
|
609 Match range operator, which is of the form @code{@{@var{n}@}} to match exactly\n\ |
|
610 @var{n} times, @code{@{@var{m},@}} to match @var{m} or more times,\n\ |
|
611 @code{@{@var{m},@var{n}@}} to match between @var{m} and @var{n} times.\n\ |
|
612 @end table\n\ |
|
613 @item [@dots{}] [^@dots{}]\n\ |
|
614 List operators, where for example @code{[ab]c} matches @code{ac} and @code{bc}\n\ |
|
615 @item ()\n\ |
|
616 Grouping operator\n\ |
|
617 @item |\n\ |
|
618 Alternation operator. Match one of a choice of regular expressions. The\n\ |
|
619 alternatives must be delimited by the grouoing operator @code{()} above\n\ |
|
620 @item ^ $\n\ |
|
621 Anchoring operator. @code{^} matches the start of the string @var{str} and\n\ |
|
622 @code{$} the end\n\ |
|
623 @end table\n\ |
|
624 \n\ |
|
625 In addition the following escaped characters have special meaning. It should\n\ |
|
626 be noted that it is recommended to quote @var{pat} in single quotes rather\n\ |
|
627 than double quotes, to avoid the escape sequences being interpreted by octave\n\ |
|
628 before being passed to @code{regexp}.\n\ |
|
629 \n\ |
|
630 @table @code\n\ |
|
631 @item \\b\n\ |
|
632 Match a word boundary\n\ |
|
633 @item \\B\n\ |
|
634 Match within a word\n\ |
|
635 @item \\w\n\ |
|
636 Matches any word character\n\ |
|
637 @item \\W\n\ |
|
638 Matches any non word character\n\ |
|
639 @item \\<\n\ |
|
640 Matches the beginning of a word\n\ |
|
641 @item \\>\n\ |
|
642 Matches the end of a word\n\ |
|
643 @item \\s\n\ |
|
644 Matches any whitespace character\n\ |
|
645 @item \\S\n\ |
|
646 Matches any non whitespace character\n\ |
|
647 @item \\d\n\ |
|
648 Matches any digit\n\ |
|
649 @item \\D\n\ |
|
650 Matches any non-digit\n\ |
|
651 @end table\n\ |
|
652 \n\ |
|
653 The outputs of @code{regexp} by default are in the order as given below\n\ |
|
654 \n\ |
|
655 @table @asis\n\ |
|
656 @item @var{s}\n\ |
|
657 The start indices of each of the matching substrings\n\ |
|
658 \n\ |
|
659 @item @var{e}\n\ |
|
660 The end indices of each matching substring\n\ |
|
661 \n\ |
|
662 @item @var{te}\n\ |
|
663 The extents of each of the matched token surrounded by @code{(@dots{})} in\n\ |
|
664 @var{pat}.\n\ |
|
665 \n\ |
|
666 @item @var{m}\n\ |
|
667 A cell array of the text of each match.\n\ |
|
668 \n\ |
|
669 @item @var{t}\n\ |
|
670 A cell array of the text of each token matched.\n\ |
|
671 \n\ |
|
672 @item @var{nm}\n\ |
|
673 A structure containing the text of each matched named token, with the name\n\ |
|
674 being used as the fieldname. A named token is denoted as\n\ |
|
675 @code{(?<name>@dots{})}\n\ |
|
676 @end table\n\ |
|
677 \n\ |
|
678 Particular output arguments or the order of the output arguments can be\n\ |
|
679 selected by additional @var{opts} arguments. These are strings and the\n\ |
|
680 correspondence between the output arguments and the optional argument\n\ |
|
681 are\n\ |
|
682 \n\ |
|
683 @multitable @columnfractions 0.2 0.3 0.3 0.2\n\ |
|
684 @item @tab 'start' @tab @var{s} @tab\n\ |
|
685 @item @tab 'end' @tab @var{e} @tab\n\ |
|
686 @item @tab 'tokenExtents' @tab @var{te} @tab\n\ |
|
687 @item @tab 'match' @tab @var{m} @tab\n\ |
|
688 @item @tab 'tokens' @tab @var{t} @tab\n\ |
|
689 @item @tab 'names' @tab @var{nm} @tab\n\ |
|
690 @end multitable\n\ |
|
691 \n\ |
|
692 A further optional argument is 'once', that limits the number of returned\n\ |
5779
|
693 matches to the first match. Additional arguments are\n\ |
|
694 \n\ |
|
695 @table @asis\n\ |
|
696 @item matchcase\n\ |
|
697 Make the matching case sensitive.\n\ |
|
698 @item ignorecase\n\ |
|
699 Make the matching case insensitive.\n\ |
|
700 @item stringanchors\n\ |
|
701 Match the anchor characters at the beginning and end of the string.\n\ |
|
702 @item lineanchors\n\ |
|
703 Match the anchor characters at the beginning and end of the line.\n\ |
|
704 @item dotall\n\ |
|
705 The character @code{.} matches the newline character.\n\ |
|
706 @item dotexceptnewline\n\ |
|
707 The character @code{.} matches all but the newline character.\n\ |
|
708 @item freespacing\n\ |
|
709 The pattern can include arbitrary whitespace and comments starting with\n\ |
|
710 @code{#}.\n\ |
|
711 @item literalspacing\n\ |
|
712 The pattern is taken literally.\n\ |
|
713 @end table\n\ |
5582
|
714 @end deftypefn") |
|
715 { |
|
716 return octregexp (args, nargout, "regexp", false); |
|
717 } |
|
718 |
|
719 /* |
|
720 |
|
721 ## seg-fault test |
|
722 %!assert(regexp("abcde","."),[1,2,3,4,5]) |
|
723 |
|
724 ## Check that anchoring of pattern works correctly |
|
725 %!assert(regexp('abcabc','^abc'),1); |
|
726 %!assert(regexp('abcabc','abc$'),4); |
5785
|
727 %!assert(regexp('abcabc','^abc$'),zeros(1,0)); |
5582
|
728 |
|
729 %!test |
|
730 %! [s, e, te, m, t] = regexp(' No Match ', 'f(.*)uck'); |
5785
|
731 %! assert (s,zeros(1,0)) |
|
732 %! assert (e,zeros(1,0)) |
|
733 %! assert (te,cell(1,0)) |
|
734 %! assert (m, cell(1,0)) |
|
735 %! assert (t, cell(1,0)) |
5582
|
736 |
|
737 %!test |
|
738 %! [s, e, te, m, t] = regexp(' FiRetrUck ', 'f(.*)uck'); |
5785
|
739 %! assert (s,zeros(1,0)) |
|
740 %! assert (e,zeros(1,0)) |
|
741 %! assert (te,cell(1,0)) |
|
742 %! assert (m, cell(1,0)) |
|
743 %! assert (t, cell(1,0)) |
5582
|
744 |
|
745 %!test |
|
746 %! [s, e, te, m, t] = regexp(' firetruck ', 'f(.*)uck'); |
|
747 %! assert (s,2) |
|
748 %! assert (e,10) |
|
749 %! assert (te{1},[3,7]) |
|
750 %! assert (m{1}, 'firetruck') |
|
751 %! assert (t{1}{1}, 'iretr') |
|
752 |
|
753 %!test |
|
754 %! [s, e, te, m, t] = regexp('short test string','\w*r\w*'); |
|
755 %! assert (s,[1,12]) |
|
756 %! assert (e,[5,17]) |
|
757 %! assert (size(te), [1,2]) |
|
758 %! assert (isempty(te{1})) |
|
759 %! assert (isempty(te{2})) |
|
760 %! assert (m{1},'short') |
|
761 %! assert (m{2},'string') |
|
762 %! assert (size(t), [1,2]) |
|
763 %! assert (isempty(t{1})) |
|
764 %! assert (isempty(t{2})) |
|
765 |
|
766 %!test |
|
767 %! [s, e, te, m, t] = regexp('short test string','\w*r\w*','once'); |
|
768 %! assert (s,1) |
|
769 %! assert (e,5) |
|
770 %! assert (size(te), [1,1]) |
|
771 %! assert (isempty(te{1})) |
|
772 %! assert (m{1},'short') |
|
773 %! ## Matlab gives [1,0] here but that seems wrong. |
|
774 %! assert (size(t), [1,1]) |
|
775 |
|
776 %!test |
|
777 %! [m, te, e, s, t] = regexp('short test string','\w*r\w*','once', 'match', 'tokenExtents', 'end', 'start', 'tokens'); |
|
778 %! assert (s,1) |
|
779 %! assert (e,5) |
|
780 %! assert (size(te), [1,1]) |
|
781 %! assert (isempty(te{1})) |
|
782 %! assert (m{1},'short') |
|
783 %! ## Matlab gives [1,0] here but that seems wrong. |
|
784 %! assert (size(t), [1,1]) |
|
785 |
5619
|
786 %!test |
5582
|
787 %! ## This test is expected to fail if PCRE is not installed |
5620
|
788 %! if (!isempty(findstr(octave_config_info ("DEFS"),"HAVE_PCRE"))) |
5619
|
789 %! [s, e, te, m, t, nm] = regexp('short test string','(?<word1>\w*t)\s*(?<word2>\w*t)'); |
|
790 %! assert (s,1) |
|
791 %! assert (e,10) |
|
792 %! assert (size(te), [1,1]) |
|
793 %! assert (te{1}, [1 5; 7, 10]) |
|
794 %! assert (m{1},'short test') |
|
795 %! assert (size(t),[1,1]) |
|
796 %! assert (t{1}{1},'short') |
|
797 %! assert (t{1}{2},'test') |
|
798 %! assert (size(nm), [1,1]) |
|
799 %! assert (!isempty(fieldnames(nm))) |
|
800 %! assert (sort(fieldnames(nm)),{'word1';'word2'}) |
|
801 %! assert (nm.word1,'short') |
|
802 %! assert (nm.word2,'test') |
|
803 %! endif |
5582
|
804 |
5619
|
805 %!test |
5582
|
806 %! ## This test is expected to fail if PCRE is not installed |
5620
|
807 %! if (!isempty(findstr(octave_config_info ("DEFS"),"HAVE_PCRE"))) |
5619
|
808 %! [nm, m, te, e, s, t] = regexp('short test string','(?<word1>\w*t)\s*(?<word2>\w*t)', 'names', 'match', 'tokenExtents', 'end', 'start', 'tokens'); |
|
809 %! assert (s,1) |
|
810 %! assert (e,10) |
|
811 %! assert (size(te), [1,1]) |
|
812 %! assert (te{1}, [1 5; 7, 10]) |
|
813 %! assert (m{1},'short test') |
|
814 %! assert (size(t),[1,1]) |
|
815 %! assert (t{1}{1},'short') |
|
816 %! assert (t{1}{2},'test') |
|
817 %! assert (size(nm), [1,1]) |
|
818 %! assert (!isempty(fieldnames(nm))) |
|
819 %! assert (sort(fieldnames(nm)),{'word1';'word2'}) |
|
820 %! assert (nm.word1,'short') |
|
821 %! assert (nm.word2,'test') |
|
822 %! endif |
|
823 |
|
824 %!test |
|
825 %! ## This test is expected to fail if PCRE is not installed |
5620
|
826 %! if (!isempty(findstr(octave_config_info ("DEFS"),"HAVE_PCRE"))) |
5619
|
827 %! [t, nm] = regexp("John Davis\nRogers, James",'(?<first>\w+)\s+(?<last>\w+)|(?<last>\w+),\s+(?<first>\w+)','tokens','names'); |
|
828 %! assert (size(t), [1,2]); |
|
829 %! assert (t{1}{1},'John'); |
|
830 %! assert (t{1}{2},'Davis'); |
|
831 %! assert (t{2}{1},'Rogers'); |
|
832 %! assert (t{2}{2},'James'); |
|
833 %! assert (size(nm), [1,1]); |
|
834 %! assert (nm.first{1},'John'); |
|
835 %! assert (nm.first{2},'James'); |
|
836 %! assert (nm.last{1},'Davis'); |
|
837 %! assert (nm.last{2},'Rogers'); |
|
838 %! endif |
5582
|
839 |
5779
|
840 %!assert(regexp("abc\nabc",'.'),[1:7]) |
|
841 %!assert(regexp("abc\nabc",'.','dotall'),[1:7]) |
|
842 %!test |
|
843 %! if (!isempty(findstr(octave_config_info ("DEFS"),"HAVE_PCRE"))) |
|
844 %! assert(regexp("abc\nabc",'(?s).'),[1:7]) |
|
845 %! assert(regexp("abc\nabc",'.','dotexceptnewline'),[1,2,3,5,6,7]) |
|
846 %! assert(regexp("abc\nabc",'(?-s).'),[1,2,3,5,6,7]) |
|
847 %! endif |
|
848 |
|
849 %!assert(regexp("caseCaSe",'case'),1) |
|
850 %!assert(regexp("caseCaSe",'case',"matchcase"),1) |
|
851 %!assert(regexp("caseCaSe",'case',"ignorecase"),[1,5]) |
|
852 %!test |
|
853 %! if (!isempty(findstr(octave_config_info ("DEFS"),"HAVE_PCRE"))) |
|
854 %! assert(regexp("caseCaSe",'(?-i)case'),1) |
|
855 %! assert(regexp("caseCaSe",'(?i)case'),[1,5]) |
|
856 %! endif |
|
857 |
|
858 %!assert (regexp("abc\nabc",'c$'),7) |
|
859 %!assert (regexp("abc\nabc",'c$',"stringanchors"),7) |
|
860 %!test |
|
861 %! if (!isempty(findstr(octave_config_info ("DEFS"),"HAVE_PCRE"))) |
|
862 %! assert (regexp("abc\nabc",'(?-m)c$'),7) |
|
863 %! assert (regexp("abc\nabc",'c$',"lineanchors"),[3,7]) |
|
864 %! assert (regexp("abc\nabc",'(?m)c$'),[3,7]) |
|
865 %! endif |
|
866 |
|
867 %!assert (regexp("this word",'s w'),4) |
|
868 %!assert (regexp("this word",'s w','literalspacing'),4) |
|
869 %!test |
|
870 %! if (!isempty(findstr(octave_config_info ("DEFS"),"HAVE_PCRE"))) |
|
871 %! assert (regexp("this word",'(?-x)s w','literalspacing'),4) |
5785
|
872 %! assert (regexp("this word",'s w','freespacing'),zeros(1,0)) |
|
873 %! assert (regexp("this word",'(?x)s w'),zeros(1,0)) |
5779
|
874 %! endif |
|
875 |
5582
|
876 %!error regexp('string', 'tri', 'BadArg'); |
|
877 %!error regexp('string'); |
|
878 |
|
879 */ |
|
880 |
|
881 DEFUN_DLD(regexpi, args, nargout, |
|
882 "-*- texinfo -*-\n\ |
|
883 @deftypefn {Loadable Function} {[@var{s}, @var{e}, @var{te}, @var{m}, @var{t}, @var{nm}] =} regexpi (@var{str}, @var{pat})\n\ |
|
884 @deftypefnx {Loadable Function} {[@dots{}] =} regexpi (@var{str}, @var{pat}, @var{opts}, @dots{})\n\ |
|
885 \n\ |
|
886 Case insensitive regular expression string matching. Matches @var{pat} in\n\ |
|
887 @var{str} and returns the position and matching substrings or empty values\n\ |
|
888 if there are none. See @code{regexp} for more details\n\ |
|
889 @end deftypefn") |
|
890 { |
|
891 return octregexp (args, nargout, "regexp", true); |
|
892 } |
|
893 |
|
894 /* |
|
895 |
|
896 ## seg-fault test |
|
897 %!assert(regexpi("abcde","."),[1,2,3,4,5]) |
|
898 |
|
899 ## Check that anchoring of pattern works correctly |
|
900 %!assert(regexpi('abcabc','^abc'),1); |
|
901 %!assert(regexpi('abcabc','abc$'),4); |
5785
|
902 %!assert(regexpi('abcabc','^abc$'),zeros(1,0)); |
5582
|
903 |
|
904 %!test |
|
905 %! [s, e, te, m, t] = regexpi(' No Match ', 'f(.*)uck'); |
5785
|
906 %! assert (s,zeros(1,0)) |
|
907 %! assert (e,zeros(1,0)) |
|
908 %! assert (te,cell(1,0)) |
|
909 %! assert (m, cell(1,0)) |
|
910 %! assert (t, cell(1,0)) |
5582
|
911 |
|
912 %!test |
|
913 %! [s, e, te, m, t] = regexpi(' FiRetrUck ', 'f(.*)uck'); |
|
914 %! assert (s,2) |
|
915 %! assert (e,10) |
|
916 %! assert (te{1},[3,7]) |
|
917 %! assert (m{1}, 'FiRetrUck') |
|
918 %! assert (t{1}{1}, 'iRetr') |
|
919 |
|
920 %!test |
|
921 %! [s, e, te, m, t] = regexpi(' firetruck ', 'f(.*)uck'); |
|
922 %! assert (s,2) |
|
923 %! assert (e,10) |
|
924 %! assert (te{1},[3,7]) |
|
925 %! assert (m{1}, 'firetruck') |
|
926 %! assert (t{1}{1}, 'iretr') |
|
927 |
|
928 %!test |
|
929 %! [s, e, te, m, t] = regexpi('ShoRt Test String','\w*r\w*'); |
|
930 %! assert (s,[1,12]) |
|
931 %! assert (e,[5,17]) |
|
932 %! assert (size(te), [1,2]) |
|
933 %! assert (isempty(te{1})) |
|
934 %! assert (isempty(te{2})) |
|
935 %! assert (m{1},'ShoRt') |
|
936 %! assert (m{2},'String') |
|
937 %! assert (size(t), [1,2]) |
|
938 %! assert (isempty(t{1})) |
|
939 %! assert (isempty(t{2})) |
|
940 |
|
941 %!test |
|
942 %! [s, e, te, m, t] = regexpi('ShoRt Test String','\w*r\w*','once'); |
|
943 %! assert (s,1) |
|
944 %! assert (e,5) |
|
945 %! assert (size(te), [1,1]) |
|
946 %! assert (isempty(te{1})) |
|
947 %! assert (m{1},'ShoRt') |
|
948 %! ## Matlab gives [1,0] here but that seems wrong. |
|
949 %! assert (size(t), [1,1]) |
|
950 |
|
951 %!test |
|
952 %! [m, te, e, s, t] = regexpi('ShoRt Test String','\w*r\w*','once', 'match', 'tokenExtents', 'end', 'start', 'tokens'); |
|
953 %! assert (s,1) |
|
954 %! assert (e,5) |
|
955 %! assert (size(te), [1,1]) |
|
956 %! assert (isempty(te{1})) |
|
957 %! assert (m{1},'ShoRt') |
|
958 %! ## Matlab gives [1,0] here but that seems wrong. |
|
959 %! assert (size(t), [1,1]) |
|
960 |
5619
|
961 %!test |
5582
|
962 %! ## This test is expected to fail if PCRE is not installed |
5620
|
963 %! if (!isempty(findstr(octave_config_info ("DEFS"),"HAVE_PCRE"))) |
5619
|
964 %! [s, e, te, m, t, nm] = regexpi('ShoRt Test String','(?<word1>\w*t)\s*(?<word2>\w*t)'); |
|
965 %! assert (s,1) |
|
966 %! assert (e,10) |
|
967 %! assert (size(te), [1,1]) |
|
968 %! assert (te{1}, [1 5; 7, 10]) |
|
969 %! assert (m{1},'ShoRt Test') |
|
970 %! assert (size(t),[1,1]) |
|
971 %! assert (t{1}{1},'ShoRt') |
|
972 %! assert (t{1}{2},'Test') |
|
973 %! assert (size(nm), [1,1]) |
|
974 %! assert (!isempty(fieldnames(nm))) |
|
975 %! assert (sort(fieldnames(nm)),{'word1';'word2'}) |
|
976 %! assert (nm.word1,'ShoRt') |
|
977 %! assert (nm.word2,'Test') |
|
978 %! endif |
5582
|
979 |
5619
|
980 %!test |
5582
|
981 %! ## This test is expected to fail if PCRE is not installed |
5620
|
982 %! if (!isempty(findstr(octave_config_info ("DEFS"),"HAVE_PCRE"))) |
5619
|
983 %! [nm, m, te, e, s, t] = regexpi('ShoRt Test String','(?<word1>\w*t)\s*(?<word2>\w*t)', 'names', 'match', 'tokenExtents', 'end', 'start', 'tokens'); |
|
984 %! assert (s,1) |
|
985 %! assert (e,10) |
|
986 %! assert (size(te), [1,1]) |
|
987 %! assert (te{1}, [1 5; 7, 10]) |
|
988 %! assert (m{1},'ShoRt Test') |
|
989 %! assert (size(t),[1,1]) |
|
990 %! assert (t{1}{1},'ShoRt') |
|
991 %! assert (t{1}{2},'Test') |
|
992 %! assert (size(nm), [1,1]) |
|
993 %! assert (!isempty(fieldnames(nm))) |
|
994 %! assert (sort(fieldnames(nm)),{'word1';'word2'}) |
|
995 %! assert (nm.word1,'ShoRt') |
|
996 %! assert (nm.word2,'Test') |
|
997 %! endif |
5582
|
998 |
5779
|
999 %!assert(regexpi("abc\nabc",'.'),[1:7]) |
|
1000 %!assert(regexpi("abc\nabc",'.','dotall'),[1:7]) |
|
1001 %!test |
|
1002 %! if (!isempty(findstr(octave_config_info ("DEFS"),"HAVE_PCRE"))) |
|
1003 %! assert(regexpi("abc\nabc",'(?s).'),[1:7]) |
|
1004 %! assert(regexpi("abc\nabc",'.','dotexceptnewline'),[1,2,3,5,6,7]) |
|
1005 %! assert(regexpi("abc\nabc",'(?-s).'),[1,2,3,5,6,7]) |
|
1006 %! endif |
|
1007 |
|
1008 %!assert(regexpi("caseCaSe",'case'),[1,5]) |
|
1009 %!assert(regexpi("caseCaSe",'case',"matchcase"),1) |
|
1010 %!assert(regexpi("caseCaSe",'case',"ignorecase"),[1,5]) |
|
1011 %!test |
|
1012 %! if (!isempty(findstr(octave_config_info ("DEFS"),"HAVE_PCRE"))) |
|
1013 %! assert(regexpi("caseCaSe",'(?-i)case'),1) |
|
1014 %! assert(regexpi("caseCaSe",'(?i)case'),[1,5]) |
|
1015 %! endif |
|
1016 |
|
1017 %!assert (regexpi("abc\nabc",'c$'),7) |
|
1018 %!assert (regexpi("abc\nabc",'c$',"stringanchors"),7) |
|
1019 %!test |
|
1020 %! if (!isempty(findstr(octave_config_info ("DEFS"),"HAVE_PCRE"))) |
|
1021 %! assert (regexpi("abc\nabc",'(?-m)c$'),7) |
|
1022 %! assert (regexpi("abc\nabc",'c$',"lineanchors"),[3,7]) |
|
1023 %! assert (regexpi("abc\nabc",'(?m)c$'),[3,7]) |
|
1024 %! endif |
|
1025 |
|
1026 %!assert (regexpi("this word",'s w'),4) |
|
1027 %!assert (regexpi("this word",'s w','literalspacing'),4) |
|
1028 %!test |
|
1029 %! if (!isempty(findstr(octave_config_info ("DEFS"),"HAVE_PCRE"))) |
|
1030 %! assert (regexpi("this word",'(?-x)s w','literalspacing'),4) |
5785
|
1031 %! assert (regexpi("this word",'s w','freespacing'),zeros(1,0)) |
|
1032 %! assert (regexpi("this word",'(?x)s w'),zeros(1,0)) |
5779
|
1033 %! endif |
|
1034 |
5582
|
1035 %!error regexpi('string', 'tri', 'BadArg'); |
|
1036 %!error regexpi('string'); |
|
1037 |
|
1038 */ |
|
1039 |
5785
|
1040 DEFUN_DLD(regexprep, args, , |
|
1041 "-*- texinfo -*-\n\ |
|
1042 @deftypefn {Function File} @var{string} = regexprep(@var{string}, @var{pat}, @var{repstr}, @var{options})\n\ |
|
1043 Replace matches of @var{pat} in @var{string} with @var{repstr}.\n\ |
|
1044 \n\ |
|
1045 \n\ |
|
1046 The replacement can contain @code{$i}, which subsubstitutes\n\ |
|
1047 for the ith set of parentheses in the match string. E.g.,\n\ |
|
1048 @example\n\ |
|
1049 \n\ |
|
1050 regexprep(\"Bill Dunn\",'(\\w+) (\\w+)','$2, $1')\n\ |
|
1051 \n\ |
|
1052 @end example\n\ |
|
1053 returns \"Dunn, Bill\"\n\ |
|
1054 \n\ |
|
1055 @var{options} may be zero or more of\n\ |
|
1056 @table @samp\n\ |
|
1057 \n\ |
|
1058 @item once\n\ |
|
1059 Replace only the first occurance of @var{pat} in the result.\n\ |
|
1060 \n\ |
|
1061 @item warnings\n\ |
|
1062 This option is present for compatibility but is ignored.\n\ |
|
1063 \n\ |
|
1064 @item ignorecase or matchcase\n\ |
|
1065 Ignore case for the pattern matching (see @code{regexpi}).\n\ |
|
1066 Alternatively, use (?i) or (?-i) in the pattern.\n\ |
|
1067 \n\ |
|
1068 @item lineanchors and stringanchors\n\ |
|
1069 Whether characters ^ and $ match the beginning and ending of lines.\n\ |
|
1070 Alternatively, use (?m) or (?-m) in the pattern.\n\ |
|
1071 \n\ |
|
1072 @item dotexceptnewline and dotall\n\ |
|
1073 Whether . matches newlines in the string.\n\ |
|
1074 Alternatively, use (?s) or (?-s) in the pattern.\n\ |
|
1075 \n\ |
|
1076 @item freespacing or literalspacing\n\ |
|
1077 Whether whitespace and # comments can be used to make the regular expression more readable.\n\ |
|
1078 Alternatively, use (?x) or (?-x) in the pattern.\n\ |
|
1079 \n\ |
|
1080 @end table\n\ |
|
1081 @seealso{regexp,regexpi}\n\ |
|
1082 @end deftypefn") |
|
1083 { |
|
1084 octave_value_list retval; |
|
1085 |
|
1086 int nargin = args.length(); |
|
1087 |
|
1088 if (nargin < 3) |
|
1089 { |
5823
|
1090 print_usage (); |
5785
|
1091 return retval; |
|
1092 } |
|
1093 |
|
1094 // Make sure we have string,pattern,replacement |
|
1095 const std::string buffer = args(0).string_value (); |
|
1096 if (error_state) return retval; |
|
1097 const std::string pattern = args(1).string_value (); |
|
1098 if (error_state) return retval; |
|
1099 const std::string replacement = args(2).string_value (); |
|
1100 if (error_state) return retval; |
|
1101 |
|
1102 // Pack options excluding 'tokenize' and various output |
|
1103 // reordering strings into regexp arg list |
|
1104 octave_value_list regexpargs(nargin-1,octave_value()); |
|
1105 regexpargs(0) = args(0); |
|
1106 regexpargs(1) = args(1); |
|
1107 int len=2; |
|
1108 for (int i = 3; i < nargin; i++) |
|
1109 { |
|
1110 const std::string opt = args(i).string_value(); |
|
1111 if (opt != "tokenize" && opt != "start" && opt != "end" |
|
1112 && opt != "tokenextents" && opt != "match" && opt != "tokens" |
|
1113 && opt != "names" && opt != "warnings") |
|
1114 { |
|
1115 regexpargs(len++) = args(i); |
|
1116 } |
|
1117 } |
|
1118 regexpargs.resize(len); |
|
1119 |
|
1120 // Identify replacement tokens; build a vector of group numbers in |
|
1121 // the replacement string so that we can quickly calculate the size |
|
1122 // of the replacement. |
|
1123 int tokens = 0; |
|
1124 for (size_t i=1; i < replacement.size(); i++) |
|
1125 { |
|
1126 if (replacement[i-1]=='$' && isdigit(replacement[i])) |
|
1127 { |
|
1128 tokens++, i++; |
|
1129 } |
|
1130 } |
|
1131 std::vector<int> token(tokens); |
|
1132 int kk = 0; |
|
1133 for (size_t i = 1; i < replacement.size(); i++) |
|
1134 { |
|
1135 if (replacement[i-1]=='$' && isdigit(replacement[i])) |
|
1136 { |
|
1137 token[kk++] = replacement[i]-'0'; |
|
1138 i++; |
|
1139 } |
|
1140 } |
|
1141 |
|
1142 // Perform replacement |
|
1143 std::string rep; |
|
1144 if (tokens > 0) |
|
1145 { |
|
1146 std::list<regexp_elem> lst; |
|
1147 string_vector named; |
|
1148 int nopts; |
|
1149 int sz = octregexp_list (regexpargs, "regexprep", false, lst, named, |
|
1150 nopts); |
|
1151 |
|
1152 if (error_state) |
|
1153 return retval; |
|
1154 if (sz == 0) |
|
1155 { |
|
1156 retval(0) = args(0); |
|
1157 return retval; |
|
1158 } |
|
1159 |
|
1160 // Determine replacement length |
|
1161 const size_t replen = replacement.size() - 2*tokens; |
|
1162 int delta = 0; |
|
1163 const_iterator p = lst.begin(); |
|
1164 for (int i = 0; i < sz; i++) |
|
1165 { |
|
1166 OCTAVE_QUIT; |
|
1167 |
|
1168 const Matrix pairs(p->te); |
|
1169 size_t pairlen = 0; |
|
1170 for (int j = 0; j < tokens; j++) |
|
1171 { |
|
1172 if (token[j] == 0) |
|
1173 pairlen += static_cast<size_t>(p->e - p->s) + 1; |
|
1174 else if (token[j] <= pairs.rows()) |
|
1175 pairlen += static_cast<size_t>(pairs(token[j]-1,1) - |
|
1176 pairs(token[j]-1,0)) + 1; |
|
1177 } |
|
1178 delta += static_cast<int>(replen + pairlen) - |
|
1179 static_cast<int>(p->e - p->s + 1); |
|
1180 p++; |
|
1181 } |
|
1182 |
|
1183 // Build replacement string |
|
1184 rep.reserve(buffer.size()+delta); |
|
1185 size_t from = 0; |
|
1186 p = lst.begin(); |
|
1187 for (int i=0; i < sz; i++) |
|
1188 { |
|
1189 OCTAVE_QUIT; |
|
1190 |
|
1191 const Matrix pairs(p->te); |
|
1192 rep.append(&buffer[from], static_cast<size_t>(p->s - 1) - from); |
|
1193 from = static_cast<size_t>(p->e - 1) + 1; |
|
1194 for (size_t j = 1; j < replacement.size(); j++) |
|
1195 { |
|
1196 if (replacement[j-1]=='$' && isdigit(replacement[j])) |
|
1197 { |
|
1198 int k = replacement[j]-'0'; |
|
1199 if (k == 0) |
|
1200 { |
|
1201 // replace with entire match |
|
1202 rep.append(&buffer[static_cast<size_t>(p->e - 1)], |
|
1203 static_cast<size_t>(p->e - p->s) + 1); |
|
1204 } |
|
1205 else if (k <= pairs.rows()) |
|
1206 { |
|
1207 // replace with group capture |
|
1208 rep.append(&buffer[static_cast<size_t>(pairs(k-1,0)-1)], |
|
1209 static_cast<size_t>(pairs(k-1,1) - |
|
1210 pairs(k-1,0))+1); |
|
1211 } |
|
1212 else |
|
1213 { |
|
1214 // replace with nothing |
|
1215 } |
|
1216 j++; |
|
1217 } |
|
1218 else |
|
1219 { |
|
1220 rep.append(1,replacement[j-1]); |
|
1221 } |
|
1222 if (j+1 == replacement.size()) |
|
1223 { |
|
1224 rep.append(1,replacement[j]); |
|
1225 } |
|
1226 } |
|
1227 p++; |
|
1228 } |
|
1229 rep.append(&buffer[from],buffer.size()-from); |
|
1230 } |
|
1231 else |
|
1232 { |
|
1233 std::list<regexp_elem> lst; |
|
1234 string_vector named; |
|
1235 int nopts; |
|
1236 int sz = octregexp_list (regexpargs, "regexprep", false, lst, named, |
|
1237 nopts); |
|
1238 |
|
1239 if (error_state) |
|
1240 return retval; |
|
1241 if (sz == 0) |
|
1242 { |
|
1243 retval(0) = args(0); |
|
1244 return retval; |
|
1245 } |
|
1246 |
|
1247 // Determine replacement length |
|
1248 const size_t replen = replacement.size(); |
|
1249 int delta = 0; |
|
1250 const_iterator p = lst.begin(); |
|
1251 for (int i = 0; i < sz; i++) |
|
1252 { |
|
1253 OCTAVE_QUIT; |
|
1254 delta += static_cast<int>(replen) - |
|
1255 static_cast<int>(p->e - p->s + 1); |
|
1256 p++; |
|
1257 } |
|
1258 |
|
1259 // Build replacement string |
|
1260 rep.reserve(buffer.size()+delta); |
|
1261 size_t from = 0; |
|
1262 p = lst.begin(); |
|
1263 for (int i=0; i < sz; i++) |
|
1264 { |
|
1265 OCTAVE_QUIT; |
|
1266 rep.append(&buffer[from], static_cast<size_t>(p->s - 1) - from); |
|
1267 from = static_cast<size_t>(p->e - 1) + 1; |
|
1268 rep.append(replacement); |
|
1269 p++; |
|
1270 } |
|
1271 rep.append(&buffer[from],buffer.size()-from); |
|
1272 } |
|
1273 |
|
1274 retval(0) = rep; |
|
1275 return retval; |
|
1276 } |
|
1277 |
|
1278 /* |
|
1279 %!test # Replace with empty |
|
1280 %! xml = '<!-- This is some XML --> <tag v="hello">some stuff<!-- sample tag--></tag>'; |
|
1281 %! t = regexprep(xml,'<[!?][^>]*>',''); |
|
1282 %! assert(t,' <tag v="hello">some stuff</tag>') |
|
1283 |
|
1284 %!test # Replace with non-empty |
|
1285 %! xml = '<!-- This is some XML --> <tag v="hello">some stuff<!-- sample tag--></tag>'; |
|
1286 %! t = regexprep(xml,'<[!?][^>]*>','?'); |
|
1287 %! assert(t,'? <tag v="hello">some stuff?</tag>') |
|
1288 |
|
1289 %!test # Check that 'tokenize' is ignored |
|
1290 %! xml = '<!-- This is some XML --> <tag v="hello">some stuff<!-- sample tag--></tag>'; |
|
1291 %! t = regexprep(xml,'<[!?][^>]*>','','tokenize'); |
|
1292 %! assert(t,' <tag v="hello">some stuff</tag>') |
|
1293 |
|
1294 %!test # Capture replacement |
|
1295 %! if (!isempty(findstr(octave_config_info ("DEFS"),"HAVE_PCRE"))) |
|
1296 %! data = "Bob Smith\nDavid Hollerith\nSam Jenkins"; |
|
1297 %! result = "Smith, Bob\nHollerith, David\nJenkins, Sam"; |
|
1298 %! t = regexprep(data,'(?m)^(\w+)\s+(\w+)$','$2, $1'); |
|
1299 %! assert(t,result) |
|
1300 %! end |
|
1301 |
|
1302 # Return the original if no match |
|
1303 %!assert(regexprep('hello','world','earth'),'hello') |
|
1304 |
|
1305 ## Test a general replacement |
|
1306 %!assert(regexprep("a[b]c{d}e-f=g", "[^A-Za-z0-9_]", "_"), "a_b_c_d_e_f_g"); |
|
1307 |
|
1308 ## Make sure it works at the beginning and end |
|
1309 %!assert(regexprep("a[b]c{d}e-f=g", "a", "_"), "_[b]c{d}e-f=g"); |
|
1310 %!assert(regexprep("a[b]c{d}e-f=g", "g", "_"), "a[b]c{d}e-f=_"); |
|
1311 |
|
1312 ## Options |
|
1313 %!assert(regexprep("a[b]c{d}e-f=g", "[^A-Za-z0-9_]", "_", "once"), "a_b]c{d}e-f=g"); |
|
1314 %!assert(regexprep("a[b]c{d}e-f=g", "[^A-Z0-9_]", "_", "ignorecase"), "a_b_c_d_e_f_g"); |
|
1315 |
|
1316 ## Option combinations |
|
1317 %!assert(regexprep("a[b]c{d}e-f=g", "[^A-Z0-9_]", "_", "once", "ignorecase"), "a_b]c{d}e-f=g"); |
|
1318 |
|
1319 ## End conditions on replacement |
|
1320 %!assert(regexprep("abc","(b)",".$1"),"a.bc"); |
|
1321 %!assert(regexprep("abc","(b)","$1"),"abc"); |
|
1322 %!assert(regexprep("abc","(b)","$1."),"ab.c"); |
|
1323 %!assert(regexprep("abc","(b)","$1.."),"ab..c"); |
|
1324 |
|
1325 */ |
|
1326 |
5582
|
1327 /* |
|
1328 ;;; Local Variables: *** |
|
1329 ;;; mode: C++ *** |
|
1330 ;;; End: *** |
|
1331 */ |