annotate src/DLD-FUNCTIONS/regexp.cc @ 14024:fc9f204faea0

refactor regexp (bug #34440) * liboctave/regexp.h, liboctave/regexp.cc: New files. Provide classes and functions for regular expressions. Adapted from src/DLD-FUNCTIONS/regexp.cc. * regex-match.h, regex-match.cc: Delete * liboctave/Makefile.am (INCS, LIBOCTAVE_CXX_SOURCES): Update. * variables.cc (name_matches_any_pattern): Use new regexp class. * symtab.h (symbol_table::regexp_global_variables, symbol_table::do_clear_variable_regexp, symbol_table::do_regexp): Likewise. * DLD-FUNCTIONS/regexp.cc (parse_options): New function. (octregexp, octcellregexp, octregexprep): Extract matching code for use in new regexp class. Use new regexp class to provide required functionality.
author John W. Eaton <jwe@octave.org>
date Sun, 11 Dec 2011 22:19:57 -0500
parents 9cae456085c2
children 72c96de7a403
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
14024
fc9f204faea0 refactor regexp (bug #34440)
John W. Eaton <jwe@octave.org>
parents: 13929
diff changeset
28 #include <list>
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 <pcre.h>
6bf56668b01a [project @ 2005-12-15 01:08:20 by jwe]
jwe
parents:
diff changeset
32
14024
fc9f204faea0 refactor regexp (bug #34440)
John W. Eaton <jwe@octave.org>
parents: 13929
diff changeset
33 #include "base-list.h"
fc9f204faea0 refactor regexp (bug #34440)
John W. Eaton <jwe@octave.org>
parents: 13929
diff changeset
34 #include "oct-locbuf.h"
fc9f204faea0 refactor regexp (bug #34440)
John W. Eaton <jwe@octave.org>
parents: 13929
diff changeset
35 #include "quit.h"
fc9f204faea0 refactor regexp (bug #34440)
John W. Eaton <jwe@octave.org>
parents: 13929
diff changeset
36 #include "regexp.h"
fc9f204faea0 refactor regexp (bug #34440)
John W. Eaton <jwe@octave.org>
parents: 13929
diff changeset
37 #include "str-vec.h"
8093
dcc31f473596 Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents: 8021
diff changeset
38
14024
fc9f204faea0 refactor regexp (bug #34440)
John W. Eaton <jwe@octave.org>
parents: 13929
diff changeset
39 #include "defun-dld.h"
fc9f204faea0 refactor regexp (bug #34440)
John W. Eaton <jwe@octave.org>
parents: 13929
diff changeset
40 #include "Cell.h"
fc9f204faea0 refactor regexp (bug #34440)
John W. Eaton <jwe@octave.org>
parents: 13929
diff changeset
41 #include "error.h"
fc9f204faea0 refactor regexp (bug #34440)
John W. Eaton <jwe@octave.org>
parents: 13929
diff changeset
42 #include "gripes.h"
fc9f204faea0 refactor regexp (bug #34440)
John W. Eaton <jwe@octave.org>
parents: 13929
diff changeset
43 #include "oct-map.h"
fc9f204faea0 refactor regexp (bug #34440)
John W. Eaton <jwe@octave.org>
parents: 13929
diff changeset
44 #include "oct-obj.h"
fc9f204faea0 refactor regexp (bug #34440)
John W. Eaton <jwe@octave.org>
parents: 13929
diff changeset
45 #include "utils.h"
5785
6b9cec830d72 [project @ 2006-05-03 19:32:46 by dbateman]
dbateman
parents: 5779
diff changeset
46
14024
fc9f204faea0 refactor regexp (bug #34440)
John W. Eaton <jwe@octave.org>
parents: 13929
diff changeset
47 static void
fc9f204faea0 refactor regexp (bug #34440)
John W. Eaton <jwe@octave.org>
parents: 13929
diff changeset
48 parse_options (regexp::opts& options, const octave_value_list& args,
fc9f204faea0 refactor regexp (bug #34440)
John W. Eaton <jwe@octave.org>
parents: 13929
diff changeset
49 const std::string& who, int skip, bool& extra_args)
fc9f204faea0 refactor regexp (bug #34440)
John W. Eaton <jwe@octave.org>
parents: 13929
diff changeset
50 {
fc9f204faea0 refactor regexp (bug #34440)
John W. Eaton <jwe@octave.org>
parents: 13929
diff changeset
51 int nargin = args.length ();
5582
6bf56668b01a [project @ 2005-12-15 01:08:20 by jwe]
jwe
parents:
diff changeset
52
14024
fc9f204faea0 refactor regexp (bug #34440)
John W. Eaton <jwe@octave.org>
parents: 13929
diff changeset
53 extra_args = false;
13227
9559417aa965 maint: regexp.cc style fixes
John W. Eaton <jwe@octave.org>
parents: 12642
diff changeset
54
14024
fc9f204faea0 refactor regexp (bug #34440)
John W. Eaton <jwe@octave.org>
parents: 13929
diff changeset
55 for (int i = skip; i < nargin; i++)
5582
6bf56668b01a [project @ 2005-12-15 01:08:20 by jwe]
jwe
parents:
diff changeset
56 {
13227
9559417aa965 maint: regexp.cc style fixes
John W. Eaton <jwe@octave.org>
parents: 12642
diff changeset
57 std::string str = args(i).string_value ();
9559417aa965 maint: regexp.cc style fixes
John W. Eaton <jwe@octave.org>
parents: 12642
diff changeset
58
5582
6bf56668b01a [project @ 2005-12-15 01:08:20 by jwe]
jwe
parents:
diff changeset
59 if (error_state)
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9064
diff changeset
60 {
14024
fc9f204faea0 refactor regexp (bug #34440)
John W. Eaton <jwe@octave.org>
parents: 13929
diff changeset
61 error ("%s: optional arguments must be character strings",
fc9f204faea0 refactor regexp (bug #34440)
John W. Eaton <jwe@octave.org>
parents: 13929
diff changeset
62 who.c_str ());
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9064
diff changeset
63 break;
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9064
diff changeset
64 }
13227
9559417aa965 maint: regexp.cc style fixes
John W. Eaton <jwe@octave.org>
parents: 12642
diff changeset
65
5582
6bf56668b01a [project @ 2005-12-15 01:08:20 by jwe]
jwe
parents:
diff changeset
66 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
67
9559417aa965 maint: regexp.cc style fixes
John W. Eaton <jwe@octave.org>
parents: 12642
diff changeset
68 if (str.find ("once", 0) == 0)
14024
fc9f204faea0 refactor regexp (bug #34440)
John W. Eaton <jwe@octave.org>
parents: 13929
diff changeset
69 options.once (true);
13227
9559417aa965 maint: regexp.cc style fixes
John W. Eaton <jwe@octave.org>
parents: 12642
diff changeset
70 else if (str.find ("matchcase", 0) == 0)
14024
fc9f204faea0 refactor regexp (bug #34440)
John W. Eaton <jwe@octave.org>
parents: 13929
diff changeset
71 options.case_insensitive (false);
13227
9559417aa965 maint: regexp.cc style fixes
John W. Eaton <jwe@octave.org>
parents: 12642
diff changeset
72 else if (str.find ("ignorecase", 0) == 0)
14024
fc9f204faea0 refactor regexp (bug #34440)
John W. Eaton <jwe@octave.org>
parents: 13929
diff changeset
73 options.case_insensitive (true);
13227
9559417aa965 maint: regexp.cc style fixes
John W. Eaton <jwe@octave.org>
parents: 12642
diff changeset
74 else if (str.find ("dotall", 0) == 0)
14024
fc9f204faea0 refactor regexp (bug #34440)
John W. Eaton <jwe@octave.org>
parents: 13929
diff changeset
75 options.dotexceptnewline (false);
13227
9559417aa965 maint: regexp.cc style fixes
John W. Eaton <jwe@octave.org>
parents: 12642
diff changeset
76 else if (str.find ("stringanchors", 0) == 0)
14024
fc9f204faea0 refactor regexp (bug #34440)
John W. Eaton <jwe@octave.org>
parents: 13929
diff changeset
77 options.lineanchors (false);
13227
9559417aa965 maint: regexp.cc style fixes
John W. Eaton <jwe@octave.org>
parents: 12642
diff changeset
78 else if (str.find ("literalspacing", 0) == 0)
14024
fc9f204faea0 refactor regexp (bug #34440)
John W. Eaton <jwe@octave.org>
parents: 13929
diff changeset
79 options.freespacing (false);
13227
9559417aa965 maint: regexp.cc style fixes
John W. Eaton <jwe@octave.org>
parents: 12642
diff changeset
80 else if (str.find ("dotexceptnewline", 0) == 0)
14024
fc9f204faea0 refactor regexp (bug #34440)
John W. Eaton <jwe@octave.org>
parents: 13929
diff changeset
81 options.dotexceptnewline (true);
13227
9559417aa965 maint: regexp.cc style fixes
John W. Eaton <jwe@octave.org>
parents: 12642
diff changeset
82 else if (str.find ("lineanchors", 0) == 0)
14024
fc9f204faea0 refactor regexp (bug #34440)
John W. Eaton <jwe@octave.org>
parents: 13929
diff changeset
83 options.lineanchors (true);
13227
9559417aa965 maint: regexp.cc style fixes
John W. Eaton <jwe@octave.org>
parents: 12642
diff changeset
84 else if (str.find ("freespacing", 0) == 0)
14024
fc9f204faea0 refactor regexp (bug #34440)
John W. Eaton <jwe@octave.org>
parents: 13929
diff changeset
85 options.freespacing (true);
fc9f204faea0 refactor regexp (bug #34440)
John W. Eaton <jwe@octave.org>
parents: 13929
diff changeset
86 else if (str.find ("start", 0) == 0
fc9f204faea0 refactor regexp (bug #34440)
John W. Eaton <jwe@octave.org>
parents: 13929
diff changeset
87 || str.find ("end", 0) == 0
fc9f204faea0 refactor regexp (bug #34440)
John W. Eaton <jwe@octave.org>
parents: 13929
diff changeset
88 || str.find ("tokenextents", 0) == 0
fc9f204faea0 refactor regexp (bug #34440)
John W. Eaton <jwe@octave.org>
parents: 13929
diff changeset
89 || str.find ("match", 0) == 0
fc9f204faea0 refactor regexp (bug #34440)
John W. Eaton <jwe@octave.org>
parents: 13929
diff changeset
90 || str.find ("tokens", 0) == 0
fc9f204faea0 refactor regexp (bug #34440)
John W. Eaton <jwe@octave.org>
parents: 13929
diff changeset
91 || str.find ("names", 0) == 0
fc9f204faea0 refactor regexp (bug #34440)
John W. Eaton <jwe@octave.org>
parents: 13929
diff changeset
92 || str.find ("split", 0) == 0)
fc9f204faea0 refactor regexp (bug #34440)
John W. Eaton <jwe@octave.org>
parents: 13929
diff changeset
93 extra_args = true;
fc9f204faea0 refactor regexp (bug #34440)
John W. Eaton <jwe@octave.org>
parents: 13929
diff changeset
94 else
fc9f204faea0 refactor regexp (bug #34440)
John W. Eaton <jwe@octave.org>
parents: 13929
diff changeset
95 error ("%s: unrecognized option", who.c_str ());
5582
6bf56668b01a [project @ 2005-12-15 01:08:20 by jwe]
jwe
parents:
diff changeset
96 }
5785
6b9cec830d72 [project @ 2006-05-03 19:32:46 by dbateman]
dbateman
parents: 5779
diff changeset
97 }
5582
6bf56668b01a [project @ 2005-12-15 01:08:20 by jwe]
jwe
parents:
diff changeset
98
5785
6b9cec830d72 [project @ 2006-05-03 19:32:46 by dbateman]
dbateman
parents: 5779
diff changeset
99 static octave_value_list
14024
fc9f204faea0 refactor regexp (bug #34440)
John W. Eaton <jwe@octave.org>
parents: 13929
diff changeset
100 octregexp (const octave_value_list &args, int nargout,
fc9f204faea0 refactor regexp (bug #34440)
John W. Eaton <jwe@octave.org>
parents: 13929
diff changeset
101 const std::string &who, bool case_insensitive = false)
5785
6b9cec830d72 [project @ 2006-05-03 19:32:46 by dbateman]
dbateman
parents: 5779
diff changeset
102 {
6b9cec830d72 [project @ 2006-05-03 19:32:46 by dbateman]
dbateman
parents: 5779
diff changeset
103 octave_value_list retval;
14024
fc9f204faea0 refactor regexp (bug #34440)
John W. Eaton <jwe@octave.org>
parents: 13929
diff changeset
104
13227
9559417aa965 maint: regexp.cc style fixes
John W. Eaton <jwe@octave.org>
parents: 12642
diff changeset
105 int nargin = args.length ();
14024
fc9f204faea0 refactor regexp (bug #34440)
John W. Eaton <jwe@octave.org>
parents: 13929
diff changeset
106
fc9f204faea0 refactor regexp (bug #34440)
John W. Eaton <jwe@octave.org>
parents: 13929
diff changeset
107 // Make sure we have string, pattern
fc9f204faea0 refactor regexp (bug #34440)
John W. Eaton <jwe@octave.org>
parents: 13929
diff changeset
108 const std::string buffer = args(0).string_value ();
fc9f204faea0 refactor regexp (bug #34440)
John W. Eaton <jwe@octave.org>
parents: 13929
diff changeset
109 if (error_state)
fc9f204faea0 refactor regexp (bug #34440)
John W. Eaton <jwe@octave.org>
parents: 13929
diff changeset
110 return retval;
fc9f204faea0 refactor regexp (bug #34440)
John W. Eaton <jwe@octave.org>
parents: 13929
diff changeset
111
fc9f204faea0 refactor regexp (bug #34440)
John W. Eaton <jwe@octave.org>
parents: 13929
diff changeset
112 const std::string pattern = args(1).string_value ();
fc9f204faea0 refactor regexp (bug #34440)
John W. Eaton <jwe@octave.org>
parents: 13929
diff changeset
113 if (error_state)
fc9f204faea0 refactor regexp (bug #34440)
John W. Eaton <jwe@octave.org>
parents: 13929
diff changeset
114 return retval;
13227
9559417aa965 maint: regexp.cc style fixes
John W. Eaton <jwe@octave.org>
parents: 12642
diff changeset
115
14024
fc9f204faea0 refactor regexp (bug #34440)
John W. Eaton <jwe@octave.org>
parents: 13929
diff changeset
116 regexp::opts options;
fc9f204faea0 refactor regexp (bug #34440)
John W. Eaton <jwe@octave.org>
parents: 13929
diff changeset
117 options.case_insensitive (case_insensitive);
fc9f204faea0 refactor regexp (bug #34440)
John W. Eaton <jwe@octave.org>
parents: 13929
diff changeset
118 bool extra_options = false;
fc9f204faea0 refactor regexp (bug #34440)
John W. Eaton <jwe@octave.org>
parents: 13929
diff changeset
119 parse_options (options, args, who, 2, extra_options);
fc9f204faea0 refactor regexp (bug #34440)
John W. Eaton <jwe@octave.org>
parents: 13929
diff changeset
120 if (error_state)
fc9f204faea0 refactor regexp (bug #34440)
John W. Eaton <jwe@octave.org>
parents: 13929
diff changeset
121 return retval;
fc9f204faea0 refactor regexp (bug #34440)
John W. Eaton <jwe@octave.org>
parents: 13929
diff changeset
122
fc9f204faea0 refactor regexp (bug #34440)
John W. Eaton <jwe@octave.org>
parents: 13929
diff changeset
123 regexp::match_data rx_lst = regexp_match (pattern, buffer, options, who);
fc9f204faea0 refactor regexp (bug #34440)
John W. Eaton <jwe@octave.org>
parents: 13929
diff changeset
124
fc9f204faea0 refactor regexp (bug #34440)
John W. Eaton <jwe@octave.org>
parents: 13929
diff changeset
125 string_vector named_pats = rx_lst.named_patterns ();
fc9f204faea0 refactor regexp (bug #34440)
John W. Eaton <jwe@octave.org>
parents: 13929
diff changeset
126
fc9f204faea0 refactor regexp (bug #34440)
John W. Eaton <jwe@octave.org>
parents: 13929
diff changeset
127 size_t sz = rx_lst.size ();
5785
6b9cec830d72 [project @ 2006-05-03 19:32:46 by dbateman]
dbateman
parents: 5779
diff changeset
128
6b9cec830d72 [project @ 2006-05-03 19:32:46 by dbateman]
dbateman
parents: 5779
diff changeset
129 if (! error_state)
6b9cec830d72 [project @ 2006-05-03 19:32:46 by dbateman]
dbateman
parents: 5779
diff changeset
130 {
6b9cec830d72 [project @ 2006-05-03 19:32:46 by dbateman]
dbateman
parents: 5779
diff changeset
131 // 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
132
6b9cec830d72 [project @ 2006-05-03 19:32:46 by dbateman]
dbateman
parents: 5779
diff changeset
133 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
134 octave_scalar_map nmap;
12464
dfeea9cae79e require PCRE to build Octave
John W. Eaton <jwe@octave.org>
parents: 12462
diff changeset
135
13310
583940a28bfd handle "split" option for regexp
John W. Eaton <jwe@octave.org>
parents: 13227
diff changeset
136 retval.resize (7);
583940a28bfd handle "split" option for regexp
John W. Eaton <jwe@octave.org>
parents: 13227
diff changeset
137
5785
6b9cec830d72 [project @ 2006-05-03 19:32:46 by dbateman]
dbateman
parents: 5779
diff changeset
138 if (sz == 1)
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9064
diff changeset
139 {
14024
fc9f204faea0 refactor regexp (bug #34440)
John W. Eaton <jwe@octave.org>
parents: 13929
diff changeset
140 string_vector named_tokens = rx_lst.begin()->named_tokens ();
fc9f204faea0 refactor regexp (bug #34440)
John W. Eaton <jwe@octave.org>
parents: 13929
diff changeset
141
fc9f204faea0 refactor regexp (bug #34440)
John W. Eaton <jwe@octave.org>
parents: 13929
diff changeset
142 for (int j = 0; j < named_pats.length (); j++)
fc9f204faea0 refactor regexp (bug #34440)
John W. Eaton <jwe@octave.org>
parents: 13929
diff changeset
143 nmap.assign (named_pats(j), named_tokens(j));
13227
9559417aa965 maint: regexp.cc style fixes
John W. Eaton <jwe@octave.org>
parents: 12642
diff changeset
144
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9064
diff changeset
145 retval(5) = nmap;
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9064
diff changeset
146 }
5785
6b9cec830d72 [project @ 2006-05-03 19:32:46 by dbateman]
dbateman
parents: 5779
diff changeset
147 else
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9064
diff changeset
148 {
14024
fc9f204faea0 refactor regexp (bug #34440)
John W. Eaton <jwe@octave.org>
parents: 13929
diff changeset
149 for (int j = 0; j < named_pats.length (); j++)
10154
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 Cell tmp (dim_vector (1, sz));
9559417aa965 maint: regexp.cc style fixes
John W. Eaton <jwe@octave.org>
parents: 12642
diff changeset
152
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9064
diff changeset
153 i = 0;
14024
fc9f204faea0 refactor regexp (bug #34440)
John W. Eaton <jwe@octave.org>
parents: 13929
diff changeset
154 for (regexp::match_data::const_iterator p = rx_lst.begin ();
fc9f204faea0 refactor regexp (bug #34440)
John W. Eaton <jwe@octave.org>
parents: 13929
diff changeset
155 p != rx_lst.end (); p++)
fc9f204faea0 refactor regexp (bug #34440)
John W. Eaton <jwe@octave.org>
parents: 13929
diff changeset
156 {
fc9f204faea0 refactor regexp (bug #34440)
John W. Eaton <jwe@octave.org>
parents: 13929
diff changeset
157 string_vector named_tokens = p->named_tokens ();
13227
9559417aa965 maint: regexp.cc style fixes
John W. Eaton <jwe@octave.org>
parents: 12642
diff changeset
158
14024
fc9f204faea0 refactor regexp (bug #34440)
John W. Eaton <jwe@octave.org>
parents: 13929
diff changeset
159 tmp(i++) = named_tokens(j);
fc9f204faea0 refactor regexp (bug #34440)
John W. Eaton <jwe@octave.org>
parents: 13929
diff changeset
160 }
fc9f204faea0 refactor regexp (bug #34440)
John W. Eaton <jwe@octave.org>
parents: 13929
diff changeset
161
fc9f204faea0 refactor regexp (bug #34440)
John W. Eaton <jwe@octave.org>
parents: 13929
diff changeset
162 nmap.assign (named_pats(j), octave_value (tmp));
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9064
diff changeset
163 }
13227
9559417aa965 maint: regexp.cc style fixes
John W. Eaton <jwe@octave.org>
parents: 12642
diff changeset
164
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9064
diff changeset
165 retval(5) = nmap;
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9064
diff changeset
166 }
5785
6b9cec830d72 [project @ 2006-05-03 19:32:46 by dbateman]
dbateman
parents: 5779
diff changeset
167
14024
fc9f204faea0 refactor regexp (bug #34440)
John W. Eaton <jwe@octave.org>
parents: 13929
diff changeset
168 if (options.once ())
13310
583940a28bfd handle "split" option for regexp
John W. Eaton <jwe@octave.org>
parents: 13227
diff changeset
169 {
14024
fc9f204faea0 refactor regexp (bug #34440)
John W. Eaton <jwe@octave.org>
parents: 13929
diff changeset
170 regexp::match_data::const_iterator p = rx_lst.begin ();
fc9f204faea0 refactor regexp (bug #34440)
John W. Eaton <jwe@octave.org>
parents: 13929
diff changeset
171
fc9f204faea0 refactor regexp (bug #34440)
John W. Eaton <jwe@octave.org>
parents: 13929
diff changeset
172 retval(4) = sz ? p->tokens () : Cell ();
fc9f204faea0 refactor regexp (bug #34440)
John W. Eaton <jwe@octave.org>
parents: 13929
diff changeset
173 retval(3) = sz ? p->match_string () : std::string ();
fc9f204faea0 refactor regexp (bug #34440)
John W. Eaton <jwe@octave.org>
parents: 13929
diff changeset
174 retval(2) = sz ? p->token_extents () : Matrix ();
13310
583940a28bfd handle "split" option for regexp
John W. Eaton <jwe@octave.org>
parents: 13227
diff changeset
175
583940a28bfd handle "split" option for regexp
John W. Eaton <jwe@octave.org>
parents: 13227
diff changeset
176 if (sz)
583940a28bfd handle "split" option for regexp
John W. Eaton <jwe@octave.org>
parents: 13227
diff changeset
177 {
14024
fc9f204faea0 refactor regexp (bug #34440)
John W. Eaton <jwe@octave.org>
parents: 13929
diff changeset
178 double start = p->start ();
fc9f204faea0 refactor regexp (bug #34440)
John W. Eaton <jwe@octave.org>
parents: 13929
diff changeset
179 double end = p->end ();
13310
583940a28bfd handle "split" option for regexp
John W. Eaton <jwe@octave.org>
parents: 13227
diff changeset
180
14024
fc9f204faea0 refactor regexp (bug #34440)
John W. Eaton <jwe@octave.org>
parents: 13929
diff changeset
181 Cell split (dim_vector (1, 2));
fc9f204faea0 refactor regexp (bug #34440)
John W. Eaton <jwe@octave.org>
parents: 13929
diff changeset
182 split(0) = buffer.substr (0, start-1);
fc9f204faea0 refactor regexp (bug #34440)
John W. Eaton <jwe@octave.org>
parents: 13929
diff changeset
183 split(1) = buffer.substr (end);
13310
583940a28bfd handle "split" option for regexp
John W. Eaton <jwe@octave.org>
parents: 13227
diff changeset
184
14024
fc9f204faea0 refactor regexp (bug #34440)
John W. Eaton <jwe@octave.org>
parents: 13929
diff changeset
185 retval(6) = split;
fc9f204faea0 refactor regexp (bug #34440)
John W. Eaton <jwe@octave.org>
parents: 13929
diff changeset
186 retval(1) = end;
fc9f204faea0 refactor regexp (bug #34440)
John W. Eaton <jwe@octave.org>
parents: 13929
diff changeset
187 retval(0) = start;
13310
583940a28bfd handle "split" option for regexp
John W. Eaton <jwe@octave.org>
parents: 13227
diff changeset
188 }
583940a28bfd handle "split" option for regexp
John W. Eaton <jwe@octave.org>
parents: 13227
diff changeset
189 else
583940a28bfd handle "split" option for regexp
John W. Eaton <jwe@octave.org>
parents: 13227
diff changeset
190 {
583940a28bfd handle "split" option for regexp
John W. Eaton <jwe@octave.org>
parents: 13227
diff changeset
191 retval(6) = buffer;
583940a28bfd handle "split" option for regexp
John W. Eaton <jwe@octave.org>
parents: 13227
diff changeset
192 retval(1) = Matrix ();
583940a28bfd handle "split" option for regexp
John W. Eaton <jwe@octave.org>
parents: 13227
diff changeset
193 retval(0) = Matrix ();
583940a28bfd handle "split" option for regexp
John W. Eaton <jwe@octave.org>
parents: 13227
diff changeset
194 }
583940a28bfd handle "split" option for regexp
John W. Eaton <jwe@octave.org>
parents: 13227
diff changeset
195 }
7893
eb9ccb44ea41 make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents: 7520
diff changeset
196 else
eb9ccb44ea41 make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents: 7520
diff changeset
197 {
14024
fc9f204faea0 refactor regexp (bug #34440)
John W. Eaton <jwe@octave.org>
parents: 13929
diff changeset
198 Cell tokens (dim_vector (1, sz));
fc9f204faea0 refactor regexp (bug #34440)
John W. Eaton <jwe@octave.org>
parents: 13929
diff changeset
199 Cell match_string (dim_vector (1, sz));
fc9f204faea0 refactor regexp (bug #34440)
John W. Eaton <jwe@octave.org>
parents: 13929
diff changeset
200 Cell token_extents (dim_vector (1, sz));
fc9f204faea0 refactor regexp (bug #34440)
John W. Eaton <jwe@octave.org>
parents: 13929
diff changeset
201 NDArray end (dim_vector (1, sz));
fc9f204faea0 refactor regexp (bug #34440)
John W. Eaton <jwe@octave.org>
parents: 13929
diff changeset
202 NDArray start (dim_vector (1, sz));
fc9f204faea0 refactor regexp (bug #34440)
John W. Eaton <jwe@octave.org>
parents: 13929
diff changeset
203 Cell split (dim_vector (1, sz+1));
13310
583940a28bfd handle "split" option for regexp
John W. Eaton <jwe@octave.org>
parents: 13227
diff changeset
204 size_t sp_start = 0;
13227
9559417aa965 maint: regexp.cc style fixes
John W. Eaton <jwe@octave.org>
parents: 12642
diff changeset
205
7893
eb9ccb44ea41 make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents: 7520
diff changeset
206 i = 0;
14024
fc9f204faea0 refactor regexp (bug #34440)
John W. Eaton <jwe@octave.org>
parents: 13929
diff changeset
207 for (regexp::match_data::const_iterator p = rx_lst.begin ();
fc9f204faea0 refactor regexp (bug #34440)
John W. Eaton <jwe@octave.org>
parents: 13929
diff changeset
208 p != rx_lst.end (); p++)
13310
583940a28bfd handle "split" option for regexp
John W. Eaton <jwe@octave.org>
parents: 13227
diff changeset
209 {
14024
fc9f204faea0 refactor regexp (bug #34440)
John W. Eaton <jwe@octave.org>
parents: 13929
diff changeset
210 double s = p->start ();
fc9f204faea0 refactor regexp (bug #34440)
John W. Eaton <jwe@octave.org>
parents: 13929
diff changeset
211 double e = p->end ();
fc9f204faea0 refactor regexp (bug #34440)
John W. Eaton <jwe@octave.org>
parents: 13929
diff changeset
212
fc9f204faea0 refactor regexp (bug #34440)
John W. Eaton <jwe@octave.org>
parents: 13929
diff changeset
213 string_vector tmp = p->tokens ();
fc9f204faea0 refactor regexp (bug #34440)
John W. Eaton <jwe@octave.org>
parents: 13929
diff changeset
214 tokens(i) = Cell (dim_vector (1, tmp.length ()), tmp);
fc9f204faea0 refactor regexp (bug #34440)
John W. Eaton <jwe@octave.org>
parents: 13929
diff changeset
215 match_string(i) = p->match_string ();
fc9f204faea0 refactor regexp (bug #34440)
John W. Eaton <jwe@octave.org>
parents: 13929
diff changeset
216 token_extents(i) = p->token_extents ();
fc9f204faea0 refactor regexp (bug #34440)
John W. Eaton <jwe@octave.org>
parents: 13929
diff changeset
217 end(i) = e;
fc9f204faea0 refactor regexp (bug #34440)
John W. Eaton <jwe@octave.org>
parents: 13929
diff changeset
218 start(i) = s;
fc9f204faea0 refactor regexp (bug #34440)
John W. Eaton <jwe@octave.org>
parents: 13929
diff changeset
219 split(i) = buffer.substr (sp_start, s-sp_start-1);
fc9f204faea0 refactor regexp (bug #34440)
John W. Eaton <jwe@octave.org>
parents: 13929
diff changeset
220 sp_start = e;
13310
583940a28bfd handle "split" option for regexp
John W. Eaton <jwe@octave.org>
parents: 13227
diff changeset
221 i++;
583940a28bfd handle "split" option for regexp
John W. Eaton <jwe@octave.org>
parents: 13227
diff changeset
222 }
13227
9559417aa965 maint: regexp.cc style fixes
John W. Eaton <jwe@octave.org>
parents: 12642
diff changeset
223
14024
fc9f204faea0 refactor regexp (bug #34440)
John W. Eaton <jwe@octave.org>
parents: 13929
diff changeset
224 split(i) = buffer.substr (sp_start);
5785
6b9cec830d72 [project @ 2006-05-03 19:32:46 by dbateman]
dbateman
parents: 5779
diff changeset
225
14024
fc9f204faea0 refactor regexp (bug #34440)
John W. Eaton <jwe@octave.org>
parents: 13929
diff changeset
226 retval(6) = split;
fc9f204faea0 refactor regexp (bug #34440)
John W. Eaton <jwe@octave.org>
parents: 13929
diff changeset
227 retval(4) = tokens;
fc9f204faea0 refactor regexp (bug #34440)
John W. Eaton <jwe@octave.org>
parents: 13929
diff changeset
228 retval(3) = match_string;
fc9f204faea0 refactor regexp (bug #34440)
John W. Eaton <jwe@octave.org>
parents: 13929
diff changeset
229 retval(2) = token_extents;
fc9f204faea0 refactor regexp (bug #34440)
John W. Eaton <jwe@octave.org>
parents: 13929
diff changeset
230 retval(1) = end;
fc9f204faea0 refactor regexp (bug #34440)
John W. Eaton <jwe@octave.org>
parents: 13929
diff changeset
231 retval(0) = start;
7893
eb9ccb44ea41 make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents: 7520
diff changeset
232 }
5582
6bf56668b01a [project @ 2005-12-15 01:08:20 by jwe]
jwe
parents:
diff changeset
233
6bf56668b01a [project @ 2005-12-15 01:08:20 by jwe]
jwe
parents:
diff changeset
234 // Alter the order of the output arguments
14024
fc9f204faea0 refactor regexp (bug #34440)
John W. Eaton <jwe@octave.org>
parents: 13929
diff changeset
235
fc9f204faea0 refactor regexp (bug #34440)
John W. Eaton <jwe@octave.org>
parents: 13929
diff changeset
236 if (extra_options)
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9064
diff changeset
237 {
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9064
diff changeset
238 int n = 0;
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9064
diff changeset
239 octave_value_list new_retval;
13227
9559417aa965 maint: regexp.cc style fixes
John W. Eaton <jwe@octave.org>
parents: 12642
diff changeset
240 new_retval.resize (nargout);
5582
6bf56668b01a [project @ 2005-12-15 01:08:20 by jwe]
jwe
parents:
diff changeset
241
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9064
diff changeset
242 OCTAVE_LOCAL_BUFFER (int, arg_used, 6);
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9064
diff changeset
243 for (int j = 0; j < 6; j++)
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9064
diff changeset
244 arg_used[j] = false;
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11572
diff changeset
245
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9064
diff changeset
246 for (int j = 2; j < nargin; j++)
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9064
diff changeset
247 {
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9064
diff changeset
248 int k = 0;
13227
9559417aa965 maint: regexp.cc style fixes
John W. Eaton <jwe@octave.org>
parents: 12642
diff changeset
249 std::string str = args(j).string_value ();
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9064
diff changeset
250 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
251
9559417aa965 maint: regexp.cc style fixes
John W. Eaton <jwe@octave.org>
parents: 12642
diff changeset
252 if (str.find ("once", 0) == 0
9559417aa965 maint: regexp.cc style fixes
John W. Eaton <jwe@octave.org>
parents: 12642
diff changeset
253 || str.find ("stringanchors", 0) == 0
9559417aa965 maint: regexp.cc style fixes
John W. Eaton <jwe@octave.org>
parents: 12642
diff changeset
254 || str.find ("lineanchors", 0) == 0
9559417aa965 maint: regexp.cc style fixes
John W. Eaton <jwe@octave.org>
parents: 12642
diff changeset
255 || str.find ("matchcase", 0) == 0
9559417aa965 maint: regexp.cc style fixes
John W. Eaton <jwe@octave.org>
parents: 12642
diff changeset
256 || str.find ("ignorecase", 0) == 0
9559417aa965 maint: regexp.cc style fixes
John W. Eaton <jwe@octave.org>
parents: 12642
diff changeset
257 || str.find ("dotall", 0) == 0
9559417aa965 maint: regexp.cc style fixes
John W. Eaton <jwe@octave.org>
parents: 12642
diff changeset
258 || str.find ("dotexceptnewline", 0) == 0
9559417aa965 maint: regexp.cc style fixes
John W. Eaton <jwe@octave.org>
parents: 12642
diff changeset
259 || str.find ("literalspacing", 0) == 0
9559417aa965 maint: regexp.cc style fixes
John W. Eaton <jwe@octave.org>
parents: 12642
diff changeset
260 || str.find ("freespacing", 0) == 0)
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9064
diff changeset
261 continue;
13227
9559417aa965 maint: regexp.cc style fixes
John W. Eaton <jwe@octave.org>
parents: 12642
diff changeset
262 else if (str.find ("start", 0) == 0)
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9064
diff changeset
263 k = 0;
13227
9559417aa965 maint: regexp.cc style fixes
John W. Eaton <jwe@octave.org>
parents: 12642
diff changeset
264 else if (str.find ("end", 0) == 0)
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9064
diff changeset
265 k = 1;
13227
9559417aa965 maint: regexp.cc style fixes
John W. Eaton <jwe@octave.org>
parents: 12642
diff changeset
266 else if (str.find ("tokenextents", 0) == 0)
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9064
diff changeset
267 k = 2;
13227
9559417aa965 maint: regexp.cc style fixes
John W. Eaton <jwe@octave.org>
parents: 12642
diff changeset
268 else if (str.find ("match", 0) == 0)
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9064
diff changeset
269 k = 3;
13227
9559417aa965 maint: regexp.cc style fixes
John W. Eaton <jwe@octave.org>
parents: 12642
diff changeset
270 else if (str.find ("tokens", 0) == 0)
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9064
diff changeset
271 k = 4;
13227
9559417aa965 maint: regexp.cc style fixes
John W. Eaton <jwe@octave.org>
parents: 12642
diff changeset
272 else if (str.find ("names", 0) == 0)
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9064
diff changeset
273 k = 5;
13310
583940a28bfd handle "split" option for regexp
John W. Eaton <jwe@octave.org>
parents: 13227
diff changeset
274 else if (str.find ("split", 0) == 0)
583940a28bfd handle "split" option for regexp
John W. Eaton <jwe@octave.org>
parents: 13227
diff changeset
275 k = 6;
5582
6bf56668b01a [project @ 2005-12-15 01:08:20 by jwe]
jwe
parents:
diff changeset
276
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9064
diff changeset
277 new_retval(n++) = retval(k);
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9064
diff changeset
278 arg_used[k] = true;
5582
6bf56668b01a [project @ 2005-12-15 01:08:20 by jwe]
jwe
parents:
diff changeset
279
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9064
diff changeset
280 if (n == nargout)
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9064
diff changeset
281 break;
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9064
diff changeset
282 }
5582
6bf56668b01a [project @ 2005-12-15 01:08:20 by jwe]
jwe
parents:
diff changeset
283
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9064
diff changeset
284 // Fill in the rest of the arguments
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9064
diff changeset
285 if (n < nargout)
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 for (int j = 0; j < 6; j++)
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9064
diff changeset
288 {
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9064
diff changeset
289 if (! arg_used[j])
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9064
diff changeset
290 new_retval(n++) = retval(j);
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9064
diff changeset
291 }
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9064
diff changeset
292 }
5582
6bf56668b01a [project @ 2005-12-15 01:08:20 by jwe]
jwe
parents:
diff changeset
293
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9064
diff changeset
294 retval = new_retval;
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9064
diff changeset
295 }
5582
6bf56668b01a [project @ 2005-12-15 01:08:20 by jwe]
jwe
parents:
diff changeset
296 }
6bf56668b01a [project @ 2005-12-15 01:08:20 by jwe]
jwe
parents:
diff changeset
297
6bf56668b01a [project @ 2005-12-15 01:08:20 by jwe]
jwe
parents:
diff changeset
298 return retval;
6bf56668b01a [project @ 2005-12-15 01:08:20 by jwe]
jwe
parents:
diff changeset
299 }
6bf56668b01a [project @ 2005-12-15 01:08:20 by jwe]
jwe
parents:
diff changeset
300
6361
776e657c9422 [project @ 2007-02-27 09:45:03 by dbateman]
dbateman
parents: 5866
diff changeset
301 static octave_value_list
13227
9559417aa965 maint: regexp.cc style fixes
John W. Eaton <jwe@octave.org>
parents: 12642
diff changeset
302 octcellregexp (const octave_value_list &args, int nargout,
14024
fc9f204faea0 refactor regexp (bug #34440)
John W. Eaton <jwe@octave.org>
parents: 13929
diff changeset
303 const std::string &who, bool case_insensitive = false)
6361
776e657c9422 [project @ 2007-02-27 09:45:03 by dbateman]
dbateman
parents: 5866
diff changeset
304 {
776e657c9422 [project @ 2007-02-27 09:45:03 by dbateman]
dbateman
parents: 5866
diff changeset
305 octave_value_list retval;
776e657c9422 [project @ 2007-02-27 09:45:03 by dbateman]
dbateman
parents: 5866
diff changeset
306
13227
9559417aa965 maint: regexp.cc style fixes
John W. Eaton <jwe@octave.org>
parents: 12642
diff changeset
307 if (args(0).is_cell ())
6361
776e657c9422 [project @ 2007-02-27 09:45:03 by dbateman]
dbateman
parents: 5866
diff changeset
308 {
776e657c9422 [project @ 2007-02-27 09:45:03 by dbateman]
dbateman
parents: 5866
diff changeset
309 OCTAVE_LOCAL_BUFFER (Cell, newretval, nargout);
776e657c9422 [project @ 2007-02-27 09:45:03 by dbateman]
dbateman
parents: 5866
diff changeset
310 octave_value_list new_args = args;
13227
9559417aa965 maint: regexp.cc style fixes
John W. Eaton <jwe@octave.org>
parents: 12642
diff changeset
311 Cell cellstr = args(0).cell_value ();
9559417aa965 maint: regexp.cc style fixes
John W. Eaton <jwe@octave.org>
parents: 12642
diff changeset
312 if (args(1).is_cell ())
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9064
diff changeset
313 {
13227
9559417aa965 maint: regexp.cc style fixes
John W. Eaton <jwe@octave.org>
parents: 12642
diff changeset
314 Cell cellpat = args(1).cell_value ();
6361
776e657c9422 [project @ 2007-02-27 09:45:03 by dbateman]
dbateman
parents: 5866
diff changeset
315
13227
9559417aa965 maint: regexp.cc style fixes
John W. Eaton <jwe@octave.org>
parents: 12642
diff changeset
316 if (cellpat.numel () == 1)
10154
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 for (int j = 0; j < nargout; j++)
13227
9559417aa965 maint: regexp.cc style fixes
John W. Eaton <jwe@octave.org>
parents: 12642
diff changeset
319 newretval[j].resize (cellstr.dims ());
6361
776e657c9422 [project @ 2007-02-27 09:45:03 by dbateman]
dbateman
parents: 5866
diff changeset
320
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9064
diff changeset
321 new_args(1) = cellpat(0);
6361
776e657c9422 [project @ 2007-02-27 09:45:03 by dbateman]
dbateman
parents: 5866
diff changeset
322
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9064
diff changeset
323 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
324 {
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9064
diff changeset
325 new_args(0) = cellstr(i);
14024
fc9f204faea0 refactor regexp (bug #34440)
John W. Eaton <jwe@octave.org>
parents: 13929
diff changeset
326 octave_value_list tmp = octregexp (new_args, nargout, who,
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9064
diff changeset
327 case_insensitive);
6361
776e657c9422 [project @ 2007-02-27 09:45:03 by dbateman]
dbateman
parents: 5866
diff changeset
328
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9064
diff changeset
329 if (error_state)
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9064
diff changeset
330 break;
6361
776e657c9422 [project @ 2007-02-27 09:45:03 by dbateman]
dbateman
parents: 5866
diff changeset
331
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9064
diff changeset
332 for (int j = 0; j < nargout; j++)
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9064
diff changeset
333 newretval[j](i) = tmp(j);
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9064
diff changeset
334 }
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9064
diff changeset
335 }
13227
9559417aa965 maint: regexp.cc style fixes
John W. Eaton <jwe@octave.org>
parents: 12642
diff changeset
336 else if (cellstr.numel () == 1)
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9064
diff changeset
337 {
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9064
diff changeset
338 for (int j = 0; j < nargout; j++)
13227
9559417aa965 maint: regexp.cc style fixes
John W. Eaton <jwe@octave.org>
parents: 12642
diff changeset
339 newretval[j].resize (cellpat.dims ());
6361
776e657c9422 [project @ 2007-02-27 09:45:03 by dbateman]
dbateman
parents: 5866
diff changeset
340
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9064
diff changeset
341 new_args(0) = cellstr(0);
6361
776e657c9422 [project @ 2007-02-27 09:45:03 by dbateman]
dbateman
parents: 5866
diff changeset
342
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9064
diff changeset
343 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
344 {
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9064
diff changeset
345 new_args(1) = cellpat(i);
14024
fc9f204faea0 refactor regexp (bug #34440)
John W. Eaton <jwe@octave.org>
parents: 13929
diff changeset
346 octave_value_list tmp = octregexp (new_args, nargout, who,
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9064
diff changeset
347 case_insensitive);
6361
776e657c9422 [project @ 2007-02-27 09:45:03 by dbateman]
dbateman
parents: 5866
diff changeset
348
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9064
diff changeset
349 if (error_state)
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9064
diff changeset
350 break;
6361
776e657c9422 [project @ 2007-02-27 09:45:03 by dbateman]
dbateman
parents: 5866
diff changeset
351
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9064
diff changeset
352 for (int j = 0; j < nargout; j++)
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9064
diff changeset
353 newretval[j](i) = tmp(j);
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9064
diff changeset
354 }
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9064
diff changeset
355 }
13227
9559417aa965 maint: regexp.cc style fixes
John W. Eaton <jwe@octave.org>
parents: 12642
diff changeset
356 else if (cellstr.numel () == cellpat.numel ())
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9064
diff changeset
357 {
6361
776e657c9422 [project @ 2007-02-27 09:45:03 by dbateman]
dbateman
parents: 5866
diff changeset
358
13227
9559417aa965 maint: regexp.cc style fixes
John W. Eaton <jwe@octave.org>
parents: 12642
diff changeset
359 if (cellstr.dims () != cellpat.dims ())
14024
fc9f204faea0 refactor regexp (bug #34440)
John W. Eaton <jwe@octave.org>
parents: 13929
diff changeset
360 error ("%s: inconsistent cell array dimensions", who.c_str ());
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9064
diff changeset
361 else
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9064
diff changeset
362 {
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9064
diff changeset
363 for (int j = 0; j < nargout; j++)
13227
9559417aa965 maint: regexp.cc style fixes
John W. Eaton <jwe@octave.org>
parents: 12642
diff changeset
364 newretval[j].resize (cellstr.dims ());
6361
776e657c9422 [project @ 2007-02-27 09:45:03 by dbateman]
dbateman
parents: 5866
diff changeset
365
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9064
diff changeset
366 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
367 {
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9064
diff changeset
368 new_args(0) = cellstr(i);
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9064
diff changeset
369 new_args(1) = cellpat(i);
6361
776e657c9422 [project @ 2007-02-27 09:45:03 by dbateman]
dbateman
parents: 5866
diff changeset
370
14024
fc9f204faea0 refactor regexp (bug #34440)
John W. Eaton <jwe@octave.org>
parents: 13929
diff changeset
371 octave_value_list tmp = octregexp (new_args, nargout, who,
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9064
diff changeset
372 case_insensitive);
6361
776e657c9422 [project @ 2007-02-27 09:45:03 by dbateman]
dbateman
parents: 5866
diff changeset
373
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9064
diff changeset
374 if (error_state)
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9064
diff changeset
375 break;
6361
776e657c9422 [project @ 2007-02-27 09:45:03 by dbateman]
dbateman
parents: 5866
diff changeset
376
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9064
diff changeset
377 for (int j = 0; j < nargout; j++)
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9064
diff changeset
378 newretval[j](i) = tmp(j);
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 }
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9064
diff changeset
381 }
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9064
diff changeset
382 else
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9064
diff changeset
383 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
384 }
6361
776e657c9422 [project @ 2007-02-27 09:45:03 by dbateman]
dbateman
parents: 5866
diff changeset
385 else
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9064
diff changeset
386 {
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9064
diff changeset
387 for (int j = 0; j < nargout; j++)
13227
9559417aa965 maint: regexp.cc style fixes
John W. Eaton <jwe@octave.org>
parents: 12642
diff changeset
388 newretval[j].resize (cellstr.dims ());
6361
776e657c9422 [project @ 2007-02-27 09:45:03 by dbateman]
dbateman
parents: 5866
diff changeset
389
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9064
diff changeset
390 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
391 {
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9064
diff changeset
392 new_args(0) = cellstr(i);
14024
fc9f204faea0 refactor regexp (bug #34440)
John W. Eaton <jwe@octave.org>
parents: 13929
diff changeset
393 octave_value_list tmp = octregexp (new_args, nargout, who,
13227
9559417aa965 maint: regexp.cc style fixes
John W. Eaton <jwe@octave.org>
parents: 12642
diff changeset
394 case_insensitive);
6361
776e657c9422 [project @ 2007-02-27 09:45:03 by dbateman]
dbateman
parents: 5866
diff changeset
395
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9064
diff changeset
396 if (error_state)
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9064
diff changeset
397 break;
6361
776e657c9422 [project @ 2007-02-27 09:45:03 by dbateman]
dbateman
parents: 5866
diff changeset
398
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9064
diff changeset
399 for (int j = 0; j < nargout; j++)
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9064
diff changeset
400 newretval[j](i) = tmp(j);
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9064
diff changeset
401 }
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9064
diff changeset
402 }
6361
776e657c9422 [project @ 2007-02-27 09:45:03 by dbateman]
dbateman
parents: 5866
diff changeset
403
776e657c9422 [project @ 2007-02-27 09:45:03 by dbateman]
dbateman
parents: 5866
diff changeset
404 if (!error_state)
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9064
diff changeset
405 for (int j = 0; j < nargout; j++)
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9064
diff changeset
406 retval(j) = octave_value (newretval[j]);
6361
776e657c9422 [project @ 2007-02-27 09:45:03 by dbateman]
dbateman
parents: 5866
diff changeset
407 }
13227
9559417aa965 maint: regexp.cc style fixes
John W. Eaton <jwe@octave.org>
parents: 12642
diff changeset
408 else if (args(1).is_cell ())
6361
776e657c9422 [project @ 2007-02-27 09:45:03 by dbateman]
dbateman
parents: 5866
diff changeset
409 {
776e657c9422 [project @ 2007-02-27 09:45:03 by dbateman]
dbateman
parents: 5866
diff changeset
410 OCTAVE_LOCAL_BUFFER (Cell, newretval, nargout);
776e657c9422 [project @ 2007-02-27 09:45:03 by dbateman]
dbateman
parents: 5866
diff changeset
411 octave_value_list new_args = args;
13227
9559417aa965 maint: regexp.cc style fixes
John W. Eaton <jwe@octave.org>
parents: 12642
diff changeset
412 Cell cellpat = args(1).cell_value ();
6361
776e657c9422 [project @ 2007-02-27 09:45:03 by dbateman]
dbateman
parents: 5866
diff changeset
413
776e657c9422 [project @ 2007-02-27 09:45:03 by dbateman]
dbateman
parents: 5866
diff changeset
414 for (int j = 0; j < nargout; j++)
13227
9559417aa965 maint: regexp.cc style fixes
John W. Eaton <jwe@octave.org>
parents: 12642
diff changeset
415 newretval[j].resize(cellpat.dims ());
6361
776e657c9422 [project @ 2007-02-27 09:45:03 by dbateman]
dbateman
parents: 5866
diff changeset
416
776e657c9422 [project @ 2007-02-27 09:45:03 by dbateman]
dbateman
parents: 5866
diff changeset
417 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
418 {
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9064
diff changeset
419 new_args(1) = cellpat(i);
14024
fc9f204faea0 refactor regexp (bug #34440)
John W. Eaton <jwe@octave.org>
parents: 13929
diff changeset
420 octave_value_list tmp = octregexp (new_args, nargout, who,
13227
9559417aa965 maint: regexp.cc style fixes
John W. Eaton <jwe@octave.org>
parents: 12642
diff changeset
421 case_insensitive);
6361
776e657c9422 [project @ 2007-02-27 09:45:03 by dbateman]
dbateman
parents: 5866
diff changeset
422
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9064
diff changeset
423 if (error_state)
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9064
diff changeset
424 break;
6361
776e657c9422 [project @ 2007-02-27 09:45:03 by dbateman]
dbateman
parents: 5866
diff changeset
425
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9064
diff changeset
426 for (int j = 0; j < nargout; j++)
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9064
diff changeset
427 newretval[j](i) = tmp(j);
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9064
diff changeset
428 }
6361
776e657c9422 [project @ 2007-02-27 09:45:03 by dbateman]
dbateman
parents: 5866
diff changeset
429
776e657c9422 [project @ 2007-02-27 09:45:03 by dbateman]
dbateman
parents: 5866
diff changeset
430 if (!error_state)
13227
9559417aa965 maint: regexp.cc style fixes
John W. Eaton <jwe@octave.org>
parents: 12642
diff changeset
431 {
9559417aa965 maint: regexp.cc style fixes
John W. Eaton <jwe@octave.org>
parents: 12642
diff changeset
432 for (int j = 0; j < nargout; j++)
9559417aa965 maint: regexp.cc style fixes
John W. Eaton <jwe@octave.org>
parents: 12642
diff changeset
433 retval(j) = octave_value (newretval[j]);
9559417aa965 maint: regexp.cc style fixes
John W. Eaton <jwe@octave.org>
parents: 12642
diff changeset
434 }
6361
776e657c9422 [project @ 2007-02-27 09:45:03 by dbateman]
dbateman
parents: 5866
diff changeset
435 }
776e657c9422 [project @ 2007-02-27 09:45:03 by dbateman]
dbateman
parents: 5866
diff changeset
436 else
14024
fc9f204faea0 refactor regexp (bug #34440)
John W. Eaton <jwe@octave.org>
parents: 13929
diff changeset
437 retval = octregexp (args, nargout, who, case_insensitive);
6361
776e657c9422 [project @ 2007-02-27 09:45:03 by dbateman]
dbateman
parents: 5866
diff changeset
438
776e657c9422 [project @ 2007-02-27 09:45:03 by dbateman]
dbateman
parents: 5866
diff changeset
439 return retval;
776e657c9422 [project @ 2007-02-27 09:45:03 by dbateman]
dbateman
parents: 5866
diff changeset
440
776e657c9422 [project @ 2007-02-27 09:45:03 by dbateman]
dbateman
parents: 5866
diff changeset
441 }
776e657c9422 [project @ 2007-02-27 09:45:03 by dbateman]
dbateman
parents: 5866
diff changeset
442
5582
6bf56668b01a [project @ 2005-12-15 01:08:20 by jwe]
jwe
parents:
diff changeset
443 DEFUN_DLD (regexp, args, nargout,
6bf56668b01a [project @ 2005-12-15 01:08:20 by jwe]
jwe
parents:
diff changeset
444 "-*- texinfo -*-\n\
10840
89f4d7e294cc Grammarcheck .cc files
Rik <octave@nomad.inbox5.com>
parents: 10551
diff changeset
445 @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
446 @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
447 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
448 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
449 are none.\n\
5582
6bf56668b01a [project @ 2005-12-15 01:08:20 by jwe]
jwe
parents:
diff changeset
450 \n\
6bf56668b01a [project @ 2005-12-15 01:08:20 by jwe]
jwe
parents:
diff changeset
451 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
452 operators, including:\n\
6bf56668b01a [project @ 2005-12-15 01:08:20 by jwe]
jwe
parents:
diff changeset
453 \n\
6bf56668b01a [project @ 2005-12-15 01:08:20 by jwe]
jwe
parents:
diff changeset
454 @table @code\n\
6bf56668b01a [project @ 2005-12-15 01:08:20 by jwe]
jwe
parents:
diff changeset
455 @item .\n\
6bf56668b01a [project @ 2005-12-15 01:08:20 by jwe]
jwe
parents:
diff changeset
456 Match any character\n\
10840
89f4d7e294cc Grammarcheck .cc files
Rik <octave@nomad.inbox5.com>
parents: 10551
diff changeset
457 \n\
5582
6bf56668b01a [project @ 2005-12-15 01:08:20 by jwe]
jwe
parents:
diff changeset
458 @item * + ? @{@}\n\
6bf56668b01a [project @ 2005-12-15 01:08:20 by jwe]
jwe
parents:
diff changeset
459 Repetition operators, representing\n\
6bf56668b01a [project @ 2005-12-15 01:08:20 by jwe]
jwe
parents:
diff changeset
460 @table @code\n\
6bf56668b01a [project @ 2005-12-15 01:08:20 by jwe]
jwe
parents:
diff changeset
461 @item *\n\
6bf56668b01a [project @ 2005-12-15 01:08:20 by jwe]
jwe
parents:
diff changeset
462 Match zero or more times\n\
10840
89f4d7e294cc Grammarcheck .cc files
Rik <octave@nomad.inbox5.com>
parents: 10551
diff changeset
463 \n\
5582
6bf56668b01a [project @ 2005-12-15 01:08:20 by jwe]
jwe
parents:
diff changeset
464 @item +\n\
6bf56668b01a [project @ 2005-12-15 01:08:20 by jwe]
jwe
parents:
diff changeset
465 Match one or more times\n\
10840
89f4d7e294cc Grammarcheck .cc files
Rik <octave@nomad.inbox5.com>
parents: 10551
diff changeset
466 \n\
5582
6bf56668b01a [project @ 2005-12-15 01:08:20 by jwe]
jwe
parents:
diff changeset
467 @item ?\n\
6bf56668b01a [project @ 2005-12-15 01:08:20 by jwe]
jwe
parents:
diff changeset
468 Match zero or one times\n\
10840
89f4d7e294cc Grammarcheck .cc files
Rik <octave@nomad.inbox5.com>
parents: 10551
diff changeset
469 \n\
11025
df2152514429 Update docstrings for regular expression functions
Rik <octave@nomad.inbox5.com>
parents: 11018
diff changeset
470 @item @{@var{n}@}\n\
df2152514429 Update docstrings for regular expression functions
Rik <octave@nomad.inbox5.com>
parents: 11018
diff changeset
471 Match exactly @var{n} times\n\
df2152514429 Update docstrings for regular expression functions
Rik <octave@nomad.inbox5.com>
parents: 11018
diff changeset
472 \n\
df2152514429 Update docstrings for regular expression functions
Rik <octave@nomad.inbox5.com>
parents: 11018
diff changeset
473 @item @{@var{n},@}\n\
df2152514429 Update docstrings for regular expression functions
Rik <octave@nomad.inbox5.com>
parents: 11018
diff changeset
474 Match @var{n} or more times\n\
df2152514429 Update docstrings for regular expression functions
Rik <octave@nomad.inbox5.com>
parents: 11018
diff changeset
475 \n\
df2152514429 Update docstrings for regular expression functions
Rik <octave@nomad.inbox5.com>
parents: 11018
diff changeset
476 @item @{@var{m},@var{n}@}\n\
df2152514429 Update docstrings for regular expression functions
Rik <octave@nomad.inbox5.com>
parents: 11018
diff changeset
477 Match between @var{m} and @var{n} times\n\
5582
6bf56668b01a [project @ 2005-12-15 01:08:20 by jwe]
jwe
parents:
diff changeset
478 @end table\n\
10840
89f4d7e294cc Grammarcheck .cc files
Rik <octave@nomad.inbox5.com>
parents: 10551
diff changeset
479 \n\
5582
6bf56668b01a [project @ 2005-12-15 01:08:20 by jwe]
jwe
parents:
diff changeset
480 @item [@dots{}] [^@dots{}]\n\
10840
89f4d7e294cc Grammarcheck .cc files
Rik <octave@nomad.inbox5.com>
parents: 10551
diff changeset
481 \n\
11025
df2152514429 Update docstrings for regular expression functions
Rik <octave@nomad.inbox5.com>
parents: 11018
diff changeset
482 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
483 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
484 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
485 \n\
12462
e4dbfe3019b1 Use PCRE regular expressions throughout Octave.
Rik <octave@nomad.inbox5.com>
parents: 12433
diff changeset
486 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
487 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
488 @code{[-+.\\d]+}.\n\
10840
89f4d7e294cc Grammarcheck .cc files
Rik <octave@nomad.inbox5.com>
parents: 10551
diff changeset
489 \n\
5582
6bf56668b01a [project @ 2005-12-15 01:08:20 by jwe]
jwe
parents:
diff changeset
490 @item ()\n\
6bf56668b01a [project @ 2005-12-15 01:08:20 by jwe]
jwe
parents:
diff changeset
491 Grouping operator\n\
10840
89f4d7e294cc Grammarcheck .cc files
Rik <octave@nomad.inbox5.com>
parents: 10551
diff changeset
492 \n\
5582
6bf56668b01a [project @ 2005-12-15 01:08:20 by jwe]
jwe
parents:
diff changeset
493 @item |\n\
9036
58604c45ca74 Cleanup of data types related documentation
Rik <rdrider0-list@yahoo.com>
parents: 8920
diff changeset
494 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
495 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
496 \n\
5582
6bf56668b01a [project @ 2005-12-15 01:08:20 by jwe]
jwe
parents:
diff changeset
497 @item ^ $\n\
11025
df2152514429 Update docstrings for regular expression functions
Rik <octave@nomad.inbox5.com>
parents: 11018
diff changeset
498 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
499 end (@code{$}) of the string.\n\
5582
6bf56668b01a [project @ 2005-12-15 01:08:20 by jwe]
jwe
parents:
diff changeset
500 @end table\n\
6bf56668b01a [project @ 2005-12-15 01:08:20 by jwe]
jwe
parents:
diff changeset
501 \n\
11025
df2152514429 Update docstrings for regular expression functions
Rik <octave@nomad.inbox5.com>
parents: 11018
diff changeset
502 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
503 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
504 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
505 being passed to @code{regexp}.\n\
5582
6bf56668b01a [project @ 2005-12-15 01:08:20 by jwe]
jwe
parents:
diff changeset
506 \n\
6bf56668b01a [project @ 2005-12-15 01:08:20 by jwe]
jwe
parents:
diff changeset
507 @table @code\n\
6bf56668b01a [project @ 2005-12-15 01:08:20 by jwe]
jwe
parents:
diff changeset
508 @item \\b\n\
6bf56668b01a [project @ 2005-12-15 01:08:20 by jwe]
jwe
parents:
diff changeset
509 Match a word boundary\n\
10840
89f4d7e294cc Grammarcheck .cc files
Rik <octave@nomad.inbox5.com>
parents: 10551
diff changeset
510 \n\
5582
6bf56668b01a [project @ 2005-12-15 01:08:20 by jwe]
jwe
parents:
diff changeset
511 @item \\B\n\
6bf56668b01a [project @ 2005-12-15 01:08:20 by jwe]
jwe
parents:
diff changeset
512 Match within a word\n\
10840
89f4d7e294cc Grammarcheck .cc files
Rik <octave@nomad.inbox5.com>
parents: 10551
diff changeset
513 \n\
5582
6bf56668b01a [project @ 2005-12-15 01:08:20 by jwe]
jwe
parents:
diff changeset
514 @item \\w\n\
11025
df2152514429 Update docstrings for regular expression functions
Rik <octave@nomad.inbox5.com>
parents: 11018
diff changeset
515 Match any word character\n\
10840
89f4d7e294cc Grammarcheck .cc files
Rik <octave@nomad.inbox5.com>
parents: 10551
diff changeset
516 \n\
5582
6bf56668b01a [project @ 2005-12-15 01:08:20 by jwe]
jwe
parents:
diff changeset
517 @item \\W\n\
11025
df2152514429 Update docstrings for regular expression functions
Rik <octave@nomad.inbox5.com>
parents: 11018
diff changeset
518 Match any non-word character\n\
10840
89f4d7e294cc Grammarcheck .cc files
Rik <octave@nomad.inbox5.com>
parents: 10551
diff changeset
519 \n\
5582
6bf56668b01a [project @ 2005-12-15 01:08:20 by jwe]
jwe
parents:
diff changeset
520 @item \\<\n\
11025
df2152514429 Update docstrings for regular expression functions
Rik <octave@nomad.inbox5.com>
parents: 11018
diff changeset
521 Match the beginning of a word\n\
10840
89f4d7e294cc Grammarcheck .cc files
Rik <octave@nomad.inbox5.com>
parents: 10551
diff changeset
522 \n\
5582
6bf56668b01a [project @ 2005-12-15 01:08:20 by jwe]
jwe
parents:
diff changeset
523 @item \\>\n\
11025
df2152514429 Update docstrings for regular expression functions
Rik <octave@nomad.inbox5.com>
parents: 11018
diff changeset
524 Match the end of a word\n\
10840
89f4d7e294cc Grammarcheck .cc files
Rik <octave@nomad.inbox5.com>
parents: 10551
diff changeset
525 \n\
5582
6bf56668b01a [project @ 2005-12-15 01:08:20 by jwe]
jwe
parents:
diff changeset
526 @item \\s\n\
11025
df2152514429 Update docstrings for regular expression functions
Rik <octave@nomad.inbox5.com>
parents: 11018
diff changeset
527 Match any whitespace character\n\
10840
89f4d7e294cc Grammarcheck .cc files
Rik <octave@nomad.inbox5.com>
parents: 10551
diff changeset
528 \n\
5582
6bf56668b01a [project @ 2005-12-15 01:08:20 by jwe]
jwe
parents:
diff changeset
529 @item \\S\n\
11025
df2152514429 Update docstrings for regular expression functions
Rik <octave@nomad.inbox5.com>
parents: 11018
diff changeset
530 Match any non-whitespace character\n\
10840
89f4d7e294cc Grammarcheck .cc files
Rik <octave@nomad.inbox5.com>
parents: 10551
diff changeset
531 \n\
5582
6bf56668b01a [project @ 2005-12-15 01:08:20 by jwe]
jwe
parents:
diff changeset
532 @item \\d\n\
11025
df2152514429 Update docstrings for regular expression functions
Rik <octave@nomad.inbox5.com>
parents: 11018
diff changeset
533 Match any digit\n\
10840
89f4d7e294cc Grammarcheck .cc files
Rik <octave@nomad.inbox5.com>
parents: 10551
diff changeset
534 \n\
5582
6bf56668b01a [project @ 2005-12-15 01:08:20 by jwe]
jwe
parents:
diff changeset
535 @item \\D\n\
11025
df2152514429 Update docstrings for regular expression functions
Rik <octave@nomad.inbox5.com>
parents: 11018
diff changeset
536 Match any non-digit\n\
5582
6bf56668b01a [project @ 2005-12-15 01:08:20 by jwe]
jwe
parents:
diff changeset
537 @end table\n\
6bf56668b01a [project @ 2005-12-15 01:08:20 by jwe]
jwe
parents:
diff changeset
538 \n\
11025
df2152514429 Update docstrings for regular expression functions
Rik <octave@nomad.inbox5.com>
parents: 11018
diff changeset
539 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
540 \n\
11032
c9b0a75b02e8 Make all regexp in Octave compatible with both POSIX and PCRE.
Rik <octave@nomad.inbox5.com>
parents: 11025
diff changeset
541 @table @var\n\
c9b0a75b02e8 Make all regexp in Octave compatible with both POSIX and PCRE.
Rik <octave@nomad.inbox5.com>
parents: 11025
diff changeset
542 @item s\n\
11025
df2152514429 Update docstrings for regular expression functions
Rik <octave@nomad.inbox5.com>
parents: 11018
diff changeset
543 The start indices of each matching substring\n\
5582
6bf56668b01a [project @ 2005-12-15 01:08:20 by jwe]
jwe
parents:
diff changeset
544 \n\
11032
c9b0a75b02e8 Make all regexp in Octave compatible with both POSIX and PCRE.
Rik <octave@nomad.inbox5.com>
parents: 11025
diff changeset
545 @item e\n\
5582
6bf56668b01a [project @ 2005-12-15 01:08:20 by jwe]
jwe
parents:
diff changeset
546 The end indices of each matching substring\n\
6bf56668b01a [project @ 2005-12-15 01:08:20 by jwe]
jwe
parents:
diff changeset
547 \n\
11032
c9b0a75b02e8 Make all regexp in Octave compatible with both POSIX and PCRE.
Rik <octave@nomad.inbox5.com>
parents: 11025
diff changeset
548 @item te\n\
11025
df2152514429 Update docstrings for regular expression functions
Rik <octave@nomad.inbox5.com>
parents: 11018
diff changeset
549 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
550 @var{pat}\n\
5582
6bf56668b01a [project @ 2005-12-15 01:08:20 by jwe]
jwe
parents:
diff changeset
551 \n\
11032
c9b0a75b02e8 Make all regexp in Octave compatible with both POSIX and PCRE.
Rik <octave@nomad.inbox5.com>
parents: 11025
diff changeset
552 @item m\n\
11025
df2152514429 Update docstrings for regular expression functions
Rik <octave@nomad.inbox5.com>
parents: 11018
diff changeset
553 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
554 \n\
11032
c9b0a75b02e8 Make all regexp in Octave compatible with both POSIX and PCRE.
Rik <octave@nomad.inbox5.com>
parents: 11025
diff changeset
555 @item t\n\
11025
df2152514429 Update docstrings for regular expression functions
Rik <octave@nomad.inbox5.com>
parents: 11018
diff changeset
556 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
557 \n\
11032
c9b0a75b02e8 Make all regexp in Octave compatible with both POSIX and PCRE.
Rik <octave@nomad.inbox5.com>
parents: 11025
diff changeset
558 @item nm\n\
5582
6bf56668b01a [project @ 2005-12-15 01:08:20 by jwe]
jwe
parents:
diff changeset
559 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
560 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
561 @code{(?<name>@dots{})}.\n\
13929
9cae456085c2 Grammarcheck of documentation before 3.6.0 release.
Rik <octave@nomad.inbox5.com>
parents: 13759
diff changeset
562 \n\
13310
583940a28bfd handle "split" option for regexp
John W. Eaton <jwe@octave.org>
parents: 13227
diff changeset
563 @item sp\n\
583940a28bfd handle "split" option for regexp
John W. Eaton <jwe@octave.org>
parents: 13227
diff changeset
564 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
565 @end table\n\
6bf56668b01a [project @ 2005-12-15 01:08:20 by jwe]
jwe
parents:
diff changeset
566 \n\
11025
df2152514429 Update docstrings for regular expression functions
Rik <octave@nomad.inbox5.com>
parents: 11018
diff changeset
567 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
568 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
569 correspondence between the output arguments and the optional argument\n\
6bf56668b01a [project @ 2005-12-15 01:08:20 by jwe]
jwe
parents:
diff changeset
570 are\n\
6bf56668b01a [project @ 2005-12-15 01:08:20 by jwe]
jwe
parents:
diff changeset
571 \n\
6bf56668b01a [project @ 2005-12-15 01:08:20 by jwe]
jwe
parents:
diff changeset
572 @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
573 @item @tab 'start' @tab @var{s} @tab\n\
6bf56668b01a [project @ 2005-12-15 01:08:20 by jwe]
jwe
parents:
diff changeset
574 @item @tab 'end' @tab @var{e} @tab\n\
6bf56668b01a [project @ 2005-12-15 01:08:20 by jwe]
jwe
parents:
diff changeset
575 @item @tab 'tokenExtents' @tab @var{te} @tab\n\
6bf56668b01a [project @ 2005-12-15 01:08:20 by jwe]
jwe
parents:
diff changeset
576 @item @tab 'match' @tab @var{m} @tab\n\
6bf56668b01a [project @ 2005-12-15 01:08:20 by jwe]
jwe
parents:
diff changeset
577 @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
578 @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
579 @item @tab 'split' @tab @var{sp} @tab\n\
5582
6bf56668b01a [project @ 2005-12-15 01:08:20 by jwe]
jwe
parents:
diff changeset
580 @end multitable\n\
6bf56668b01a [project @ 2005-12-15 01:08:20 by jwe]
jwe
parents:
diff changeset
581 \n\
11025
df2152514429 Update docstrings for regular expression functions
Rik <octave@nomad.inbox5.com>
parents: 11018
diff changeset
582 Additional arguments are summarized below.\n\
5779
12eeebfa7ead [project @ 2006-04-27 19:30:14 by dbateman]
dbateman
parents: 5775
diff changeset
583 \n\
11025
df2152514429 Update docstrings for regular expression functions
Rik <octave@nomad.inbox5.com>
parents: 11018
diff changeset
584 @table @samp\n\
df2152514429 Update docstrings for regular expression functions
Rik <octave@nomad.inbox5.com>
parents: 11018
diff changeset
585 @item once\n\
df2152514429 Update docstrings for regular expression functions
Rik <octave@nomad.inbox5.com>
parents: 11018
diff changeset
586 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
587 \n\
5779
12eeebfa7ead [project @ 2006-04-27 19:30:14 by dbateman]
dbateman
parents: 5775
diff changeset
588 @item matchcase\n\
11025
df2152514429 Update docstrings for regular expression functions
Rik <octave@nomad.inbox5.com>
parents: 11018
diff changeset
589 Make the matching case sensitive. (default)\n\
df2152514429 Update docstrings for regular expression functions
Rik <octave@nomad.inbox5.com>
parents: 11018
diff changeset
590 \n\
12462
e4dbfe3019b1 Use PCRE regular expressions throughout Octave.
Rik <octave@nomad.inbox5.com>
parents: 12433
diff changeset
591 Alternatively, use (?-i) in the pattern.\n\
10840
89f4d7e294cc Grammarcheck .cc files
Rik <octave@nomad.inbox5.com>
parents: 10551
diff changeset
592 \n\
5779
12eeebfa7ead [project @ 2006-04-27 19:30:14 by dbateman]
dbateman
parents: 5775
diff changeset
593 @item ignorecase\n\
11025
df2152514429 Update docstrings for regular expression functions
Rik <octave@nomad.inbox5.com>
parents: 11018
diff changeset
594 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
595 \n\
12462
e4dbfe3019b1 Use PCRE regular expressions throughout Octave.
Rik <octave@nomad.inbox5.com>
parents: 12433
diff changeset
596 Alternatively, use (?i) in the pattern.\n\
10840
89f4d7e294cc Grammarcheck .cc files
Rik <octave@nomad.inbox5.com>
parents: 10551
diff changeset
597 \n\
5779
12eeebfa7ead [project @ 2006-04-27 19:30:14 by dbateman]
dbateman
parents: 5775
diff changeset
598 @item stringanchors\n\
12642
f96b9b9f141b doc: Periodic grammarcheck and spellcheck of documentation.
Rik <octave@nomad.inbox5.com>
parents: 12464
diff changeset
599 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
600 (default)\n\
df2152514429 Update docstrings for regular expression functions
Rik <octave@nomad.inbox5.com>
parents: 11018
diff changeset
601 \n\
12462
e4dbfe3019b1 Use PCRE regular expressions throughout Octave.
Rik <octave@nomad.inbox5.com>
parents: 12433
diff changeset
602 Alternatively, use (?-m) in the pattern.\n\
10840
89f4d7e294cc Grammarcheck .cc files
Rik <octave@nomad.inbox5.com>
parents: 10551
diff changeset
603 \n\
5779
12eeebfa7ead [project @ 2006-04-27 19:30:14 by dbateman]
dbateman
parents: 5775
diff changeset
604 @item lineanchors\n\
12eeebfa7ead [project @ 2006-04-27 19:30:14 by dbateman]
dbateman
parents: 5775
diff changeset
605 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
606 \n\
12462
e4dbfe3019b1 Use PCRE regular expressions throughout Octave.
Rik <octave@nomad.inbox5.com>
parents: 12433
diff changeset
607 Alternatively, use (?m) in the pattern.\n\
10840
89f4d7e294cc Grammarcheck .cc files
Rik <octave@nomad.inbox5.com>
parents: 10551
diff changeset
608 \n\
5779
12eeebfa7ead [project @ 2006-04-27 19:30:14 by dbateman]
dbateman
parents: 5775
diff changeset
609 @item dotall\n\
11025
df2152514429 Update docstrings for regular expression functions
Rik <octave@nomad.inbox5.com>
parents: 11018
diff changeset
610 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
611 (default)\n\
df2152514429 Update docstrings for regular expression functions
Rik <octave@nomad.inbox5.com>
parents: 11018
diff changeset
612 \n\
12462
e4dbfe3019b1 Use PCRE regular expressions throughout Octave.
Rik <octave@nomad.inbox5.com>
parents: 12433
diff changeset
613 Alternatively, use (?s) in the pattern.\n\
10840
89f4d7e294cc Grammarcheck .cc files
Rik <octave@nomad.inbox5.com>
parents: 10551
diff changeset
614 \n\
5779
12eeebfa7ead [project @ 2006-04-27 19:30:14 by dbateman]
dbateman
parents: 5775
diff changeset
615 @item dotexceptnewline\n\
11025
df2152514429 Update docstrings for regular expression functions
Rik <octave@nomad.inbox5.com>
parents: 11018
diff changeset
616 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
617 \n\
12462
e4dbfe3019b1 Use PCRE regular expressions throughout Octave.
Rik <octave@nomad.inbox5.com>
parents: 12433
diff changeset
618 Alternatively, use (?-s) in the pattern.\n\
11025
df2152514429 Update docstrings for regular expression functions
Rik <octave@nomad.inbox5.com>
parents: 11018
diff changeset
619 \n\
df2152514429 Update docstrings for regular expression functions
Rik <octave@nomad.inbox5.com>
parents: 11018
diff changeset
620 @item literalspacing\n\
df2152514429 Update docstrings for regular expression functions
Rik <octave@nomad.inbox5.com>
parents: 11018
diff changeset
621 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
622 used in pattern matching. (default)\n\
df2152514429 Update docstrings for regular expression functions
Rik <octave@nomad.inbox5.com>
parents: 11018
diff changeset
623 \n\
12462
e4dbfe3019b1 Use PCRE regular expressions throughout Octave.
Rik <octave@nomad.inbox5.com>
parents: 12433
diff changeset
624 Alternatively, use (?-x) in the pattern.\n\
10840
89f4d7e294cc Grammarcheck .cc files
Rik <octave@nomad.inbox5.com>
parents: 10551
diff changeset
625 \n\
5779
12eeebfa7ead [project @ 2006-04-27 19:30:14 by dbateman]
dbateman
parents: 5775
diff changeset
626 @item freespacing\n\
11025
df2152514429 Update docstrings for regular expression functions
Rik <octave@nomad.inbox5.com>
parents: 11018
diff changeset
627 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
628 the character @samp{#}.\n\
10840
89f4d7e294cc Grammarcheck .cc files
Rik <octave@nomad.inbox5.com>
parents: 10551
diff changeset
629 \n\
12462
e4dbfe3019b1 Use PCRE regular expressions throughout Octave.
Rik <octave@nomad.inbox5.com>
parents: 12433
diff changeset
630 Alternatively, use (?x) in the pattern.\n\
11025
df2152514429 Update docstrings for regular expression functions
Rik <octave@nomad.inbox5.com>
parents: 11018
diff changeset
631 \n\
5779
12eeebfa7ead [project @ 2006-04-27 19:30:14 by dbateman]
dbateman
parents: 5775
diff changeset
632 @end table\n\
11572
7d6d8c1e471f Grammarcheck Texinfo for files in src directory.
Rik <octave@nomad.inbox5.com>
parents: 11553
diff changeset
633 @seealso{regexpi, strfind, regexprep}\n\
5582
6bf56668b01a [project @ 2005-12-15 01:08:20 by jwe]
jwe
parents:
diff changeset
634 @end deftypefn")
6bf56668b01a [project @ 2005-12-15 01:08:20 by jwe]
jwe
parents:
diff changeset
635 {
6361
776e657c9422 [project @ 2007-02-27 09:45:03 by dbateman]
dbateman
parents: 5866
diff changeset
636 octave_value_list retval;
13227
9559417aa965 maint: regexp.cc style fixes
John W. Eaton <jwe@octave.org>
parents: 12642
diff changeset
637
9559417aa965 maint: regexp.cc style fixes
John W. Eaton <jwe@octave.org>
parents: 12642
diff changeset
638 int nargin = args.length ();
6361
776e657c9422 [project @ 2007-02-27 09:45:03 by dbateman]
dbateman
parents: 5866
diff changeset
639
776e657c9422 [project @ 2007-02-27 09:45:03 by dbateman]
dbateman
parents: 5866
diff changeset
640 if (nargin < 2)
776e657c9422 [project @ 2007-02-27 09:45:03 by dbateman]
dbateman
parents: 5866
diff changeset
641 print_usage ();
13227
9559417aa965 maint: regexp.cc style fixes
John W. Eaton <jwe@octave.org>
parents: 12642
diff changeset
642 else if (args(0).is_cell () || args(1).is_cell ())
14024
fc9f204faea0 refactor regexp (bug #34440)
John W. Eaton <jwe@octave.org>
parents: 13929
diff changeset
643 retval = octcellregexp (args, nargout, "regexp");
6361
776e657c9422 [project @ 2007-02-27 09:45:03 by dbateman]
dbateman
parents: 5866
diff changeset
644 else
14024
fc9f204faea0 refactor regexp (bug #34440)
John W. Eaton <jwe@octave.org>
parents: 13929
diff changeset
645 retval = octregexp (args, nargout, "regexp");
6361
776e657c9422 [project @ 2007-02-27 09:45:03 by dbateman]
dbateman
parents: 5866
diff changeset
646
776e657c9422 [project @ 2007-02-27 09:45:03 by dbateman]
dbateman
parents: 5866
diff changeset
647 return retval;
5582
6bf56668b01a [project @ 2005-12-15 01:08:20 by jwe]
jwe
parents:
diff changeset
648 }
6bf56668b01a [project @ 2005-12-15 01:08:20 by jwe]
jwe
parents:
diff changeset
649
6bf56668b01a [project @ 2005-12-15 01:08:20 by jwe]
jwe
parents:
diff changeset
650 /*
6bf56668b01a [project @ 2005-12-15 01:08:20 by jwe]
jwe
parents:
diff changeset
651
8140
cdd05e46f6c9 Increase pcre's match_limit for difficult regexps
Thomas Weber <thomas.weber.mail@gmail.com>
parents: 8093
diff changeset
652 ## PCRE_ERROR_MATCHLIMIT test
cdd05e46f6c9 Increase pcre's match_limit for difficult regexps
Thomas Weber <thomas.weber.mail@gmail.com>
parents: 8093
diff changeset
653 %!test
cdd05e46f6c9 Increase pcre's match_limit for difficult regexps
Thomas Weber <thomas.weber.mail@gmail.com>
parents: 8093
diff changeset
654 %! 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
655 %! ws = warning("query");
cdd05e46f6c9 Increase pcre's match_limit for difficult regexps
Thomas Weber <thomas.weber.mail@gmail.com>
parents: 8093
diff changeset
656 %! unwind_protect
cdd05e46f6c9 Increase pcre's match_limit for difficult regexps
Thomas Weber <thomas.weber.mail@gmail.com>
parents: 8093
diff changeset
657 %! warning("off");
cdd05e46f6c9 Increase pcre's match_limit for difficult regexps
Thomas Weber <thomas.weber.mail@gmail.com>
parents: 8093
diff changeset
658 %! 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
659 %! unwind_protect_cleanup
cdd05e46f6c9 Increase pcre's match_limit for difficult regexps
Thomas Weber <thomas.weber.mail@gmail.com>
parents: 8093
diff changeset
660 %! warning(ws);
cdd05e46f6c9 Increase pcre's match_limit for difficult regexps
Thomas Weber <thomas.weber.mail@gmail.com>
parents: 8093
diff changeset
661 %! end_unwind_protect
cdd05e46f6c9 Increase pcre's match_limit for difficult regexps
Thomas Weber <thomas.weber.mail@gmail.com>
parents: 8093
diff changeset
662
5582
6bf56668b01a [project @ 2005-12-15 01:08:20 by jwe]
jwe
parents:
diff changeset
663 ## seg-fault test
6bf56668b01a [project @ 2005-12-15 01:08:20 by jwe]
jwe
parents:
diff changeset
664 %!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
665 ## 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
666 %!assert (isempty (regexp("abcde", "")))
5582
6bf56668b01a [project @ 2005-12-15 01:08:20 by jwe]
jwe
parents:
diff changeset
667
6bf56668b01a [project @ 2005-12-15 01:08:20 by jwe]
jwe
parents:
diff changeset
668 ## Check that anchoring of pattern works correctly
6bf56668b01a [project @ 2005-12-15 01:08:20 by jwe]
jwe
parents:
diff changeset
669 %!assert(regexp('abcabc','^abc'),1);
6bf56668b01a [project @ 2005-12-15 01:08:20 by jwe]
jwe
parents:
diff changeset
670 %!assert(regexp('abcabc','abc$'),4);
5785
6b9cec830d72 [project @ 2006-05-03 19:32:46 by dbateman]
dbateman
parents: 5779
diff changeset
671 %!assert(regexp('abcabc','^abc$'),zeros(1,0));
5582
6bf56668b01a [project @ 2005-12-15 01:08:20 by jwe]
jwe
parents:
diff changeset
672
6bf56668b01a [project @ 2005-12-15 01:08:20 by jwe]
jwe
parents:
diff changeset
673 %!test
6bf56668b01a [project @ 2005-12-15 01:08:20 by jwe]
jwe
parents:
diff changeset
674 %! [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
675 %! assert (s,zeros(1,0))
6b9cec830d72 [project @ 2006-05-03 19:32:46 by dbateman]
dbateman
parents: 5779
diff changeset
676 %! assert (e,zeros(1,0))
6b9cec830d72 [project @ 2006-05-03 19:32:46 by dbateman]
dbateman
parents: 5779
diff changeset
677 %! assert (te,cell(1,0))
6b9cec830d72 [project @ 2006-05-03 19:32:46 by dbateman]
dbateman
parents: 5779
diff changeset
678 %! assert (m, cell(1,0))
6b9cec830d72 [project @ 2006-05-03 19:32:46 by dbateman]
dbateman
parents: 5779
diff changeset
679 %! assert (t, cell(1,0))
5582
6bf56668b01a [project @ 2005-12-15 01:08:20 by jwe]
jwe
parents:
diff changeset
680
6bf56668b01a [project @ 2005-12-15 01:08:20 by jwe]
jwe
parents:
diff changeset
681 %!test
6bf56668b01a [project @ 2005-12-15 01:08:20 by jwe]
jwe
parents:
diff changeset
682 %! [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
683 %! assert (s,zeros(1,0))
6b9cec830d72 [project @ 2006-05-03 19:32:46 by dbateman]
dbateman
parents: 5779
diff changeset
684 %! assert (e,zeros(1,0))
6b9cec830d72 [project @ 2006-05-03 19:32:46 by dbateman]
dbateman
parents: 5779
diff changeset
685 %! assert (te,cell(1,0))
6b9cec830d72 [project @ 2006-05-03 19:32:46 by dbateman]
dbateman
parents: 5779
diff changeset
686 %! assert (m, cell(1,0))
6b9cec830d72 [project @ 2006-05-03 19:32:46 by dbateman]
dbateman
parents: 5779
diff changeset
687 %! assert (t, cell(1,0))
5582
6bf56668b01a [project @ 2005-12-15 01:08:20 by jwe]
jwe
parents:
diff changeset
688
6bf56668b01a [project @ 2005-12-15 01:08:20 by jwe]
jwe
parents:
diff changeset
689 %!test
6bf56668b01a [project @ 2005-12-15 01:08:20 by jwe]
jwe
parents:
diff changeset
690 %! [s, e, te, m, t] = regexp(' firetruck ', 'f(.*)uck');
6bf56668b01a [project @ 2005-12-15 01:08:20 by jwe]
jwe
parents:
diff changeset
691 %! assert (s,2)
6bf56668b01a [project @ 2005-12-15 01:08:20 by jwe]
jwe
parents:
diff changeset
692 %! assert (e,10)
6bf56668b01a [project @ 2005-12-15 01:08:20 by jwe]
jwe
parents:
diff changeset
693 %! assert (te{1},[3,7])
6bf56668b01a [project @ 2005-12-15 01:08:20 by jwe]
jwe
parents:
diff changeset
694 %! assert (m{1}, 'firetruck')
6bf56668b01a [project @ 2005-12-15 01:08:20 by jwe]
jwe
parents:
diff changeset
695 %! assert (t{1}{1}, 'iretr')
6bf56668b01a [project @ 2005-12-15 01:08:20 by jwe]
jwe
parents:
diff changeset
696
6bf56668b01a [project @ 2005-12-15 01:08:20 by jwe]
jwe
parents:
diff changeset
697 %!test
6bf56668b01a [project @ 2005-12-15 01:08:20 by jwe]
jwe
parents:
diff changeset
698 %! [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
699 %! assert (s,[1,12])
6bf56668b01a [project @ 2005-12-15 01:08:20 by jwe]
jwe
parents:
diff changeset
700 %! assert (e,[5,17])
6bf56668b01a [project @ 2005-12-15 01:08:20 by jwe]
jwe
parents:
diff changeset
701 %! assert (size(te), [1,2])
6bf56668b01a [project @ 2005-12-15 01:08:20 by jwe]
jwe
parents:
diff changeset
702 %! assert (isempty(te{1}))
6bf56668b01a [project @ 2005-12-15 01:08:20 by jwe]
jwe
parents:
diff changeset
703 %! assert (isempty(te{2}))
6bf56668b01a [project @ 2005-12-15 01:08:20 by jwe]
jwe
parents:
diff changeset
704 %! assert (m{1},'short')
6bf56668b01a [project @ 2005-12-15 01:08:20 by jwe]
jwe
parents:
diff changeset
705 %! assert (m{2},'string')
6bf56668b01a [project @ 2005-12-15 01:08:20 by jwe]
jwe
parents:
diff changeset
706 %! assert (size(t), [1,2])
6bf56668b01a [project @ 2005-12-15 01:08:20 by jwe]
jwe
parents:
diff changeset
707 %! assert (isempty(t{1}))
6bf56668b01a [project @ 2005-12-15 01:08:20 by jwe]
jwe
parents:
diff changeset
708 %! assert (isempty(t{2}))
6bf56668b01a [project @ 2005-12-15 01:08:20 by jwe]
jwe
parents:
diff changeset
709
6bf56668b01a [project @ 2005-12-15 01:08:20 by jwe]
jwe
parents:
diff changeset
710 %!test
6bf56668b01a [project @ 2005-12-15 01:08:20 by jwe]
jwe
parents:
diff changeset
711 %! [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
712 %! assert (s,1)
6bf56668b01a [project @ 2005-12-15 01:08:20 by jwe]
jwe
parents:
diff changeset
713 %! assert (e,5)
7893
eb9ccb44ea41 make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents: 7520
diff changeset
714 %! assert (isempty(te))
eb9ccb44ea41 make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents: 7520
diff changeset
715 %! assert (m,'short')
eb9ccb44ea41 make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents: 7520
diff changeset
716 %! assert (isempty(t))
5582
6bf56668b01a [project @ 2005-12-15 01:08:20 by jwe]
jwe
parents:
diff changeset
717
6bf56668b01a [project @ 2005-12-15 01:08:20 by jwe]
jwe
parents:
diff changeset
718 %!test
6bf56668b01a [project @ 2005-12-15 01:08:20 by jwe]
jwe
parents:
diff changeset
719 %! [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
720 %! assert (s,1)
6bf56668b01a [project @ 2005-12-15 01:08:20 by jwe]
jwe
parents:
diff changeset
721 %! assert (e,5)
7893
eb9ccb44ea41 make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents: 7520
diff changeset
722 %! assert (isempty(te))
eb9ccb44ea41 make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents: 7520
diff changeset
723 %! assert (m,'short')
eb9ccb44ea41 make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents: 7520
diff changeset
724 %! assert (isempty(t))
5582
6bf56668b01a [project @ 2005-12-15 01:08:20 by jwe]
jwe
parents:
diff changeset
725
12462
e4dbfe3019b1 Use PCRE regular expressions throughout Octave.
Rik <octave@nomad.inbox5.com>
parents: 12433
diff changeset
726 %!test
7242
e4398e3903be [project @ 2007-12-03 23:52:07 by dbateman]
dbateman
parents: 7237
diff changeset
727 %! [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
728 %! assert (s,1)
e4398e3903be [project @ 2007-12-03 23:52:07 by dbateman]
dbateman
parents: 7237
diff changeset
729 %! assert (e,10)
e4398e3903be [project @ 2007-12-03 23:52:07 by dbateman]
dbateman
parents: 7237
diff changeset
730 %! assert (size(te), [1,1])
e4398e3903be [project @ 2007-12-03 23:52:07 by dbateman]
dbateman
parents: 7237
diff changeset
731 %! assert (te{1}, [1 5; 7, 10])
e4398e3903be [project @ 2007-12-03 23:52:07 by dbateman]
dbateman
parents: 7237
diff changeset
732 %! assert (m{1},'short test')
e4398e3903be [project @ 2007-12-03 23:52:07 by dbateman]
dbateman
parents: 7237
diff changeset
733 %! assert (size(t),[1,1])
e4398e3903be [project @ 2007-12-03 23:52:07 by dbateman]
dbateman
parents: 7237
diff changeset
734 %! assert (t{1}{1},'short')
e4398e3903be [project @ 2007-12-03 23:52:07 by dbateman]
dbateman
parents: 7237
diff changeset
735 %! assert (t{1}{2},'test')
e4398e3903be [project @ 2007-12-03 23:52:07 by dbateman]
dbateman
parents: 7237
diff changeset
736 %! assert (size(nm), [1,1])
e4398e3903be [project @ 2007-12-03 23:52:07 by dbateman]
dbateman
parents: 7237
diff changeset
737 %! assert (!isempty(fieldnames(nm)))
e4398e3903be [project @ 2007-12-03 23:52:07 by dbateman]
dbateman
parents: 7237
diff changeset
738 %! assert (sort(fieldnames(nm)),{'word1';'word2'})
e4398e3903be [project @ 2007-12-03 23:52:07 by dbateman]
dbateman
parents: 7237
diff changeset
739 %! assert (nm.word1,'short')
e4398e3903be [project @ 2007-12-03 23:52:07 by dbateman]
dbateman
parents: 7237
diff changeset
740 %! assert (nm.word2,'test')
5582
6bf56668b01a [project @ 2005-12-15 01:08:20 by jwe]
jwe
parents:
diff changeset
741
12462
e4dbfe3019b1 Use PCRE regular expressions throughout Octave.
Rik <octave@nomad.inbox5.com>
parents: 12433
diff changeset
742 %!test
7242
e4398e3903be [project @ 2007-12-03 23:52:07 by dbateman]
dbateman
parents: 7237
diff changeset
743 %! [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
744 %! assert (s,1)
e4398e3903be [project @ 2007-12-03 23:52:07 by dbateman]
dbateman
parents: 7237
diff changeset
745 %! assert (e,10)
e4398e3903be [project @ 2007-12-03 23:52:07 by dbateman]
dbateman
parents: 7237
diff changeset
746 %! assert (size(te), [1,1])
e4398e3903be [project @ 2007-12-03 23:52:07 by dbateman]
dbateman
parents: 7237
diff changeset
747 %! assert (te{1}, [1 5; 7, 10])
e4398e3903be [project @ 2007-12-03 23:52:07 by dbateman]
dbateman
parents: 7237
diff changeset
748 %! assert (m{1},'short test')
e4398e3903be [project @ 2007-12-03 23:52:07 by dbateman]
dbateman
parents: 7237
diff changeset
749 %! assert (size(t),[1,1])
e4398e3903be [project @ 2007-12-03 23:52:07 by dbateman]
dbateman
parents: 7237
diff changeset
750 %! assert (t{1}{1},'short')
e4398e3903be [project @ 2007-12-03 23:52:07 by dbateman]
dbateman
parents: 7237
diff changeset
751 %! assert (t{1}{2},'test')
e4398e3903be [project @ 2007-12-03 23:52:07 by dbateman]
dbateman
parents: 7237
diff changeset
752 %! assert (size(nm), [1,1])
e4398e3903be [project @ 2007-12-03 23:52:07 by dbateman]
dbateman
parents: 7237
diff changeset
753 %! assert (!isempty(fieldnames(nm)))
e4398e3903be [project @ 2007-12-03 23:52:07 by dbateman]
dbateman
parents: 7237
diff changeset
754 %! assert (sort(fieldnames(nm)),{'word1';'word2'})
e4398e3903be [project @ 2007-12-03 23:52:07 by dbateman]
dbateman
parents: 7237
diff changeset
755 %! assert (nm.word1,'short')
e4398e3903be [project @ 2007-12-03 23:52:07 by dbateman]
dbateman
parents: 7237
diff changeset
756 %! assert (nm.word2,'test')
5619
e9112ff172b1 [project @ 2006-02-13 20:05:36 by dbateman]
dbateman
parents: 5582
diff changeset
757
12462
e4dbfe3019b1 Use PCRE regular expressions throughout Octave.
Rik <octave@nomad.inbox5.com>
parents: 12433
diff changeset
758 %!test
7242
e4398e3903be [project @ 2007-12-03 23:52:07 by dbateman]
dbateman
parents: 7237
diff changeset
759 %! [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
760 %! assert (size(t), [1,2]);
e4398e3903be [project @ 2007-12-03 23:52:07 by dbateman]
dbateman
parents: 7237
diff changeset
761 %! assert (t{1}{1},'John');
e4398e3903be [project @ 2007-12-03 23:52:07 by dbateman]
dbateman
parents: 7237
diff changeset
762 %! assert (t{1}{2},'Davis');
e4398e3903be [project @ 2007-12-03 23:52:07 by dbateman]
dbateman
parents: 7237
diff changeset
763 %! assert (t{2}{1},'Rogers');
e4398e3903be [project @ 2007-12-03 23:52:07 by dbateman]
dbateman
parents: 7237
diff changeset
764 %! assert (t{2}{2},'James');
e4398e3903be [project @ 2007-12-03 23:52:07 by dbateman]
dbateman
parents: 7237
diff changeset
765 %! assert (size(nm), [1,1]);
e4398e3903be [project @ 2007-12-03 23:52:07 by dbateman]
dbateman
parents: 7237
diff changeset
766 %! assert (nm.first{1},'John');
e4398e3903be [project @ 2007-12-03 23:52:07 by dbateman]
dbateman
parents: 7237
diff changeset
767 %! assert (nm.first{2},'James');
e4398e3903be [project @ 2007-12-03 23:52:07 by dbateman]
dbateman
parents: 7237
diff changeset
768 %! assert (nm.last{1},'Davis');
e4398e3903be [project @ 2007-12-03 23:52:07 by dbateman]
dbateman
parents: 7237
diff changeset
769 %! assert (nm.last{2},'Rogers');
5582
6bf56668b01a [project @ 2005-12-15 01:08:20 by jwe]
jwe
parents:
diff changeset
770
12462
e4dbfe3019b1 Use PCRE regular expressions throughout Octave.
Rik <octave@nomad.inbox5.com>
parents: 12433
diff changeset
771 %!test
10518
fcafe0e9bd58 Handle repeated matches in matches returned by pcre
David Bateman <dbateman@free.fr>
parents: 10504
diff changeset
772 %! # 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
773 %! 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
774
5779
12eeebfa7ead [project @ 2006-04-27 19:30:14 by dbateman]
dbateman
parents: 5775
diff changeset
775 %!assert(regexp("abc\nabc",'.'),[1:7])
12eeebfa7ead [project @ 2006-04-27 19:30:14 by dbateman]
dbateman
parents: 5775
diff changeset
776 %!assert(regexp("abc\nabc",'.','dotall'),[1:7])
12462
e4dbfe3019b1 Use PCRE regular expressions throughout Octave.
Rik <octave@nomad.inbox5.com>
parents: 12433
diff changeset
777 %!test
7242
e4398e3903be [project @ 2007-12-03 23:52:07 by dbateman]
dbateman
parents: 7237
diff changeset
778 %! assert(regexp("abc\nabc",'(?s).'),[1:7])
e4398e3903be [project @ 2007-12-03 23:52:07 by dbateman]
dbateman
parents: 7237
diff changeset
779 %! 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
780 %! 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
781
12eeebfa7ead [project @ 2006-04-27 19:30:14 by dbateman]
dbateman
parents: 5775
diff changeset
782 %!assert(regexp("caseCaSe",'case'),1)
12eeebfa7ead [project @ 2006-04-27 19:30:14 by dbateman]
dbateman
parents: 5775
diff changeset
783 %!assert(regexp("caseCaSe",'case',"matchcase"),1)
12eeebfa7ead [project @ 2006-04-27 19:30:14 by dbateman]
dbateman
parents: 5775
diff changeset
784 %!assert(regexp("caseCaSe",'case',"ignorecase"),[1,5])
12462
e4dbfe3019b1 Use PCRE regular expressions throughout Octave.
Rik <octave@nomad.inbox5.com>
parents: 12433
diff changeset
785 %!test
7242
e4398e3903be [project @ 2007-12-03 23:52:07 by dbateman]
dbateman
parents: 7237
diff changeset
786 %! assert(regexp("caseCaSe",'(?-i)case'),1)
e4398e3903be [project @ 2007-12-03 23:52:07 by dbateman]
dbateman
parents: 7237
diff changeset
787 %! assert(regexp("caseCaSe",'(?i)case'),[1,5])
5779
12eeebfa7ead [project @ 2006-04-27 19:30:14 by dbateman]
dbateman
parents: 5775
diff changeset
788
12eeebfa7ead [project @ 2006-04-27 19:30:14 by dbateman]
dbateman
parents: 5775
diff changeset
789 %!assert (regexp("abc\nabc",'c$'),7)
12eeebfa7ead [project @ 2006-04-27 19:30:14 by dbateman]
dbateman
parents: 5775
diff changeset
790 %!assert (regexp("abc\nabc",'c$',"stringanchors"),7)
12462
e4dbfe3019b1 Use PCRE regular expressions throughout Octave.
Rik <octave@nomad.inbox5.com>
parents: 12433
diff changeset
791 %!test
7242
e4398e3903be [project @ 2007-12-03 23:52:07 by dbateman]
dbateman
parents: 7237
diff changeset
792 %! assert (regexp("abc\nabc",'(?-m)c$'),7)
e4398e3903be [project @ 2007-12-03 23:52:07 by dbateman]
dbateman
parents: 7237
diff changeset
793 %! assert (regexp("abc\nabc",'c$',"lineanchors"),[3,7])
e4398e3903be [project @ 2007-12-03 23:52:07 by dbateman]
dbateman
parents: 7237
diff changeset
794 %! assert (regexp("abc\nabc",'(?m)c$'),[3,7])
5779
12eeebfa7ead [project @ 2006-04-27 19:30:14 by dbateman]
dbateman
parents: 5775
diff changeset
795
12eeebfa7ead [project @ 2006-04-27 19:30:14 by dbateman]
dbateman
parents: 5775
diff changeset
796 %!assert (regexp("this word",'s w'),4)
12eeebfa7ead [project @ 2006-04-27 19:30:14 by dbateman]
dbateman
parents: 5775
diff changeset
797 %!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
798 %!test
7242
e4398e3903be [project @ 2007-12-03 23:52:07 by dbateman]
dbateman
parents: 7237
diff changeset
799 %! assert (regexp("this word",'(?-x)s w','literalspacing'),4)
e4398e3903be [project @ 2007-12-03 23:52:07 by dbateman]
dbateman
parents: 7237
diff changeset
800 %! 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
801 %! 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
802
5582
6bf56668b01a [project @ 2005-12-15 01:08:20 by jwe]
jwe
parents:
diff changeset
803 %!error regexp('string', 'tri', 'BadArg');
6bf56668b01a [project @ 2005-12-15 01:08:20 by jwe]
jwe
parents:
diff changeset
804 %!error regexp('string');
6bf56668b01a [project @ 2005-12-15 01:08:20 by jwe]
jwe
parents:
diff changeset
805
6361
776e657c9422 [project @ 2007-02-27 09:45:03 by dbateman]
dbateman
parents: 5866
diff changeset
806 %!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
807 %!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
808 %!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
809 %!assert(regexp('Strings',{'t','s'}),{2,7})
776e657c9422 [project @ 2007-02-27 09:45:03 by dbateman]
dbateman
parents: 5866
diff changeset
810
8093
dcc31f473596 Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents: 8021
diff changeset
811 ## Test case for lookaround operators
12462
e4dbfe3019b1 Use PCRE regular expressions throughout Octave.
Rik <octave@nomad.inbox5.com>
parents: 12433
diff changeset
812 %!test
11025
df2152514429 Update docstrings for regular expression functions
Rik <octave@nomad.inbox5.com>
parents: 11018
diff changeset
813 %! assert(regexp('Iraq','q(?!u)'),4)
df2152514429 Update docstrings for regular expression functions
Rik <octave@nomad.inbox5.com>
parents: 11018
diff changeset
814 %! assert(regexp('quit','q(?!u)'), zeros(1,0))
df2152514429 Update docstrings for regular expression functions
Rik <octave@nomad.inbox5.com>
parents: 11018
diff changeset
815 %! assert(regexp('quit','q(?=u)','match'), {'q'})
df2152514429 Update docstrings for regular expression functions
Rik <octave@nomad.inbox5.com>
parents: 11018
diff changeset
816 %! assert(regexp("quit",'q(?=u+)','match'), {'q'})
df2152514429 Update docstrings for regular expression functions
Rik <octave@nomad.inbox5.com>
parents: 11018
diff changeset
817 %! 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
818 %! assert(regexp("qit",'q(?=u*)','match'), {'q'})
df2152514429 Update docstrings for regular expression functions
Rik <octave@nomad.inbox5.com>
parents: 11018
diff changeset
819 %! 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
820
13310
583940a28bfd handle "split" option for regexp
John W. Eaton <jwe@octave.org>
parents: 13227
diff changeset
821 ## Tests for split option.
583940a28bfd handle "split" option for regexp
John W. Eaton <jwe@octave.org>
parents: 13227
diff changeset
822 %!shared str
583940a28bfd handle "split" option for regexp
John W. Eaton <jwe@octave.org>
parents: 13227
diff changeset
823 %! str = "foo bar foo";
583940a28bfd handle "split" option for regexp
John W. Eaton <jwe@octave.org>
parents: 13227
diff changeset
824 %!test
583940a28bfd handle "split" option for regexp
John W. Eaton <jwe@octave.org>
parents: 13227
diff changeset
825 %! [a, b] = regexp (str, "f..", "match", "split");
583940a28bfd handle "split" option for regexp
John W. Eaton <jwe@octave.org>
parents: 13227
diff changeset
826 %! assert (a, {"foo", "foo"});
583940a28bfd handle "split" option for regexp
John W. Eaton <jwe@octave.org>
parents: 13227
diff changeset
827 %! assert (b, {"", " bar ", ""});
583940a28bfd handle "split" option for regexp
John W. Eaton <jwe@octave.org>
parents: 13227
diff changeset
828 %!test
583940a28bfd handle "split" option for regexp
John W. Eaton <jwe@octave.org>
parents: 13227
diff changeset
829 %! [a, b] = regexp (str, "f..", "match", "split", "once");
583940a28bfd handle "split" option for regexp
John W. Eaton <jwe@octave.org>
parents: 13227
diff changeset
830 %! assert (a, "foo");
583940a28bfd handle "split" option for regexp
John W. Eaton <jwe@octave.org>
parents: 13227
diff changeset
831 %! assert (b, {"", " bar foo"});
583940a28bfd handle "split" option for regexp
John W. Eaton <jwe@octave.org>
parents: 13227
diff changeset
832 %!test
583940a28bfd handle "split" option for regexp
John W. Eaton <jwe@octave.org>
parents: 13227
diff changeset
833 %! [a, b] = regexp (str, "fx.", "match", "split");
583940a28bfd handle "split" option for regexp
John W. Eaton <jwe@octave.org>
parents: 13227
diff changeset
834 %! assert (a, cell (1, 0));
583940a28bfd handle "split" option for regexp
John W. Eaton <jwe@octave.org>
parents: 13227
diff changeset
835 %! assert (b, {"foo bar foo"});
583940a28bfd handle "split" option for regexp
John W. Eaton <jwe@octave.org>
parents: 13227
diff changeset
836 %!test
583940a28bfd handle "split" option for regexp
John W. Eaton <jwe@octave.org>
parents: 13227
diff changeset
837 %! [a, b] = regexp (str, "fx.", "match", "split", "once");
583940a28bfd handle "split" option for regexp
John W. Eaton <jwe@octave.org>
parents: 13227
diff changeset
838 %! assert (a, "");
583940a28bfd handle "split" option for regexp
John W. Eaton <jwe@octave.org>
parents: 13227
diff changeset
839 %! assert (b, "foo bar foo")
583940a28bfd handle "split" option for regexp
John W. Eaton <jwe@octave.org>
parents: 13227
diff changeset
840
583940a28bfd handle "split" option for regexp
John W. Eaton <jwe@octave.org>
parents: 13227
diff changeset
841 %!shared str
583940a28bfd handle "split" option for regexp
John W. Eaton <jwe@octave.org>
parents: 13227
diff changeset
842 %! str = "foo bar";
583940a28bfd handle "split" option for regexp
John W. Eaton <jwe@octave.org>
parents: 13227
diff changeset
843 %!test
583940a28bfd handle "split" option for regexp
John W. Eaton <jwe@octave.org>
parents: 13227
diff changeset
844 %! [a, b] = regexp (str, "f..", "match", "split");
583940a28bfd handle "split" option for regexp
John W. Eaton <jwe@octave.org>
parents: 13227
diff changeset
845 %! assert (a, {"foo"});
583940a28bfd handle "split" option for regexp
John W. Eaton <jwe@octave.org>
parents: 13227
diff changeset
846 %! assert (b, {"", " bar"});
583940a28bfd handle "split" option for regexp
John W. Eaton <jwe@octave.org>
parents: 13227
diff changeset
847 %!test
583940a28bfd handle "split" option for regexp
John W. Eaton <jwe@octave.org>
parents: 13227
diff changeset
848 %! [a, b] = regexp (str, "b..", "match", "split");
583940a28bfd handle "split" option for regexp
John W. Eaton <jwe@octave.org>
parents: 13227
diff changeset
849 %! assert (a, {"bar"});
583940a28bfd handle "split" option for regexp
John W. Eaton <jwe@octave.org>
parents: 13227
diff changeset
850 %! assert (b, {"foo ", ""});
583940a28bfd handle "split" option for regexp
John W. Eaton <jwe@octave.org>
parents: 13227
diff changeset
851 %!test
583940a28bfd handle "split" option for regexp
John W. Eaton <jwe@octave.org>
parents: 13227
diff changeset
852 %! [a, b] = regexp (str, "x", "match", "split");
583940a28bfd handle "split" option for regexp
John W. Eaton <jwe@octave.org>
parents: 13227
diff changeset
853 %! assert (a, cell (1, 0));
583940a28bfd handle "split" option for regexp
John W. Eaton <jwe@octave.org>
parents: 13227
diff changeset
854 %! assert (b, {"foo bar"});
583940a28bfd handle "split" option for regexp
John W. Eaton <jwe@octave.org>
parents: 13227
diff changeset
855 %!test
583940a28bfd handle "split" option for regexp
John W. Eaton <jwe@octave.org>
parents: 13227
diff changeset
856 %! [a, b] = regexp (str, "[o]+", "match", "split");
583940a28bfd handle "split" option for regexp
John W. Eaton <jwe@octave.org>
parents: 13227
diff changeset
857 %! assert (a, {"oo"});
583940a28bfd handle "split" option for regexp
John W. Eaton <jwe@octave.org>
parents: 13227
diff changeset
858 %! assert (b, {"f", " bar"});
583940a28bfd handle "split" option for regexp
John W. Eaton <jwe@octave.org>
parents: 13227
diff changeset
859
5582
6bf56668b01a [project @ 2005-12-15 01:08:20 by jwe]
jwe
parents:
diff changeset
860 */
6bf56668b01a [project @ 2005-12-15 01:08:20 by jwe]
jwe
parents:
diff changeset
861
6549
5a5a09d7deb8 [project @ 2007-04-20 06:55:29 by jwe]
jwe
parents: 6547
diff changeset
862 DEFUN_DLD (regexpi, args, nargout,
5582
6bf56668b01a [project @ 2005-12-15 01:08:20 by jwe]
jwe
parents:
diff changeset
863 "-*- texinfo -*-\n\
10840
89f4d7e294cc Grammarcheck .cc files
Rik <octave@nomad.inbox5.com>
parents: 10551
diff changeset
864 @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
865 @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
866 \n\
11025
df2152514429 Update docstrings for regular expression functions
Rik <octave@nomad.inbox5.com>
parents: 11018
diff changeset
867 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
868 @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
869 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
870 syntax of the search pattern.\n\
df2152514429 Update docstrings for regular expression functions
Rik <octave@nomad.inbox5.com>
parents: 11018
diff changeset
871 @seealso{regexp}\n\
5582
6bf56668b01a [project @ 2005-12-15 01:08:20 by jwe]
jwe
parents:
diff changeset
872 @end deftypefn")
6bf56668b01a [project @ 2005-12-15 01:08:20 by jwe]
jwe
parents:
diff changeset
873 {
6361
776e657c9422 [project @ 2007-02-27 09:45:03 by dbateman]
dbateman
parents: 5866
diff changeset
874 octave_value_list retval;
13227
9559417aa965 maint: regexp.cc style fixes
John W. Eaton <jwe@octave.org>
parents: 12642
diff changeset
875
9559417aa965 maint: regexp.cc style fixes
John W. Eaton <jwe@octave.org>
parents: 12642
diff changeset
876 int nargin = args.length ();
6361
776e657c9422 [project @ 2007-02-27 09:45:03 by dbateman]
dbateman
parents: 5866
diff changeset
877
776e657c9422 [project @ 2007-02-27 09:45:03 by dbateman]
dbateman
parents: 5866
diff changeset
878 if (nargin < 2)
776e657c9422 [project @ 2007-02-27 09:45:03 by dbateman]
dbateman
parents: 5866
diff changeset
879 print_usage ();
13227
9559417aa965 maint: regexp.cc style fixes
John W. Eaton <jwe@octave.org>
parents: 12642
diff changeset
880 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
881 retval = octcellregexp (args, nargout, "regexpi", true);
776e657c9422 [project @ 2007-02-27 09:45:03 by dbateman]
dbateman
parents: 5866
diff changeset
882 else
776e657c9422 [project @ 2007-02-27 09:45:03 by dbateman]
dbateman
parents: 5866
diff changeset
883 retval = octregexp (args, nargout, "regexpi", true);
776e657c9422 [project @ 2007-02-27 09:45:03 by dbateman]
dbateman
parents: 5866
diff changeset
884
776e657c9422 [project @ 2007-02-27 09:45:03 by dbateman]
dbateman
parents: 5866
diff changeset
885 return retval;
5582
6bf56668b01a [project @ 2005-12-15 01:08:20 by jwe]
jwe
parents:
diff changeset
886 }
6bf56668b01a [project @ 2005-12-15 01:08:20 by jwe]
jwe
parents:
diff changeset
887
6bf56668b01a [project @ 2005-12-15 01:08:20 by jwe]
jwe
parents:
diff changeset
888 /*
6bf56668b01a [project @ 2005-12-15 01:08:20 by jwe]
jwe
parents:
diff changeset
889
6bf56668b01a [project @ 2005-12-15 01:08:20 by jwe]
jwe
parents:
diff changeset
890 ## seg-fault test
6bf56668b01a [project @ 2005-12-15 01:08:20 by jwe]
jwe
parents:
diff changeset
891 %!assert(regexpi("abcde","."),[1,2,3,4,5])
6bf56668b01a [project @ 2005-12-15 01:08:20 by jwe]
jwe
parents:
diff changeset
892
6bf56668b01a [project @ 2005-12-15 01:08:20 by jwe]
jwe
parents:
diff changeset
893 ## Check that anchoring of pattern works correctly
11025
df2152514429 Update docstrings for regular expression functions
Rik <octave@nomad.inbox5.com>
parents: 11018
diff changeset
894 %!assert(regexpi('abcabc','^ABC'),1);
df2152514429 Update docstrings for regular expression functions
Rik <octave@nomad.inbox5.com>
parents: 11018
diff changeset
895 %!assert(regexpi('abcabc','ABC$'),4);
df2152514429 Update docstrings for regular expression functions
Rik <octave@nomad.inbox5.com>
parents: 11018
diff changeset
896 %!assert(regexpi('abcabc','^ABC$'),zeros(1,0));
5582
6bf56668b01a [project @ 2005-12-15 01:08:20 by jwe]
jwe
parents:
diff changeset
897
6bf56668b01a [project @ 2005-12-15 01:08:20 by jwe]
jwe
parents:
diff changeset
898 %!test
6bf56668b01a [project @ 2005-12-15 01:08:20 by jwe]
jwe
parents:
diff changeset
899 %! [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
900 %! assert (s,zeros(1,0))
6b9cec830d72 [project @ 2006-05-03 19:32:46 by dbateman]
dbateman
parents: 5779
diff changeset
901 %! assert (e,zeros(1,0))
6b9cec830d72 [project @ 2006-05-03 19:32:46 by dbateman]
dbateman
parents: 5779
diff changeset
902 %! assert (te,cell(1,0))
6b9cec830d72 [project @ 2006-05-03 19:32:46 by dbateman]
dbateman
parents: 5779
diff changeset
903 %! assert (m, cell(1,0))
6b9cec830d72 [project @ 2006-05-03 19:32:46 by dbateman]
dbateman
parents: 5779
diff changeset
904 %! assert (t, cell(1,0))
5582
6bf56668b01a [project @ 2005-12-15 01:08:20 by jwe]
jwe
parents:
diff changeset
905
6bf56668b01a [project @ 2005-12-15 01:08:20 by jwe]
jwe
parents:
diff changeset
906 %!test
6bf56668b01a [project @ 2005-12-15 01:08:20 by jwe]
jwe
parents:
diff changeset
907 %! [s, e, te, m, t] = regexpi(' FiRetrUck ', 'f(.*)uck');
6bf56668b01a [project @ 2005-12-15 01:08:20 by jwe]
jwe
parents:
diff changeset
908 %! assert (s,2)
6bf56668b01a [project @ 2005-12-15 01:08:20 by jwe]
jwe
parents:
diff changeset
909 %! assert (e,10)
6bf56668b01a [project @ 2005-12-15 01:08:20 by jwe]
jwe
parents:
diff changeset
910 %! assert (te{1},[3,7])
6bf56668b01a [project @ 2005-12-15 01:08:20 by jwe]
jwe
parents:
diff changeset
911 %! assert (m{1}, 'FiRetrUck')
6bf56668b01a [project @ 2005-12-15 01:08:20 by jwe]
jwe
parents:
diff changeset
912 %! assert (t{1}{1}, 'iRetr')
6bf56668b01a [project @ 2005-12-15 01:08:20 by jwe]
jwe
parents:
diff changeset
913
6bf56668b01a [project @ 2005-12-15 01:08:20 by jwe]
jwe
parents:
diff changeset
914 %!test
6bf56668b01a [project @ 2005-12-15 01:08:20 by jwe]
jwe
parents:
diff changeset
915 %! [s, e, te, m, t] = regexpi(' firetruck ', 'f(.*)uck');
6bf56668b01a [project @ 2005-12-15 01:08:20 by jwe]
jwe
parents:
diff changeset
916 %! assert (s,2)
6bf56668b01a [project @ 2005-12-15 01:08:20 by jwe]
jwe
parents:
diff changeset
917 %! assert (e,10)
6bf56668b01a [project @ 2005-12-15 01:08:20 by jwe]
jwe
parents:
diff changeset
918 %! assert (te{1},[3,7])
6bf56668b01a [project @ 2005-12-15 01:08:20 by jwe]
jwe
parents:
diff changeset
919 %! assert (m{1}, 'firetruck')
6bf56668b01a [project @ 2005-12-15 01:08:20 by jwe]
jwe
parents:
diff changeset
920 %! assert (t{1}{1}, 'iretr')
6bf56668b01a [project @ 2005-12-15 01:08:20 by jwe]
jwe
parents:
diff changeset
921
6bf56668b01a [project @ 2005-12-15 01:08:20 by jwe]
jwe
parents:
diff changeset
922 %!test
6bf56668b01a [project @ 2005-12-15 01:08:20 by jwe]
jwe
parents:
diff changeset
923 %! [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
924 %! assert (s,[1,12])
6bf56668b01a [project @ 2005-12-15 01:08:20 by jwe]
jwe
parents:
diff changeset
925 %! assert (e,[5,17])
6bf56668b01a [project @ 2005-12-15 01:08:20 by jwe]
jwe
parents:
diff changeset
926 %! assert (size(te), [1,2])
6bf56668b01a [project @ 2005-12-15 01:08:20 by jwe]
jwe
parents:
diff changeset
927 %! assert (isempty(te{1}))
6bf56668b01a [project @ 2005-12-15 01:08:20 by jwe]
jwe
parents:
diff changeset
928 %! assert (isempty(te{2}))
6bf56668b01a [project @ 2005-12-15 01:08:20 by jwe]
jwe
parents:
diff changeset
929 %! assert (m{1},'ShoRt')
6bf56668b01a [project @ 2005-12-15 01:08:20 by jwe]
jwe
parents:
diff changeset
930 %! assert (m{2},'String')
6bf56668b01a [project @ 2005-12-15 01:08:20 by jwe]
jwe
parents:
diff changeset
931 %! assert (size(t), [1,2])
6bf56668b01a [project @ 2005-12-15 01:08:20 by jwe]
jwe
parents:
diff changeset
932 %! assert (isempty(t{1}))
6bf56668b01a [project @ 2005-12-15 01:08:20 by jwe]
jwe
parents:
diff changeset
933 %! assert (isempty(t{2}))
6bf56668b01a [project @ 2005-12-15 01:08:20 by jwe]
jwe
parents:
diff changeset
934
6bf56668b01a [project @ 2005-12-15 01:08:20 by jwe]
jwe
parents:
diff changeset
935 %!test
6bf56668b01a [project @ 2005-12-15 01:08:20 by jwe]
jwe
parents:
diff changeset
936 %! [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
937 %! assert (s,1)
6bf56668b01a [project @ 2005-12-15 01:08:20 by jwe]
jwe
parents:
diff changeset
938 %! assert (e,5)
7893
eb9ccb44ea41 make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents: 7520
diff changeset
939 %! assert (isempty(te))
eb9ccb44ea41 make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents: 7520
diff changeset
940 %! assert (m,'ShoRt')
eb9ccb44ea41 make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents: 7520
diff changeset
941 %! assert (isempty(t))
5582
6bf56668b01a [project @ 2005-12-15 01:08:20 by jwe]
jwe
parents:
diff changeset
942
6bf56668b01a [project @ 2005-12-15 01:08:20 by jwe]
jwe
parents:
diff changeset
943 %!test
6bf56668b01a [project @ 2005-12-15 01:08:20 by jwe]
jwe
parents:
diff changeset
944 %! [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
945 %! assert (s,1)
6bf56668b01a [project @ 2005-12-15 01:08:20 by jwe]
jwe
parents:
diff changeset
946 %! assert (e,5)
7893
eb9ccb44ea41 make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents: 7520
diff changeset
947 %! assert (isempty(te))
eb9ccb44ea41 make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents: 7520
diff changeset
948 %! assert (m,'ShoRt')
eb9ccb44ea41 make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents: 7520
diff changeset
949 %! assert (isempty(t))
5582
6bf56668b01a [project @ 2005-12-15 01:08:20 by jwe]
jwe
parents:
diff changeset
950
12462
e4dbfe3019b1 Use PCRE regular expressions throughout Octave.
Rik <octave@nomad.inbox5.com>
parents: 12433
diff changeset
951 %!test
7242
e4398e3903be [project @ 2007-12-03 23:52:07 by dbateman]
dbateman
parents: 7237
diff changeset
952 %! [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
953 %! assert (s,1)
e4398e3903be [project @ 2007-12-03 23:52:07 by dbateman]
dbateman
parents: 7237
diff changeset
954 %! assert (e,10)
e4398e3903be [project @ 2007-12-03 23:52:07 by dbateman]
dbateman
parents: 7237
diff changeset
955 %! assert (size(te), [1,1])
e4398e3903be [project @ 2007-12-03 23:52:07 by dbateman]
dbateman
parents: 7237
diff changeset
956 %! assert (te{1}, [1 5; 7, 10])
e4398e3903be [project @ 2007-12-03 23:52:07 by dbateman]
dbateman
parents: 7237
diff changeset
957 %! assert (m{1},'ShoRt Test')
e4398e3903be [project @ 2007-12-03 23:52:07 by dbateman]
dbateman
parents: 7237
diff changeset
958 %! assert (size(t),[1,1])
e4398e3903be [project @ 2007-12-03 23:52:07 by dbateman]
dbateman
parents: 7237
diff changeset
959 %! assert (t{1}{1},'ShoRt')
e4398e3903be [project @ 2007-12-03 23:52:07 by dbateman]
dbateman
parents: 7237
diff changeset
960 %! assert (t{1}{2},'Test')
e4398e3903be [project @ 2007-12-03 23:52:07 by dbateman]
dbateman
parents: 7237
diff changeset
961 %! assert (size(nm), [1,1])
e4398e3903be [project @ 2007-12-03 23:52:07 by dbateman]
dbateman
parents: 7237
diff changeset
962 %! assert (!isempty(fieldnames(nm)))
e4398e3903be [project @ 2007-12-03 23:52:07 by dbateman]
dbateman
parents: 7237
diff changeset
963 %! assert (sort(fieldnames(nm)),{'word1';'word2'})
e4398e3903be [project @ 2007-12-03 23:52:07 by dbateman]
dbateman
parents: 7237
diff changeset
964 %! assert (nm.word1,'ShoRt')
e4398e3903be [project @ 2007-12-03 23:52:07 by dbateman]
dbateman
parents: 7237
diff changeset
965 %! assert (nm.word2,'Test')
5582
6bf56668b01a [project @ 2005-12-15 01:08:20 by jwe]
jwe
parents:
diff changeset
966
12462
e4dbfe3019b1 Use PCRE regular expressions throughout Octave.
Rik <octave@nomad.inbox5.com>
parents: 12433
diff changeset
967 %!test
7242
e4398e3903be [project @ 2007-12-03 23:52:07 by dbateman]
dbateman
parents: 7237
diff changeset
968 %! [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
969 %! assert (s,1)
e4398e3903be [project @ 2007-12-03 23:52:07 by dbateman]
dbateman
parents: 7237
diff changeset
970 %! assert (e,10)
e4398e3903be [project @ 2007-12-03 23:52:07 by dbateman]
dbateman
parents: 7237
diff changeset
971 %! assert (size(te), [1,1])
e4398e3903be [project @ 2007-12-03 23:52:07 by dbateman]
dbateman
parents: 7237
diff changeset
972 %! assert (te{1}, [1 5; 7, 10])
e4398e3903be [project @ 2007-12-03 23:52:07 by dbateman]
dbateman
parents: 7237
diff changeset
973 %! assert (m{1},'ShoRt Test')
e4398e3903be [project @ 2007-12-03 23:52:07 by dbateman]
dbateman
parents: 7237
diff changeset
974 %! assert (size(t),[1,1])
e4398e3903be [project @ 2007-12-03 23:52:07 by dbateman]
dbateman
parents: 7237
diff changeset
975 %! assert (t{1}{1},'ShoRt')
e4398e3903be [project @ 2007-12-03 23:52:07 by dbateman]
dbateman
parents: 7237
diff changeset
976 %! assert (t{1}{2},'Test')
e4398e3903be [project @ 2007-12-03 23:52:07 by dbateman]
dbateman
parents: 7237
diff changeset
977 %! assert (size(nm), [1,1])
e4398e3903be [project @ 2007-12-03 23:52:07 by dbateman]
dbateman
parents: 7237
diff changeset
978 %! assert (!isempty(fieldnames(nm)))
e4398e3903be [project @ 2007-12-03 23:52:07 by dbateman]
dbateman
parents: 7237
diff changeset
979 %! assert (sort(fieldnames(nm)),{'word1';'word2'})
e4398e3903be [project @ 2007-12-03 23:52:07 by dbateman]
dbateman
parents: 7237
diff changeset
980 %! assert (nm.word1,'ShoRt')
e4398e3903be [project @ 2007-12-03 23:52:07 by dbateman]
dbateman
parents: 7237
diff changeset
981 %! assert (nm.word2,'Test')
5582
6bf56668b01a [project @ 2005-12-15 01:08:20 by jwe]
jwe
parents:
diff changeset
982
5779
12eeebfa7ead [project @ 2006-04-27 19:30:14 by dbateman]
dbateman
parents: 5775
diff changeset
983 %!assert(regexpi("abc\nabc",'.'),[1:7])
12eeebfa7ead [project @ 2006-04-27 19:30:14 by dbateman]
dbateman
parents: 5775
diff changeset
984 %!assert(regexpi("abc\nabc",'.','dotall'),[1:7])
12462
e4dbfe3019b1 Use PCRE regular expressions throughout Octave.
Rik <octave@nomad.inbox5.com>
parents: 12433
diff changeset
985 %!test
7242
e4398e3903be [project @ 2007-12-03 23:52:07 by dbateman]
dbateman
parents: 7237
diff changeset
986 %! assert(regexpi("abc\nabc",'(?s).'),[1:7])
e4398e3903be [project @ 2007-12-03 23:52:07 by dbateman]
dbateman
parents: 7237
diff changeset
987 %! 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
988 %! 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
989
12eeebfa7ead [project @ 2006-04-27 19:30:14 by dbateman]
dbateman
parents: 5775
diff changeset
990 %!assert(regexpi("caseCaSe",'case'),[1,5])
12eeebfa7ead [project @ 2006-04-27 19:30:14 by dbateman]
dbateman
parents: 5775
diff changeset
991 %!assert(regexpi("caseCaSe",'case',"matchcase"),1)
12eeebfa7ead [project @ 2006-04-27 19:30:14 by dbateman]
dbateman
parents: 5775
diff changeset
992 %!assert(regexpi("caseCaSe",'case',"ignorecase"),[1,5])
12462
e4dbfe3019b1 Use PCRE regular expressions throughout Octave.
Rik <octave@nomad.inbox5.com>
parents: 12433
diff changeset
993 %!test
7242
e4398e3903be [project @ 2007-12-03 23:52:07 by dbateman]
dbateman
parents: 7237
diff changeset
994 %! assert(regexpi("caseCaSe",'(?-i)case'),1)
e4398e3903be [project @ 2007-12-03 23:52:07 by dbateman]
dbateman
parents: 7237
diff changeset
995 %! assert(regexpi("caseCaSe",'(?i)case'),[1,5])
5779
12eeebfa7ead [project @ 2006-04-27 19:30:14 by dbateman]
dbateman
parents: 5775
diff changeset
996
11025
df2152514429 Update docstrings for regular expression functions
Rik <octave@nomad.inbox5.com>
parents: 11018
diff changeset
997 %!assert (regexpi("abc\nabc",'C$'),7)
df2152514429 Update docstrings for regular expression functions
Rik <octave@nomad.inbox5.com>
parents: 11018
diff changeset
998 %!assert (regexpi("abc\nabc",'C$',"stringanchors"),7)
12462
e4dbfe3019b1 Use PCRE regular expressions throughout Octave.
Rik <octave@nomad.inbox5.com>
parents: 12433
diff changeset
999 %!test
11025
df2152514429 Update docstrings for regular expression functions
Rik <octave@nomad.inbox5.com>
parents: 11018
diff changeset
1000 %! assert (regexpi("abc\nabc",'(?-m)C$'),7)
df2152514429 Update docstrings for regular expression functions
Rik <octave@nomad.inbox5.com>
parents: 11018
diff changeset
1001 %! assert (regexpi("abc\nabc",'C$',"lineanchors"),[3,7])
df2152514429 Update docstrings for regular expression functions
Rik <octave@nomad.inbox5.com>
parents: 11018
diff changeset
1002 %! assert (regexpi("abc\nabc",'(?m)C$'),[3,7])
5779
12eeebfa7ead [project @ 2006-04-27 19:30:14 by dbateman]
dbateman
parents: 5775
diff changeset
1003
11025
df2152514429 Update docstrings for regular expression functions
Rik <octave@nomad.inbox5.com>
parents: 11018
diff changeset
1004 %!assert (regexpi("this word",'S w'),4)
df2152514429 Update docstrings for regular expression functions
Rik <octave@nomad.inbox5.com>
parents: 11018
diff changeset
1005 %!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
1006 %!test
11025
df2152514429 Update docstrings for regular expression functions
Rik <octave@nomad.inbox5.com>
parents: 11018
diff changeset
1007 %! 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
1008 %! 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
1009 %! 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
1010
5582
6bf56668b01a [project @ 2005-12-15 01:08:20 by jwe]
jwe
parents:
diff changeset
1011 %!error regexpi('string', 'tri', 'BadArg');
6bf56668b01a [project @ 2005-12-15 01:08:20 by jwe]
jwe
parents:
diff changeset
1012 %!error regexpi('string');
6bf56668b01a [project @ 2005-12-15 01:08:20 by jwe]
jwe
parents:
diff changeset
1013
6361
776e657c9422 [project @ 2007-02-27 09:45:03 by dbateman]
dbateman
parents: 5866
diff changeset
1014 %!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
1015 %!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
1016 %!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
1017 %!assert(regexpi('Strings',{'t','s'}),{2,[1,7]})
776e657c9422 [project @ 2007-02-27 09:45:03 by dbateman]
dbateman
parents: 5866
diff changeset
1018
5582
6bf56668b01a [project @ 2005-12-15 01:08:20 by jwe]
jwe
parents:
diff changeset
1019 */
6bf56668b01a [project @ 2005-12-15 01:08:20 by jwe]
jwe
parents:
diff changeset
1020
6361
776e657c9422 [project @ 2007-02-27 09:45:03 by dbateman]
dbateman
parents: 5866
diff changeset
1021
776e657c9422 [project @ 2007-02-27 09:45:03 by dbateman]
dbateman
parents: 5866
diff changeset
1022 static octave_value
14024
fc9f204faea0 refactor regexp (bug #34440)
John W. Eaton <jwe@octave.org>
parents: 13929
diff changeset
1023 octregexprep (const octave_value_list &args, const std::string &who)
5785
6b9cec830d72 [project @ 2006-05-03 19:32:46 by dbateman]
dbateman
parents: 5779
diff changeset
1024 {
6361
776e657c9422 [project @ 2007-02-27 09:45:03 by dbateman]
dbateman
parents: 5866
diff changeset
1025 octave_value retval;
5785
6b9cec830d72 [project @ 2006-05-03 19:32:46 by dbateman]
dbateman
parents: 5779
diff changeset
1026
13227
9559417aa965 maint: regexp.cc style fixes
John W. Eaton <jwe@octave.org>
parents: 12642
diff changeset
1027 int nargin = args.length ();
9559417aa965 maint: regexp.cc style fixes
John W. Eaton <jwe@octave.org>
parents: 12642
diff changeset
1028
9559417aa965 maint: regexp.cc style fixes
John W. Eaton <jwe@octave.org>
parents: 12642
diff changeset
1029 // Make sure we have string, pattern, replacement
5785
6b9cec830d72 [project @ 2006-05-03 19:32:46 by dbateman]
dbateman
parents: 5779
diff changeset
1030 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
1031 if (error_state)
9559417aa965 maint: regexp.cc style fixes
John W. Eaton <jwe@octave.org>
parents: 12642
diff changeset
1032 return retval;
9559417aa965 maint: regexp.cc style fixes
John W. Eaton <jwe@octave.org>
parents: 12642
diff changeset
1033
5785
6b9cec830d72 [project @ 2006-05-03 19:32:46 by dbateman]
dbateman
parents: 5779
diff changeset
1034 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
1035 if (error_state)
9559417aa965 maint: regexp.cc style fixes
John W. Eaton <jwe@octave.org>
parents: 12642
diff changeset
1036 return retval;
9559417aa965 maint: regexp.cc style fixes
John W. Eaton <jwe@octave.org>
parents: 12642
diff changeset
1037
5785
6b9cec830d72 [project @ 2006-05-03 19:32:46 by dbateman]
dbateman
parents: 5779
diff changeset
1038 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
1039 if (error_state)
9559417aa965 maint: regexp.cc style fixes
John W. Eaton <jwe@octave.org>
parents: 12642
diff changeset
1040 return retval;
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11572
diff changeset
1041
5785
6b9cec830d72 [project @ 2006-05-03 19:32:46 by dbateman]
dbateman
parents: 5779
diff changeset
1042 // Pack options excluding 'tokenize' and various output
6b9cec830d72 [project @ 2006-05-03 19:32:46 by dbateman]
dbateman
parents: 5779
diff changeset
1043 // reordering strings into regexp arg list
14024
fc9f204faea0 refactor regexp (bug #34440)
John W. Eaton <jwe@octave.org>
parents: 13929
diff changeset
1044 octave_value_list regexpargs (nargin-3, octave_value ());
13227
9559417aa965 maint: regexp.cc style fixes
John W. Eaton <jwe@octave.org>
parents: 12642
diff changeset
1045
14024
fc9f204faea0 refactor regexp (bug #34440)
John W. Eaton <jwe@octave.org>
parents: 13929
diff changeset
1046 int len = 0;
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11572
diff changeset
1047 for (int i = 3; i < nargin; i++)
5785
6b9cec830d72 [project @ 2006-05-03 19:32:46 by dbateman]
dbateman
parents: 5779
diff changeset
1048 {
13227
9559417aa965 maint: regexp.cc style fixes
John W. Eaton <jwe@octave.org>
parents: 12642
diff changeset
1049 const std::string opt = args(i).string_value ();
5785
6b9cec830d72 [project @ 2006-05-03 19:32:46 by dbateman]
dbateman
parents: 5779
diff changeset
1050 if (opt != "tokenize" && opt != "start" && opt != "end"
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9064
diff changeset
1051 && opt != "tokenextents" && opt != "match" && opt != "tokens"
13310
583940a28bfd handle "split" option for regexp
John W. Eaton <jwe@octave.org>
parents: 13227
diff changeset
1052 && opt != "names" && opt != "split" && opt != "warnings")
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9064
diff changeset
1053 {
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9064
diff changeset
1054 regexpargs(len++) = args(i);
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9064
diff changeset
1055 }
5785
6b9cec830d72 [project @ 2006-05-03 19:32:46 by dbateman]
dbateman
parents: 5779
diff changeset
1056 }
13227
9559417aa965 maint: regexp.cc style fixes
John W. Eaton <jwe@octave.org>
parents: 12642
diff changeset
1057 regexpargs.resize (len);
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11572
diff changeset
1058
14024
fc9f204faea0 refactor regexp (bug #34440)
John W. Eaton <jwe@octave.org>
parents: 13929
diff changeset
1059 regexp::opts options;
fc9f204faea0 refactor regexp (bug #34440)
John W. Eaton <jwe@octave.org>
parents: 13929
diff changeset
1060 bool extra_args = false;
fc9f204faea0 refactor regexp (bug #34440)
John W. Eaton <jwe@octave.org>
parents: 13929
diff changeset
1061 parse_options (options, regexpargs, who, 0, extra_args);
fc9f204faea0 refactor regexp (bug #34440)
John W. Eaton <jwe@octave.org>
parents: 13929
diff changeset
1062 if (error_state)
fc9f204faea0 refactor regexp (bug #34440)
John W. Eaton <jwe@octave.org>
parents: 13929
diff changeset
1063 return retval;
5785
6b9cec830d72 [project @ 2006-05-03 19:32:46 by dbateman]
dbateman
parents: 5779
diff changeset
1064
14024
fc9f204faea0 refactor regexp (bug #34440)
John W. Eaton <jwe@octave.org>
parents: 13929
diff changeset
1065 return regexp_replace (pattern, buffer, replacement, options, who);
6361
776e657c9422 [project @ 2007-02-27 09:45:03 by dbateman]
dbateman
parents: 5866
diff changeset
1066 }
776e657c9422 [project @ 2007-02-27 09:45:03 by dbateman]
dbateman
parents: 5866
diff changeset
1067
6549
5a5a09d7deb8 [project @ 2007-04-20 06:55:29 by jwe]
jwe
parents: 6547
diff changeset
1068 DEFUN_DLD (regexprep, args, ,
6361
776e657c9422 [project @ 2007-02-27 09:45:03 by dbateman]
dbateman
parents: 5866
diff changeset
1069 "-*- texinfo -*-\n\
11025
df2152514429 Update docstrings for regular expression functions
Rik <octave@nomad.inbox5.com>
parents: 11018
diff changeset
1070 @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
1071 @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
1072 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
1073 \n\
11025
df2152514429 Update docstrings for regular expression functions
Rik <octave@nomad.inbox5.com>
parents: 11018
diff changeset
1074 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
1075 @xref{doc-regexp,,regexp}.\n\
df2152514429 Update docstrings for regular expression functions
Rik <octave@nomad.inbox5.com>
parents: 11018
diff changeset
1076 \n\
df2152514429 Update docstrings for regular expression functions
Rik <octave@nomad.inbox5.com>
parents: 11018
diff changeset
1077 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
1078 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
1079 \n\
6361
776e657c9422 [project @ 2007-02-27 09:45:03 by dbateman]
dbateman
parents: 5866
diff changeset
1080 @example\n\
11025
df2152514429 Update docstrings for regular expression functions
Rik <octave@nomad.inbox5.com>
parents: 11018
diff changeset
1081 regexprep(\"Bill Dunn\",'(\\w+) (\\w+)','$2, $1')\n\
6361
776e657c9422 [project @ 2007-02-27 09:45:03 by dbateman]
dbateman
parents: 5866
diff changeset
1082 @end example\n\
10840
89f4d7e294cc Grammarcheck .cc files
Rik <octave@nomad.inbox5.com>
parents: 10551
diff changeset
1083 \n\
10846
a4f482e66b65 Grammarcheck more of the documentation.
Rik <octave@nomad.inbox5.com>
parents: 10840
diff changeset
1084 @noindent\n\
6361
776e657c9422 [project @ 2007-02-27 09:45:03 by dbateman]
dbateman
parents: 5866
diff changeset
1085 returns \"Dunn, Bill\"\n\
776e657c9422 [project @ 2007-02-27 09:45:03 by dbateman]
dbateman
parents: 5866
diff changeset
1086 \n\
11025
df2152514429 Update docstrings for regular expression functions
Rik <octave@nomad.inbox5.com>
parents: 11018
diff changeset
1087 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
1088 \n\
6361
776e657c9422 [project @ 2007-02-27 09:45:03 by dbateman]
dbateman
parents: 5866
diff changeset
1089 @table @samp\n\
776e657c9422 [project @ 2007-02-27 09:45:03 by dbateman]
dbateman
parents: 5866
diff changeset
1090 \n\
776e657c9422 [project @ 2007-02-27 09:45:03 by dbateman]
dbateman
parents: 5866
diff changeset
1091 @item once\n\
7001
8b0cfeb06365 [project @ 2007-10-10 18:02:59 by jwe]
jwe
parents: 6678
diff changeset
1092 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
1093 \n\
776e657c9422 [project @ 2007-02-27 09:45:03 by dbateman]
dbateman
parents: 5866
diff changeset
1094 @item warnings\n\
776e657c9422 [project @ 2007-02-27 09:45:03 by dbateman]
dbateman
parents: 5866
diff changeset
1095 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
1096 \n\
776e657c9422 [project @ 2007-02-27 09:45:03 by dbateman]
dbateman
parents: 5866
diff changeset
1097 @end table\n\
11572
7d6d8c1e471f Grammarcheck Texinfo for files in src directory.
Rik <octave@nomad.inbox5.com>
parents: 11553
diff changeset
1098 @seealso{regexp, regexpi, strrep}\n\
6361
776e657c9422 [project @ 2007-02-27 09:45:03 by dbateman]
dbateman
parents: 5866
diff changeset
1099 @end deftypefn")
776e657c9422 [project @ 2007-02-27 09:45:03 by dbateman]
dbateman
parents: 5866
diff changeset
1100 {
776e657c9422 [project @ 2007-02-27 09:45:03 by dbateman]
dbateman
parents: 5866
diff changeset
1101 octave_value_list retval;
13227
9559417aa965 maint: regexp.cc style fixes
John W. Eaton <jwe@octave.org>
parents: 12642
diff changeset
1102 int nargin = args.length ();
6361
776e657c9422 [project @ 2007-02-27 09:45:03 by dbateman]
dbateman
parents: 5866
diff changeset
1103
776e657c9422 [project @ 2007-02-27 09:45:03 by dbateman]
dbateman
parents: 5866
diff changeset
1104 if (nargin < 3)
776e657c9422 [project @ 2007-02-27 09:45:03 by dbateman]
dbateman
parents: 5866
diff changeset
1105 {
776e657c9422 [project @ 2007-02-27 09:45:03 by dbateman]
dbateman
parents: 5866
diff changeset
1106 print_usage ();
776e657c9422 [project @ 2007-02-27 09:45:03 by dbateman]
dbateman
parents: 5866
diff changeset
1107 return retval;
776e657c9422 [project @ 2007-02-27 09:45:03 by dbateman]
dbateman
parents: 5866
diff changeset
1108 }
776e657c9422 [project @ 2007-02-27 09:45:03 by dbateman]
dbateman
parents: 5866
diff changeset
1109
13227
9559417aa965 maint: regexp.cc style fixes
John W. Eaton <jwe@octave.org>
parents: 12642
diff changeset
1110 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
1111 {
776e657c9422 [project @ 2007-02-27 09:45:03 by dbateman]
dbateman
parents: 5866
diff changeset
1112 Cell str;
776e657c9422 [project @ 2007-02-27 09:45:03 by dbateman]
dbateman
parents: 5866
diff changeset
1113 Cell pat;
776e657c9422 [project @ 2007-02-27 09:45:03 by dbateman]
dbateman
parents: 5866
diff changeset
1114 Cell rep;
6495
fd09c7e8c4c9 [project @ 2007-04-05 16:18:20 by dbateman]
dbateman
parents: 6361
diff changeset
1115 dim_vector dv0;
13227
9559417aa965 maint: regexp.cc style fixes
John W. Eaton <jwe@octave.org>
parents: 12642
diff changeset
1116 dim_vector dv1 (1, 1);
6361
776e657c9422 [project @ 2007-02-27 09:45:03 by dbateman]
dbateman
parents: 5866
diff changeset
1117
13227
9559417aa965 maint: regexp.cc style fixes
John W. Eaton <jwe@octave.org>
parents: 12642
diff changeset
1118 if (args(0).is_cell ())
9559417aa965 maint: regexp.cc style fixes
John W. Eaton <jwe@octave.org>
parents: 12642
diff changeset
1119 str = args(0).cell_value ();
6361
776e657c9422 [project @ 2007-02-27 09:45:03 by dbateman]
dbateman
parents: 5866
diff changeset
1120 else
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9064
diff changeset
1121 str = Cell (args(0));
6361
776e657c9422 [project @ 2007-02-27 09:45:03 by dbateman]
dbateman
parents: 5866
diff changeset
1122
13227
9559417aa965 maint: regexp.cc style fixes
John W. Eaton <jwe@octave.org>
parents: 12642
diff changeset
1123 if (args(1).is_cell ())
9559417aa965 maint: regexp.cc style fixes
John W. Eaton <jwe@octave.org>
parents: 12642
diff changeset
1124 pat = args(1).cell_value ();
6361
776e657c9422 [project @ 2007-02-27 09:45:03 by dbateman]
dbateman
parents: 5866
diff changeset
1125 else
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9064
diff changeset
1126 pat = Cell (args(1));
6361
776e657c9422 [project @ 2007-02-27 09:45:03 by dbateman]
dbateman
parents: 5866
diff changeset
1127
13227
9559417aa965 maint: regexp.cc style fixes
John W. Eaton <jwe@octave.org>
parents: 12642
diff changeset
1128 if (args(2).is_cell ())
9559417aa965 maint: regexp.cc style fixes
John W. Eaton <jwe@octave.org>
parents: 12642
diff changeset
1129 rep = args(2).cell_value ();
6361
776e657c9422 [project @ 2007-02-27 09:45:03 by dbateman]
dbateman
parents: 5866
diff changeset
1130 else
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9064
diff changeset
1131 rep = Cell (args(2));
6361
776e657c9422 [project @ 2007-02-27 09:45:03 by dbateman]
dbateman
parents: 5866
diff changeset
1132
13227
9559417aa965 maint: regexp.cc style fixes
John W. Eaton <jwe@octave.org>
parents: 12642
diff changeset
1133 dv0 = str.dims ();
9559417aa965 maint: regexp.cc style fixes
John W. Eaton <jwe@octave.org>
parents: 12642
diff changeset
1134 if (pat.numel () != 1)
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9064
diff changeset
1135 {
13227
9559417aa965 maint: regexp.cc style fixes
John W. Eaton <jwe@octave.org>
parents: 12642
diff changeset
1136 dv1 = pat.dims ();
9559417aa965 maint: regexp.cc style fixes
John W. Eaton <jwe@octave.org>
parents: 12642
diff changeset
1137 if (rep.numel () != 1 && dv1 != rep.dims ())
14024
fc9f204faea0 refactor regexp (bug #34440)
John W. Eaton <jwe@octave.org>
parents: 13929
diff changeset
1138 error ("regexprep: inconsistent cell array dimensions");
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9064
diff changeset
1139 }
13227
9559417aa965 maint: regexp.cc style fixes
John W. Eaton <jwe@octave.org>
parents: 12642
diff changeset
1140 else if (rep.numel () != 1)
9559417aa965 maint: regexp.cc style fixes
John W. Eaton <jwe@octave.org>
parents: 12642
diff changeset
1141 dv1 = rep.dims ();
6361
776e657c9422 [project @ 2007-02-27 09:45:03 by dbateman]
dbateman
parents: 5866
diff changeset
1142
776e657c9422 [project @ 2007-02-27 09:45:03 by dbateman]
dbateman
parents: 5866
diff changeset
1143 if (!error_state)
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9064
diff changeset
1144 {
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9064
diff changeset
1145 Cell ret (dv0);
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9064
diff changeset
1146 octave_value_list new_args = args;
6361
776e657c9422 [project @ 2007-02-27 09:45:03 by dbateman]
dbateman
parents: 5866
diff changeset
1147
13227
9559417aa965 maint: regexp.cc style fixes
John W. Eaton <jwe@octave.org>
parents: 12642
diff changeset
1148 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
1149 {
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9064
diff changeset
1150 new_args(0) = str(i);
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9064
diff changeset
1151 if (pat.numel() == 1)
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9064
diff changeset
1152 new_args(1) = pat(0);
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9064
diff changeset
1153 if (rep.numel() == 1)
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9064
diff changeset
1154 new_args(2) = rep(0);
13227
9559417aa965 maint: regexp.cc style fixes
John W. Eaton <jwe@octave.org>
parents: 12642
diff changeset
1155
9559417aa965 maint: regexp.cc style fixes
John W. Eaton <jwe@octave.org>
parents: 12642
diff changeset
1156 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
1157 {
13227
9559417aa965 maint: regexp.cc style fixes
John W. Eaton <jwe@octave.org>
parents: 12642
diff changeset
1158 if (pat.numel () != 1)
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9064
diff changeset
1159 new_args(1) = pat(j);
13227
9559417aa965 maint: regexp.cc style fixes
John W. Eaton <jwe@octave.org>
parents: 12642
diff changeset
1160 if (rep.numel () != 1)
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9064
diff changeset
1161 new_args(2) = rep(j);
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9064
diff changeset
1162 new_args(0) = octregexprep (new_args, "regexprep");
6361
776e657c9422 [project @ 2007-02-27 09:45:03 by dbateman]
dbateman
parents: 5866
diff changeset
1163
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9064
diff changeset
1164 if (error_state)
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9064
diff changeset
1165 break;
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9064
diff changeset
1166 }
6361
776e657c9422 [project @ 2007-02-27 09:45:03 by dbateman]
dbateman
parents: 5866
diff changeset
1167
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9064
diff changeset
1168 if (error_state)
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9064
diff changeset
1169 break;
6495
fd09c7e8c4c9 [project @ 2007-04-05 16:18:20 by dbateman]
dbateman
parents: 6361
diff changeset
1170
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9064
diff changeset
1171 ret(i) = new_args(0);
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9064
diff changeset
1172 }
6361
776e657c9422 [project @ 2007-02-27 09:45:03 by dbateman]
dbateman
parents: 5866
diff changeset
1173
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9064
diff changeset
1174 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
1175 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
1176 ? octave_value (ret) : octave_value (ret(0));
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9064
diff changeset
1177 }
6361
776e657c9422 [project @ 2007-02-27 09:45:03 by dbateman]
dbateman
parents: 5866
diff changeset
1178 }
776e657c9422 [project @ 2007-02-27 09:45:03 by dbateman]
dbateman
parents: 5866
diff changeset
1179 else
776e657c9422 [project @ 2007-02-27 09:45:03 by dbateman]
dbateman
parents: 5866
diff changeset
1180 retval = octregexprep (args, "regexprep");
776e657c9422 [project @ 2007-02-27 09:45:03 by dbateman]
dbateman
parents: 5866
diff changeset
1181
5785
6b9cec830d72 [project @ 2006-05-03 19:32:46 by dbateman]
dbateman
parents: 5779
diff changeset
1182 return retval;
6b9cec830d72 [project @ 2006-05-03 19:32:46 by dbateman]
dbateman
parents: 5779
diff changeset
1183 }
6b9cec830d72 [project @ 2006-05-03 19:32:46 by dbateman]
dbateman
parents: 5779
diff changeset
1184
6b9cec830d72 [project @ 2006-05-03 19:32:46 by dbateman]
dbateman
parents: 5779
diff changeset
1185 /*
6b9cec830d72 [project @ 2006-05-03 19:32:46 by dbateman]
dbateman
parents: 5779
diff changeset
1186 %!test # Replace with empty
6b9cec830d72 [project @ 2006-05-03 19:32:46 by dbateman]
dbateman
parents: 5779
diff changeset
1187 %! 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
1188 %! t = regexprep(xml,'<[!?][^>]*>','');
6b9cec830d72 [project @ 2006-05-03 19:32:46 by dbateman]
dbateman
parents: 5779
diff changeset
1189 %! assert(t,' <tag v="hello">some stuff</tag>')
6b9cec830d72 [project @ 2006-05-03 19:32:46 by dbateman]
dbateman
parents: 5779
diff changeset
1190
6b9cec830d72 [project @ 2006-05-03 19:32:46 by dbateman]
dbateman
parents: 5779
diff changeset
1191 %!test # Replace with non-empty
6b9cec830d72 [project @ 2006-05-03 19:32:46 by dbateman]
dbateman
parents: 5779
diff changeset
1192 %! 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
1193 %! t = regexprep(xml,'<[!?][^>]*>','?');
6b9cec830d72 [project @ 2006-05-03 19:32:46 by dbateman]
dbateman
parents: 5779
diff changeset
1194 %! assert(t,'? <tag v="hello">some stuff?</tag>')
6b9cec830d72 [project @ 2006-05-03 19:32:46 by dbateman]
dbateman
parents: 5779
diff changeset
1195
6b9cec830d72 [project @ 2006-05-03 19:32:46 by dbateman]
dbateman
parents: 5779
diff changeset
1196 %!test # Check that 'tokenize' is ignored
6b9cec830d72 [project @ 2006-05-03 19:32:46 by dbateman]
dbateman
parents: 5779
diff changeset
1197 %! 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
1198 %! t = regexprep(xml,'<[!?][^>]*>','','tokenize');
6b9cec830d72 [project @ 2006-05-03 19:32:46 by dbateman]
dbateman
parents: 5779
diff changeset
1199 %! assert(t,' <tag v="hello">some stuff</tag>')
6b9cec830d72 [project @ 2006-05-03 19:32:46 by dbateman]
dbateman
parents: 5779
diff changeset
1200
11032
c9b0a75b02e8 Make all regexp in Octave compatible with both POSIX and PCRE.
Rik <octave@nomad.inbox5.com>
parents: 11025
diff changeset
1201 ## Test capture replacement
12462
e4dbfe3019b1 Use PCRE regular expressions throughout Octave.
Rik <octave@nomad.inbox5.com>
parents: 12433
diff changeset
1202 %!test
7242
e4398e3903be [project @ 2007-12-03 23:52:07 by dbateman]
dbateman
parents: 7237
diff changeset
1203 %! data = "Bob Smith\nDavid Hollerith\nSam Jenkins";
e4398e3903be [project @ 2007-12-03 23:52:07 by dbateman]
dbateman
parents: 7237
diff changeset
1204 %! result = "Smith, Bob\nHollerith, David\nJenkins, Sam";
e4398e3903be [project @ 2007-12-03 23:52:07 by dbateman]
dbateman
parents: 7237
diff changeset
1205 %! t = regexprep(data,'(?m)^(\w+)\s+(\w+)$','$2, $1');
e4398e3903be [project @ 2007-12-03 23:52:07 by dbateman]
dbateman
parents: 7237
diff changeset
1206 %! assert(t,result)
5785
6b9cec830d72 [project @ 2006-05-03 19:32:46 by dbateman]
dbateman
parents: 5779
diff changeset
1207
11032
c9b0a75b02e8 Make all regexp in Octave compatible with both POSIX and PCRE.
Rik <octave@nomad.inbox5.com>
parents: 11025
diff changeset
1208 ## Return the original if no match
5785
6b9cec830d72 [project @ 2006-05-03 19:32:46 by dbateman]
dbateman
parents: 5779
diff changeset
1209 %!assert(regexprep('hello','world','earth'),'hello')
6b9cec830d72 [project @ 2006-05-03 19:32:46 by dbateman]
dbateman
parents: 5779
diff changeset
1210
6b9cec830d72 [project @ 2006-05-03 19:32:46 by dbateman]
dbateman
parents: 5779
diff changeset
1211 ## Test a general replacement
6b9cec830d72 [project @ 2006-05-03 19:32:46 by dbateman]
dbateman
parents: 5779
diff changeset
1212 %!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
1213
6b9cec830d72 [project @ 2006-05-03 19:32:46 by dbateman]
dbateman
parents: 5779
diff changeset
1214 ## Make sure it works at the beginning and end
6b9cec830d72 [project @ 2006-05-03 19:32:46 by dbateman]
dbateman
parents: 5779
diff changeset
1215 %!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
1216 %!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
1217
6b9cec830d72 [project @ 2006-05-03 19:32:46 by dbateman]
dbateman
parents: 5779
diff changeset
1218 ## Options
6b9cec830d72 [project @ 2006-05-03 19:32:46 by dbateman]
dbateman
parents: 5779
diff changeset
1219 %!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
1220 %!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
1221
6b9cec830d72 [project @ 2006-05-03 19:32:46 by dbateman]
dbateman
parents: 5779
diff changeset
1222 ## Option combinations
6b9cec830d72 [project @ 2006-05-03 19:32:46 by dbateman]
dbateman
parents: 5779
diff changeset
1223 %!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
1224
6b9cec830d72 [project @ 2006-05-03 19:32:46 by dbateman]
dbateman
parents: 5779
diff changeset
1225 ## End conditions on replacement
6b9cec830d72 [project @ 2006-05-03 19:32:46 by dbateman]
dbateman
parents: 5779
diff changeset
1226 %!assert(regexprep("abc","(b)",".$1"),"a.bc");
6b9cec830d72 [project @ 2006-05-03 19:32:46 by dbateman]
dbateman
parents: 5779
diff changeset
1227 %!assert(regexprep("abc","(b)","$1"),"abc");
6b9cec830d72 [project @ 2006-05-03 19:32:46 by dbateman]
dbateman
parents: 5779
diff changeset
1228 %!assert(regexprep("abc","(b)","$1."),"ab.c");
6b9cec830d72 [project @ 2006-05-03 19:32:46 by dbateman]
dbateman
parents: 5779
diff changeset
1229 %!assert(regexprep("abc","(b)","$1.."),"ab..c");
6b9cec830d72 [project @ 2006-05-03 19:32:46 by dbateman]
dbateman
parents: 5779
diff changeset
1230
6361
776e657c9422 [project @ 2007-02-27 09:45:03 by dbateman]
dbateman
parents: 5866
diff changeset
1231 ## 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
1232 %!assert(regexprep("abc",{"b","a"},"?"),"??c")
6361
776e657c9422 [project @ 2007-02-27 09:45:03 by dbateman]
dbateman
parents: 5866
diff changeset
1233 %!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
1234 %!assert(regexprep({"abc","cba"},{"b","a"},{"?","!"}),{"!?c","c?!"})
6361
776e657c9422 [project @ 2007-02-27 09:45:03 by dbateman]
dbateman
parents: 5866
diff changeset
1235
8093
dcc31f473596 Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents: 8021
diff changeset
1236 # Nasty lookbehind expression
12462
e4dbfe3019b1 Use PCRE regular expressions throughout Octave.
Rik <octave@nomad.inbox5.com>
parents: 12433
diff changeset
1237 %!test
11025
df2152514429 Update docstrings for regular expression functions
Rik <octave@nomad.inbox5.com>
parents: 11018
diff changeset
1238 %! 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
1239
5785
6b9cec830d72 [project @ 2006-05-03 19:32:46 by dbateman]
dbateman
parents: 5779
diff changeset
1240 */