GNU Octave  4.2.1
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
lo-regexp.h
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 2012 John W. Eaton
4 Copyright (C) 2005-2016 David Bateman
5 
6 This file is part of Octave.
7 
8 Octave is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by the
10 Free Software Foundation; either version 3 of the License, or (at your
11 option) any later version.
12 
13 Octave is distributed in the hope that it will be useful, but WITHOUT
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
17 
18 You should have received a copy of the GNU General Public License
19 along with Octave; see the file COPYING. If not, see
20 <http://www.gnu.org/licenses/>.
21 
22 */
23 
24 #if ! defined (octave_lo_regexp_h)
25 #define octave_lo_regexp_h 1
26 
27 #include "octave-config.h"
28 
29 #include <list>
30 #include <sstream>
31 #include <string>
32 
33 #include "Array.h"
34 #include "Matrix.h"
35 #include "base-list.h"
36 #include "str-vec.h"
37 
38 namespace octave
39 {
40  class
41  OCTAVE_API
42  regexp
43  {
44  public:
45 
46  class opts;
47  class match_data;
48 
49  regexp (const std::string& pat = "",
50  const regexp::opts& opt = regexp::opts (),
51  const std::string& w = "regexp")
52  : pattern (pat), options (opt), data (0), named_pats (),
53  nnames (0), named_idx (), who (w)
54  {
55  compile_internal ();
56  }
57 
58  regexp (const regexp& rx)
59  : pattern (rx.pattern), data (rx.data), named_pats (rx.named_pats),
60  nnames (rx.nnames), named_idx (rx.named_idx)
61  { }
62 
63  regexp& operator = (const regexp& rx)
64  {
65  if (this != &rx)
66  {
67  pattern = rx.pattern;
68  data = rx.data;
69  named_pats = rx.named_pats;
70  nnames = rx.nnames;
71  named_idx = rx.named_idx;
72  }
73 
74  return *this;
75  }
76 
77  ~regexp (void) { free (); }
78 
79  void compile (const std::string& pat,
80  const regexp::opts& opt = regexp::opts ())
81  {
82  pattern = pat;
83  options = opt;
84  compile_internal ();
85  }
86 
87  match_data match (const std::string& buffer);
88 
89  bool is_match (const std::string& buffer);
90 
91  Array<bool> is_match (const string_vector& buffer);
92 
93  std::string replace (const std::string& buffer,
94  const std::string& replacement);
95 
96  static regexp::match_data
97  match (const std::string& pat, const std::string& buffer,
98  const regexp::opts& opt = regexp::opts (),
99  const std::string& who = "regexp")
100  {
101  regexp rx (pat, opt, who);
102 
103  return rx.match (buffer);
104  }
105 
106  static bool
107  is_match (const std::string& pat, const std::string& buffer,
108  const regexp::opts& opt = regexp::opts (),
109  const std::string& who = "regexp")
110  {
111  regexp rx (pat, opt, who);
112 
113  return rx.is_match (buffer);
114  }
115 
116  static Array<bool>
117  is_match (const std::string& pat, const string_vector& buffer,
118  const regexp::opts& opt = regexp::opts (),
119  const std::string& who = "regexp")
120  {
121  regexp rx (pat, opt, who);
122 
123  return rx.is_match (buffer);
124  }
125 
126  static std::string
127  replace (const std::string& pat, const std::string& buffer,
128  const std::string& replacement,
129  const regexp::opts& opt = regexp::opts (),
130  const std::string& who = "regexp")
131  {
132  regexp rx (pat, opt, who);
133 
134  return rx.replace (buffer, replacement);
135  }
136 
137  class opts
138  {
139  public:
140 
141  opts (void)
142  : x_case_insensitive (false), x_dotexceptnewline (false),
143  x_emptymatch (false), x_freespacing (false), x_lineanchors (false),
144  x_once (false) { }
145 
146  opts (const opts& o)
147  : x_case_insensitive (o.x_case_insensitive),
148  x_dotexceptnewline (o.x_dotexceptnewline),
149  x_emptymatch (o.x_emptymatch),
150  x_freespacing (o.x_freespacing),
151  x_lineanchors (o.x_lineanchors),
152  x_once (o.x_once)
153  { }
154 
155  opts& operator = (const opts& o)
156  {
157  if (this != &o)
158  {
159  x_case_insensitive = o.x_case_insensitive;
160  x_dotexceptnewline = o.x_dotexceptnewline;
161  x_emptymatch = o.x_emptymatch;
162  x_freespacing = o.x_freespacing;
163  x_lineanchors = o.x_lineanchors;
164  x_once = o.x_once;
165  }
166 
167  return *this;
168  }
169 
170  ~opts (void) { }
171 
172  void case_insensitive (bool val) { x_case_insensitive = val; }
173  void dotexceptnewline (bool val) { x_dotexceptnewline = val; }
174  void emptymatch (bool val) { x_emptymatch = val; }
175  void freespacing (bool val) { x_freespacing = val; }
176  void lineanchors (bool val) { x_lineanchors = val; }
177  void once (bool val) { x_once = val; }
178 
179  bool case_insensitive (void) const { return x_case_insensitive; }
180  bool dotexceptnewline (void) const { return x_dotexceptnewline; }
181  bool emptymatch (void) const { return x_emptymatch; }
182  bool freespacing (void) const { return x_freespacing; }
183  bool lineanchors (void) const { return x_lineanchors; }
184  bool once (void) const { return x_once; }
185 
186  private:
187 
193  bool x_once;
194  };
195 
197  {
198  public:
199 
201  const std::string& ms, const Matrix& te,
202  double s, double e)
203  : x_match_string (ms), x_named_tokens (nt), x_tokens (t),
204  x_token_extents (te), x_start (s), x_end (e)
205  { }
206 
208  : x_match_string (a.x_match_string),
209  x_named_tokens (a.x_named_tokens), x_tokens (a.x_tokens),
210  x_token_extents (a.x_token_extents),
211  x_start (a.x_start), x_end (a.x_end)
212  { }
213 
214  std::string match_string (void) const { return x_match_string; }
215  string_vector named_tokens (void) const { return x_named_tokens; }
216  string_vector tokens (void) const { return x_tokens; }
217  Matrix token_extents (void) const { return x_token_extents; }
218  double start (void) const { return x_start; }
219  double end (void) const { return x_end; }
220 
221  private:
222 
227  double x_start;
228  double x_end;
229  };
230 
231  class match_data : public base_list<match_element>
232  {
233  public:
234 
235  match_data (void)
236  : base_list<match_element> (), named_pats ()
237  { }
238 
239  match_data (const std::list<match_element>& l, const string_vector& np)
240  : base_list<match_element> (l), named_pats (np)
241  { }
242 
243  match_data (const match_data& rx_lst)
244  : base_list<match_element> (rx_lst),
245  named_pats (rx_lst.named_pats)
246  { }
247 
248  match_data& operator = (const match_data& rx_lst)
249  {
250  if (this != &rx_lst)
251  {
253  named_pats = rx_lst.named_pats;
254  }
255 
256  return *this;
257  }
258 
259  ~match_data (void) { }
260 
261  string_vector named_patterns (void) { return named_pats; }
262 
263  private:
264 
266  };
267 
268  private:
269 
270  // The pattern we've been asked to match.
272 
274 
275  // Internal data describing the regular expression.
276  void *data;
277 
280  int nnames;
283 
284  void free (void);
285 
286  void compile_internal (void);
287  };
288 }
289 
290 #if defined (OCTAVE_USE_DEPRECATED_FUNCTIONS)
291 
292 OCTAVE_DEPRECATED ("use 'octave::regexp' instead")
293 typedef octave::regexp regexp;
294 
295 OCTAVE_DEPRECATED ("use 'octave::regexp::match' instead")
296 inline regexp::match_data
297 regexp_match (const std::string& pat,
298  const std::string& buffer,
299  const regexp::opts& opt = regexp::opts (),
300  const std::string& who = "regexp")
301 {
302  return octave::regexp::match (pat, buffer, opt, who);
303 }
304 
305 OCTAVE_DEPRECATED ("use 'octave::regexp::is_match' instead")
306 inline bool
307 is_regexp_match (const std::string& pat,
308  const std::string& buffer,
309  const regexp::opts& opt = regexp::opts (),
310  const std::string& who = "regexp")
311 {
312  return octave::regexp::is_match (pat, buffer, opt, who);
313 }
314 
315 OCTAVE_DEPRECATED ("use 'octave::regexp::is_match' instead")
316 inline Array<bool>
317 is_regexp_match (const std::string& pat,
318  const string_vector& buffer,
319  const regexp::opts& opt = regexp::opts (),
320  const std::string& who = "regexp")
321 {
322  return octave::regexp::is_match (pat, buffer, opt, who);
323 }
324 
325 OCTAVE_DEPRECATED ("use 'octave::regexp::replace' instead")
326 inline std::string
327 regexp_replace (const std::string& pat,
328  const std::string& buffer,
329  const std::string& replacement,
330  const regexp::opts& opt = regexp::opts (),
331  const std::string& who = "regexp")
332 {
333  return octave::regexp::replace (pat, buffer, replacement, opt, who);
334 }
335 
336 #endif
337 
338 #endif
bool case_insensitive(void) const
Definition: lo-regexp.h:179
std::string who
Definition: lo-regexp.h:282
match_data(const std::list< match_element > &l, const string_vector &np)
Definition: lo-regexp.h:239
Octave interface to the compression and uncompression libraries.
Definition: aepbalance.cc:47
match_element(const string_vector &nt, const string_vector &t, const std::string &ms, const Matrix &te, double s, double e)
Definition: lo-regexp.h:200
bool emptymatch(void) const
Definition: lo-regexp.h:181
~regexp(void)
Definition: lo-regexp.h:77
double start(void) const
Definition: lo-regexp.h:218
identity matrix If supplied two scalar respectively For allows like xample val
Definition: data.cc:5068
std::string match_string(void) const
Definition: lo-regexp.h:214
static regexp::match_data match(const std::string &pat, const std::string &buffer, const regexp::opts &opt=regexp::opts(), const std::string &who="regexp")
Definition: lo-regexp.h:97
STL namespace.
string_vector named_tokens(void) const
Definition: lo-regexp.h:215
std::string m
Definition: lo-regexp.h:278
OCTAVE_EXPORT octave_value_list return the number of command line arguments passed to Octave If called with the optional argument the function t
Definition: ov-usr-fcn.cc:935
s
Definition: file-io.cc:2682
regexp(const regexp &rx)
Definition: lo-regexp.h:58
i e
Definition: data.cc:2724
calling an anonymous function involves an overhead quite comparable to the overhead of an m file function Passing a handle to a built in function is because the interpreter is not involved in the internal loop For a
Definition: cellfun.cc:398
static Array< bool > is_match(const std::string &pat, const string_vector &buffer, const regexp::opts &opt=regexp::opts(), const std::string &who="regexp")
Definition: lo-regexp.h:117
static std::string replace(const std::string &pat, const std::string &buffer, const std::string &replacement, const regexp::opts &opt=regexp::opts(), const std::string &who="regexp")
Definition: lo-regexp.h:127
void dotexceptnewline(bool val)
Definition: lo-regexp.h:173
static void replace(QString &text, const QRegExp &re, const QString &after)
Definition: parser.cc:561
string_vector x_named_tokens
Definition: lo-regexp.h:224
bool freespacing(void) const
Definition: lo-regexp.h:182
std::complex< double > w(std::complex< double > z, double relerr=0)
bool dotexceptnewline(void) const
Definition: lo-regexp.h:180
opts(const opts &o)
Definition: lo-regexp.h:146
string_vector tokens(void) const
Definition: lo-regexp.h:216
is false
Definition: cellfun.cc:398
std::string replace(const std::string &buffer, const std::string &replacement)
Definition: lo-regexp.cc:448
Definition: dMatrix.h:37
bool is_match(const std::string &buffer)
Definition: lo-regexp.cc:420
Array< int > named_idx
Definition: lo-regexp.h:281
string_vector named_pats
Definition: lo-regexp.h:265
bool lineanchors(void) const
Definition: lo-regexp.h:183
match_data match(const std::string &buffer)
Definition: lo-regexp.cc:241
regexp(const std::string &pat="", const regexp::opts &opt=regexp::opts(), const std::string &w="regexp")
Definition: lo-regexp.h:49
void emptymatch(bool val)
Definition: lo-regexp.h:174
base_list & operator=(const base_list &bl)
Definition: base-list.h:118
match_element(const match_element &a)
Definition: lo-regexp.h:207
void free(void *)
match_data(const match_data &rx_lst)
Definition: lo-regexp.h:243
is longer than or if then or only for unique occurrences of the complete pattern(false).The default is true.If a cell array of strings ar
Definition: strfind.cc:192
string_vector named_patterns(void)
Definition: lo-regexp.h:261
OCTAVE_EXPORT octave_value_list only variables visible in the local scope are displayed The following are valid options
Definition: variables.cc:1859
string_vector named_pats
Definition: lo-regexp.h:279
static bool is_match(const std::string &pat, const std::string &buffer, const regexp::opts &opt=regexp::opts(), const std::string &who="regexp")
Definition: lo-regexp.h:107
bool once(void) const
Definition: lo-regexp.h:184
double end(void) const
Definition: lo-regexp.h:219
Matrix token_extents(void) const
Definition: lo-regexp.h:217
void case_insensitive(bool val)
Definition: lo-regexp.h:172
void lineanchors(bool val)
Definition: lo-regexp.h:176
std::string pattern
Definition: lo-regexp.h:271
If this string is the system will ring the terminal sometimes it is useful to be able to print the original representation of the string
Definition: utils.cc:854
void compile(const std::string &pat, const regexp::opts &opt=regexp::opts())
Definition: lo-regexp.h:79
void once(bool val)
Definition: lo-regexp.h:177
void freespacing(bool val)
Definition: lo-regexp.h:175