1
/*=========================================================================
3
Program: Insight Segmentation & Registration Toolkit
4
Module: $RCSfile: cmRegularExpression.h,v $
6
Date: $Date: 2001/04/27 12:01:17 $
7
Version: $Revision: 1.5 $
9
Copyright (c) 2001 Insight Consortium
12
Redistribution and use in source and binary forms, with or without
13
modification, are permitted provided that the following conditions are met:
15
* Redistributions of source code must retain the above copyright notice,
16
this list of conditions and the following disclaimer.
18
* Redistributions in binary form must reproduce the above copyright notice,
19
this list of conditions and the following disclaimer in the documentation
20
and/or other materials provided with the distribution.
22
* The name of the Insight Consortium, nor the names of any consortium members,
23
nor of any contributors, may be used to endorse or promote products derived
24
from this software without specific prior written permission.
26
* Modified source versions must be plainly marked as such, and must not be
27
misrepresented as being the original software.
29
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS ``AS IS''
30
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32
ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE FOR
33
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
35
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
36
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
37
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
38
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40
=========================================================================*/
41
// Original Copyright notice:
42
// Copyright (C) 1991 Texas Instruments Incorporated.
44
// Permission is granted to any individual or institution to use, copy, modify,
45
// and distribute this software, provided that this complete copyright and
46
// permission notice is maintained, intact, in all copies and supporting
49
// Texas Instruments Incorporated provides this software "as is" without
50
// express or implied warranty.
52
// Created: MNF 06/13/89 Initial Design and Implementation
53
// Updated: LGO 08/09/89 Inherit from Generic
54
// Updated: MBN 09/07/89 Added conditional exception handling
55
// Updated: MBN 12/15/89 Sprinkled "const" qualifiers all over the place!
56
// Updated: DLS 03/22/91 New lite version
59
#ifndef cmRegularExpression_h
60
#define cmRegularExpression_h
62
#include "cmStandardIncludes.h"
64
const int NSUBEXP = 10;
66
/** \class cmRegularExpression
67
* \brief Implements pattern matching with regular expressions.
69
* This is the header file for the regular expression class. An object of
70
* this class contains a regular expression, in a special "compiled" format.
71
* This compiled format consists of several slots all kept as the objects
72
* private data. The cmRegularExpression class provides a convenient way to
73
* represent regular expressions. It makes it easy to search for the same
74
* regular expression in many different strings without having to compile a
75
* string to regular expression format more than necessary.
77
* This class implements pattern matching via regular expressions.
78
* A regular expression allows a programmer to specify complex
79
* patterns that can be searched for and matched against the
80
* character string of a string object. In its simplest form, a
81
* regular expression is a sequence of characters used to
82
* search for exact character matches. However, many times the
83
* exact sequence to be found is not known, or only a match at
84
* the beginning or end of a string is desired. The cmRegularExpression regu-
85
* lar expression class implements regular expression pattern
86
* matching as is found and implemented in many UNIX commands
89
* Example: The perl code
91
* $filename =~ m"([a-z]+)\.cc";
94
* Is written as follows in C++
96
* cmRegularExpression re("([a-z]+)\\.cc");
98
* cerr << re.match(1);
101
* The regular expression class provides a convenient mechanism
102
* for specifying and manipulating regular expressions. The
103
* regular expression object allows specification of such pat-
104
* terns by using the following regular expression metacharac-
107
* ^ Matches at beginning of a line
109
* $ Matches at end of a line
111
* . Matches any single character
113
* [ ] Matches any character(s) inside the brackets
115
* [^ ] Matches any character(s) not inside the brackets
117
* - Matches any character in range on either side of a dash
119
* * Matches preceding pattern zero or more times
121
* + Matches preceding pattern one or more times
123
* ? Matches preceding pattern zero or once only
125
* () Saves a matched expression and uses it in a later match
127
* Note that more than one of these metacharacters can be used
128
* in a single regular expression in order to create complex
129
* search patterns. For example, the pattern [^ab1-9] says to
130
* match any character sequence that does not begin with the
131
* characters "ab" followed by numbers in the series one
134
* There are three constructors for cmRegularExpression. One just creates an
135
* empty cmRegularExpression object. Another creates a cmRegularExpression
136
* object and initializes it with a regular expression that is given in the
137
* form of a char*. The third takes a reference to a cmRegularExpression
138
* object as an argument and creates an object initialized with the
139
* information from the given cmRegularExpression object.
141
* The find member function finds the first occurence of the regualr
142
* expression of that object in the string given to find as an argument. Find
143
* returns a boolean, and if true, mutates the private data appropriately.
144
* Find sets pointers to the beginning and end of the thing last found, they
145
* are pointers into the actual string that was searched. The start and end
146
* member functions return indicies into the searched string that correspond
147
* to the beginning and end pointers respectively. The compile member
148
* function takes a char* and puts the compiled version of the char* argument
149
* into the object's private data fields. The == and != operators only check
150
* the to see if the compiled regular expression is the same, and the
151
* deep_equal functions also checks to see if the start and end pointers are
152
* the same. The is_valid function returns false if program is set to NULL,
153
* (i.e. there is no valid compiled exression). The set_invalid function sets
154
* the program to NULL (Warning: this deletes the compiled expression). The
155
* following examples may help clarify regular expression usage:
157
* * The regular expression "^hello" matches a "hello" only at the
158
* beginning of a line. It would match "hello there" but not "hi,
161
* * The regular expression "long$" matches a "long" only at the end
162
* of a line. It would match "so long\0", but not "long ago".
164
* * The regular expression "t..t..g" will match anything that has a
165
* "t" then any two characters, another "t", any two characters and
166
* then a "g". It will match "testing", or "test again" but would
167
* not match "toasting"
169
* * The regular expression "[1-9ab]" matches any number one through
170
* nine, and the characters "a" and "b". It would match "hello 1"
171
* or "begin", but would not match "no-match".
173
* * The regular expression "[^1-9ab]" matches any character that is
174
* not a number one through nine, or an "a" or "b". It would NOT
175
* match "hello 1" or "begin", but would match "no-match".
177
* * The regular expression "br* " matches something that begins with
178
* a "b", is followed by zero or more "r"s, and ends in a space. It
179
* would match "brrrrr ", and "b ", but would not match "brrh ".
181
* * The regular expression "br+ " matches something that begins with
182
* a "b", is followed by one or more "r"s, and ends in a space. It
183
* would match "brrrrr ", and "br ", but would not match "b " or
186
* * The regular expression "br? " matches something that begins with
187
* a "b", is followed by zero or one "r"s, and ends in a space. It
188
* would match "br ", and "b ", but would not match "brrrr " or
191
* * The regular expression "(..p)b" matches something ending with pb
192
* and beginning with whatever the two characters before the first p
193
* encounterd in the line were. It would find "repb" in "rep drepa
194
* qrepb". The regular expression "(..p)a" would find "repa qrepb"
195
* in "rep drepa qrepb"
197
* * The regular expression "d(..p)" matches something ending with p,
198
* beginning with d, and having two characters in between that are
199
* the same as the two characters before the first p encounterd in
200
* the line. It would match "drepa qrepb" in "rep drepa qrepb".
203
class cmRegularExpression
207
* Instantiate cmRegularExpression with program=NULL.
209
inline cmRegularExpression ();
212
* Instantiate cmRegularExpression with compiled char*.
214
inline cmRegularExpression (char const*);
217
* Instantiate cmRegularExpression as a copy of another regular expression.
219
cmRegularExpression (cmRegularExpression const&);
224
inline ~cmRegularExpression();
227
* Compile a regular expression into internal code
228
* for later pattern matching.
230
void compile (char const*);
233
* Matches the regular expression to the given string.
234
* Returns true if found, and sets start and end indexes accordingly.
236
bool find (char const*);
239
* Matches the regular expression to the given std string.
240
* Returns true if found, and sets start and end indexes accordingly.
242
bool find (std::string const&);
245
* Index to start of first find.
247
inline long start() const;
250
* Index to end of first find.
252
inline long end() const;
255
* Returns true if two regular expressions have the same
256
* compiled program for pattern matching.
258
bool operator== (cmRegularExpression const&) const;
261
* Returns true if two regular expressions have different
262
* compiled program for pattern matching.
264
inline bool operator!= (cmRegularExpression const&) const;
267
* Returns true if have the same compiled regular expressions
268
* and the same start and end pointers.
270
bool deep_equal (cmRegularExpression const&) const;
273
* True if the compiled regexp is valid.
275
inline bool is_valid() const;
278
* Marks the regular expression as invalid.
280
inline void set_invalid();
286
int start(int n) const;
287
int end(int n) const;
288
std::string match(int n) const;
291
const char* startp[NSUBEXP];
292
const char* endp[NSUBEXP];
293
char regstart; // Internal use only
294
char reganch; // Internal use only
295
const char* regmust; // Internal use only
296
int regmlen; // Internal use only
299
const char* searchstring;
303
* Create an empty regular expression.
305
inline cmRegularExpression::cmRegularExpression ()
307
this->program = NULL;
311
* Creates a regular expression from string s, and
314
inline cmRegularExpression::cmRegularExpression (const char* s)
316
this->program = NULL;
321
* Destroys and frees space allocated for the regular expression.
323
inline cmRegularExpression::~cmRegularExpression ()
326
delete [] this->program;
331
* Set the start position for the regular expression.
333
inline long cmRegularExpression::start () const
335
return(this->startp[0] - searchstring);
340
* Returns the start/end index of the last item found.
342
inline long cmRegularExpression::end () const
344
return(this->endp[0] - searchstring);
348
* Returns true if two regular expressions have different
349
* compiled program for pattern matching.
351
inline bool cmRegularExpression::operator!= (const cmRegularExpression& r) const
353
return(!(*this == r));
357
* Returns true if a valid regular expression is compiled
358
* and ready for pattern matching.
360
inline bool cmRegularExpression::is_valid () const
362
return (this->program != NULL);
366
inline void cmRegularExpression::set_invalid ()
369
delete [] this->program;
371
this->program = NULL;
375
* Return start index of nth submatch. start(0) is the start of the full match.
377
inline int cmRegularExpression::start(int n) const
379
return this->startp[n] - searchstring;
384
* Return end index of nth submatch. end(0) is the end of the full match.
386
inline int cmRegularExpression::end(int n) const
388
return this->endp[n] - searchstring;
392
* Return nth submatch as a string.
394
inline std::string cmRegularExpression::match(int n) const
396
return std::string(this->startp[n], this->endp[n] - this->startp[n]);
399
#endif // cmRegularExpressionh