1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
|
includefile(include/header)
COMMENT(manpage, section, releasedate, archive, short name)
manpage(FBB::Pattern)(3bobcat)(_CurYrs_)(libbobcat-dev__CurVers_)
(Pattern matcher)
manpagename(FBB::Pattern)(Performs RE pattern matching)
manpagesynopsis()
bf(#include <bobcat/pattern>)nl()
Linking option: tt(-lbobcat)
manpagedescription()
bf(Pattern) objects may be used for Regular Expression (RE) pattern
matching. The class is a wrapper around the bf(regcomp)(3) family of
functions. By default it uses `extended regular expressions', requiring you to
escape multipliers and bounding-characters when they should be interpreted as
ordinary characters (i.e., tt(*, +, ?, ^, $, |, (, ), [, ], {, }) should be
escaped when used as literal characters).
The bf(Pattern) class supports the use of the following (Perl-like)
special escape sequences: nl()
\b - indicating a word-boundary nl()
\d - indicating a digit (tt([[:digit:]])) character nl()
\s - indicating a white-space (tt([:space:])) character nl()
\w - indicating a word (tt([:alnum:])) character
The corresponding capitals (e.g., bf(\W)) define the complementary
character sets. The capitalized character set shorthands are not expanded
inside explicit character-classes (i.e., tt([ ... ]) constructions). So
tt([\W]) represents a set of two characters: tt(\) and tt(W).
As the backslash (tt(\)) is treated as a special character it should be
handled carefully. bf(Pattern) converts the escape sequences tt(\d \s \w) (and
outside of explicit character classes the sequences tt(\D \S \W)) to their
respective character classes. All other escape sequences are kept as-is, and
the resulting regular expression is offered to the pattern matching
compilation function bf(regcomp)(3). This function interprets
escape sequences. Consequently some care should be exercised when defining
patterns containing escape sequences. Here are the rules:
itemization(
it() Special escape sequences (like tt(\d)) are converted to character
classes. E.g.,
verb(
---------------------------------------------------------
Specify: Converts to: regcomp uses: Matches:
---------------------------------------------------------
\d [[:digit:]] [[:digit:]] 3
---------------------------------------------------------
)
it() Ordinary escape sequences (like tt(\x)) are kept as-is. E.g.,
verb(
---------------------------------------------------------
Specify: Converts to: regcomp uses: Matches:
---------------------------------------------------------
\x \x x x
---------------------------------------------------------
)
it() To specify literal escape sequences, Raw String Literals are advised,
as they don't require doubling escape sequences. E.g., the following
regular expression matches an (alpha-numeric) word, followed by optional
blanks, a colon, more optional blanks and a (decimal) number:
verb(
R"((\w+)\s*:\s*\d+)"
)
)
includefile(include/namespace)
manpagesection(INHERITS FROM)
-
manpagesection(TYPEDEF)
itemization(
itb(Pattern::Position)
A nested type representing the offsets of the first character and
the offset beyond the last character of the matched text or indexed
subexpression, defined as tt(std::pair<std::string::size_type,
std::string::size_type>).
)
manpagesection(CONSTRUCTORS)
itemization(
itb(Pattern())
The default constructor defines no pattern, but is available as a
placeholder for, e.g., containers requiring default constructors. A
bf(Pattern) object thus constructed cannot be used to match patterns, but can
be the em(lvalue) in assignments where another bf(Pattern) object is the
em(rvalue). However, it can receive a pattern using the member
bf(setPattern()) (see below). An bf(FBB::Exception) object is thrown if the
object could not be constructed.
itb(Pattern(std::string const &pattern,
bool caseSensitive = true,
size_t nSub = 10,
int options = REG_EXTENDED | REG_NEWLINE))
This constructor compiles tt(pattern), preparing the bf(Pattern)
object for pattern matches. The second parameter determines whether case
sensitive matching will be used (the default) or not. Subexpressions are
defined by parentheses pairs. Each matching pair defines a subexpression,
where the order-number of their opening parentheses determines the
subexpression's index. By default at most 10 subexpressions are recognized.
The em(options) flags may be:
REG_EXTENDED: nl()
Use POSIX Extended Regular Expression syntax when
interpreting regex. If not set, POSIX Basic Regular
Expression syntax is used.
REG_NOSUB: nl()
Support for substring addressing of matches is not
required. The nmatch and pmatch parameters to
regexec are ignored if the pattern buffer supplied
was compiled with this flag set.
REG_NEWLINE: nl()
Match-any-character operators don't match a newline.
A non-matching list ([^...]) not containing a newline
does not match a newline.
Match-beginning-of-line operator (^) matches the empty
string immediately after a newline, regardless of whether
eflags, the execution flags of regexec, contains
REG_NOTBOL.
Match-end-of-line operator ($) matches the empty
string immediately before a newline, regardless of
whether eflags contains REG_NOTEOL.
)
Copy and move constructors (and assignment operators) are available.
manpagesection(MEMBER FUNCTIONS)
All members of bf(std::ostringstream) and bf( std::exception) are
available, as bf(Pattern) inherits from these classes.
itemization(
itb(std::string before() const)
Following a successful match, bf(before()) returns the text before the
matched text.
itb(std::string beyond() const)
Following a successful match, bf(beyond()) returns the text beyond the
matched text.
itb(size_t end() const)
Returns the number of matched elements (text and
subexpressions). bf(end()) is the lowest index value for which bf(position())
returns two tt(std::string::npos) values (see the bf(position()) member
function, below).
itb(void match(std::string const &text, int options = 0))
Match a string with a pattern. If the text could not be matched, an
bf(Exception) exception is thrown , using bf(Pattern::match()) as its
prefix-text.
Options may be:
REG_NOTBOL: nl()
The match-beginning-of-line operator always fails to match
(but see the compilation flag REG_NEWLINE above) This flag
may be used when different portions of a string are passed
to regexec and the beginning of the string should not be
interpreted as the beginning of the line.
REG_NOTEOL: nl()
The match-end-of-line operator always fails to
match (but see the compilation flag REG_NEWLINE)
itb(std::string matched() const)
Following a successful match, this function returns the matched text.
itb(std::string const &pattern() const)
This member function returns the pattern that is offered to
bf(regcomp)(3). It returns the content of a tt(static) string that is
overwritten at each construction of a bf(Pattern) object and at each call of
the tt(setPattern()) member function.
itb(Pattern::Position position(size_t index) const)
With em(index == 0) the fully matched text is returned (identical to
tt(matched())). Other index values return the corresponding
subexpressions. bf(std::string::npos, std::string::npos) is returned if index
is at least bf(end()) (which may happen at index value 0).
itb(void setPattern(std::string const &pattern,
bool caseSensitive = true,
size_t nSub = 10,
int options = REG_EXTENDED | REG_NEWLINE))
This member function installs a new compiled tt(pattern) in its
bf(Pattern) object. This member's parameters are identical to the second
constructor's parameters. Refer to that constructor for details about the
parameters. Like the constructor, an bf(FBB::Exception) exception is thrown if
the new pattern could not be compiled.
itb(void swap(Pattern &other))
The content of the current object and the tt(other) object are
swapped.
)
manpagesection(OVERLOADED OPERATORS)
itemization(
itb(std::string operator[](size_t index) const)
Returns the matched text (for index 0) or the text of a
subexpression. An empty string is returned for index values which are at least
bf(end()).
itb(Pattern &operator<<(int matchOptions))
Defines match-options to be used with the following overloaded
operator.
itb(bool operator<<(std::string const &text))
Performs a bf(match(text, matchOptions)) call, catching any exception
that might be thrown. If no em(matchOptions) were set using the above
overloaded operator, none are used. The options set this way are not `sticky':
when necessary, they have to be re-inserted before each new pattern
matching. The function returns bf(true) if the matching was successful,
bf(false) otherwise.
)
manpagesection(EXAMPLE)
verbinclude(../../pattern/driver/driver.cc)
manpagefiles()
em(bobcat/pattern) - defines the class interface
manpageseealso()
bf(bobcat)(7), bf(regcomp)(3), bf(regex)(3), bf(regex)(7)
manpagebugs()
Using tt(Pattern) objects as static data members of classes (or as global
objects) is potentially dangerous. If the object files defining these static
data members are stored in a dynamic library they may not be initialized
properly or timely, and their eventual destruction may result in a
segmentation fault. This is a well-known problem with static data, see, e.g.,
tt(http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.15). In situations
like this prefer the use of a (shared, unique) pointer to a tt(Pattern),
initializing the pointer when, e.g., first used.
includefile(include/trailer)
|