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