annotate src/DLD-FUNCTIONS/regexp.cc @ 13759:c4b6ea833fa5

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