FBB::Pattern(3bobcat)
β¨ FBB::Pattern β¨ β like, it's the FBB::Pattern(3bobcat) page bestie π
okay so picture this: it's FBB::Pattern(3bobcat), the "Pattern matcher" page from the bobcat fam, and it's literally the whole personality of doing RE pattern matching in C++. buckle up π
πΈ NAME πΈ
FBB::Pattern β Performs RE pattern matching π
π¦ SYNOPSIS π¦
#include <bobcat/pattern>
Linking option: -lbobcat
β¨ DESCRIPTION β¨
okay so Pattern objects?? they're for Regular Expression (RE) pattern matching, no cap. And here's the tea: the class is a wrapper around the regcomp(3) family of functions. π«Ά
By default it uses extended regular expressions, which means you have to escape multipliers and bounding-characters when they're supposed to be interpreted as ordinary characters. Translation: *, +, ?, ^, $, |, (, ), [, ], {, } all need escaping when they're just vibing as literal characters. π
The Pattern class supports these (Perl-like) special escape sequences, and honestly? iconic:
\bβ indicating a word-boundary\dβ indicating a digit ([[:digit:]]) character\sβ indicating a white-space ([:space:]) character\wβ indicating a word ([[:alnum:]]) character
The corresponding capitals (e.g., \W) define the complementary character sets. β¨ But heads up bestie: the capitalized character set shorthands are not expanded inside explicit character-classes (i.e., the [ ... ] constructions). E.g., [\W] represents a set of two characters: \ and W. the way that trips everyone up fr fr π
As the backslash (\) is treated as a special character it should be handled carefully. Pattern converts the escape sequences \d \s \w (and outside of explicit character classes the sequences \D \S \W) to their respective character classes. All other escape sequences are kept as-is, and the resulting regular expression is passed to the pattern matching compilation function regcomp(3). The regcomp function interprets escape sequences. Consequently some care should be exercised when defining patterns containing escape sequences. here are the rules, queen π
- Special escape sequences (like
\d) are converted to character classes. E.g.,
---------------------------------------------------------
Specify: Converts to: regcomp uses: Matches:
---------------------------------------------------------
\d [[:digit:]] [[:digit:]] 3
---------------------------------------------------------
- Ordinary escape sequences (like
\x) are kept as-is. E.g.,
---------------------------------------------------------
Specify: Converts to: regcomp uses: Matches:
---------------------------------------------------------
\x \x x x
---------------------------------------------------------
- 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:
R"((\w+)\s*:\s*\d+)"
Furthermore, by using (raw) string concatenation in source file the legibility of complex regular expressions is greatly improved. The above regular expression could also be specified as π
R"((\w+))" // a word
R"(\s*:)" // then β:β, maybe first some space chars
R"(\s*\d+)" // then digits, maybe first some space chars
π NAMESPACE π
FBB
All constructors, members, operators and manipulators, mentioned in this man-page, are defined in the namespace FBB. periodt π
π INHERITS FROM π
-
(yeah, nothing, it's standing on its own two feet bestie π«Ά)
π§ TYPEDEF π§
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 asstd::pair<size_t, size_t>. cute lil pair energy π
π OPTIONS π
The following options can be specified with the non-default constructor and the match member (see below):
REG_EXTENDEDβ Used by default: POSIX Extended Regular Expression syntax when interpreting regex. If not set, POSIX Basic Regular Expression syntax is used;REG_NOSUBβ Support for substring addressing of matches is not required. Thenmatchandpmatchparameters toregexecare ignored if the pattern buffer supplied was compiled with this flag set;REG_NEWLINEβ Used by default: match-any-character operators don't match newlines.- 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 whethereflags, the execution flags ofregexec, containsREG_NOTBOL. - Match-end-of-line operator (
$) matches the empty string immediately before a newline, regardless of whethereflagscontainsREG_NOTEOL.
- A non-matching list (
π CONSTRUCTORS π
Pattern()β The default constructor does not define a pattern, but can be used in e.g., containers requiring default constructors;Pattern(std::string const &pattern, bool caseSensitive = true, size_t nSub = 10, int options = REG_EXTENDED | REG_NEWLINE)β This constructor compilespattern, preparing thePatternobject for pattern matches. The second parameter determines whether case sensitive matching will be used (the default) or not. Subexpressions are defined by pairs of parentheses. 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. π«Ά
Copy and move constructors (and assignment operators) are available. slay β¨
π MEMBER FUNCTIONS π
std::string before() constβ Following a successful match,before()returns the text before the matched text;std::string beyond() constβ Following a successful match,beyond()returns the text beyond the matched text;size_t end() constβ Returns the number of matched elements (the full pattern and subexpressions). When no match was obtainedstring::nposis returned. When specifying index valuesend()or beyond thenposition(idx)returns twostd::string::npos;operator[](idx)returns an empty string;void match(std::string const &text, int options = 0)β Match a string with a pattern. If the text could not be matched, anException(3bobcat)exception is thrown, usingPattern::match()as its prefix-text;std::string matched() constβ Following a successful match, this function returns the (completely) matched text;std::string const &pattern() constβ This member function returns the pattern that was specified by the constructor and by thesetPatternmember;Pattern::Position position(size_t index) constβ When specifying index 0 the begin and end index values of the fully matched text is returned. Other index values return the begin and end index values of the specified (parenthesized) sub-expression. Whenindexis at leastend()aPosition{ string::npos, string::npos }value is returned;void setPattern(std::string const &pattern, bool caseSensitive = true, size_t nSub = 10, int options = REG_EXTENDED | REG_NEWLINE)β This member function redefinespatternas the regular expression used my subsequent match-member calls. AnFBB::Exceptionexception is thrown if the new pattern could not be compiled;void swap(Pattern &other)β The content of the current object and theotherobject are swapped.
π¦ OVERLOADED OPERATORS π¦
std::string operator[](size_t index) constβ Returns (for index value 0) the fully matched text or (for larger index values up to the value returned byend()) the text of sub-expressionindex. An empty string is returned for index values at least equal toend();Pattern &operator<<(int matchOptions)β Sets match-options to be used (once) with the following overloaded operator;bool operator<<(std::string const &text)β Performs amatch(text, matchOptions)call, catching any exception that might be thrown. If nomatchOptionswere set usingoperator<<(int matchOptions)no match-options are used. The options set this way are notsticky: when necessary, they have to be re-inserted before each new pattern matching. The function returnstrueif the matching was successful,falseotherwise. π₯
okay that was a LOT and we're literally only halfway, part 2 is still loading bestie ππ
β¨ EXAMPLE β¨
okay bestie, this is the whole little demo program, and honestly it's giving "learn by vibes" π here's the entire thing, verbatim, no edits, no cap:
#include "driver.h"
#include <bobcat/pattern>
using namespace std;
using namespace FBB;
#include <algorithm>
#include <cstring>
void showSubstr(string const &str)
{
static int count = 0;
cout << "String " << ++count << " is β" << str << "β\n";
}
void match(Pattern const &patt, string const &text)
try
{
Pattern pattern{ patt };
pattern.match(text);
Pattern p3(pattern);
cout << "before: " << p3.before() << "\n"
"matched: " << p3.matched() << "\n"
"beyond: " << pattern.beyond() << "\n"
"end() = " << pattern.end() << β\nβ;
for (size_t idx = 0; idx != pattern.end(); ++idx)
{
string str = pattern[idx];
if (str.empty())
cout << "part " << idx << " not present\n";
else
{
Pattern::Position pos = pattern.position(idx);
cout << "part " << idx << ": β" << str << "β (" <<
pos.first << "-" << pos.second << ")\n";
}
}
}
catch (exception const &exc)
{
cout << exc.what() << β\nβ;
}
int main(int argc, char **argv)
{
string patStr = R"(\d+)";
do
{
cout << "Pattern: β" << patStr << "β\n";
try
{
// by default: case sensitive
// use any args. for case insensitive
Pattern patt(patStr, argc == 1);
cout << "Compiled pattern: " << patt.pattern() << β\nβ;
while (true)
{
cout << "string to match : ";
string text;
getline(cin, text);
if (text.empty())
break;
cout << "String: β" << text << "β\n";
match(patt, text);
}
}
catch (exception const &exc)
{
cout << exc.what() << ": compilation failed\n";
}
cout << "New pattern: ";
}
while (getline(cin, patStr) and not patStr.empty());
}
quick tour of the chaos π
match() takes a pattern and a text, copies the pattern (iconic, non-destructive behaviour), calls match(), then flexes before(), matched() and beyond() on it, plus end() to know how many parts there are. Then it loops from idx = 0 up to pattern.end(), grabs pattern[idx], and if that string is empty it announces "not present" (π rude but fair), otherwise it pulls a Pattern::Position out of pattern.position(idx) and prints the part, the text, and the first-second pair. The whole try/catch is right there on the function's shoulders as a function-try-block, and anything that goes wrong just gets exc.what()'d straight to cout. no crashing, only reporting β¨
in main(): patStr starts life as the raw string R"(\d+)", and the comment is doing the heavy lifting here β by default it's case sensitive, and you pass any args to get case insensitive. hence Pattern patt(patStr, argc == 1). cute. Then it prints the compiled pattern, and loops forever reading lines with getline(cin, text) until you feed it an empty line. Empty line ends the inner loop; then it asks for a "New pattern" and keeps going while getline(cin, patStr) and not patStr.empty() holds. Fancy π
ngl thePattern::Position posline is the moment β that's thefirst/secondpair of indices for that part. screenshot it, bestie πΈ
π FILES π
bobcat/patternβ this is where the class interface lives, that's the header, periodt.
π SEE ALSO π
go read those too, iykyk π¦
π BUGS π
- None reported.
not me being shocked β genuinely, no bugs on the record. slay π
π BOBCAT PROJECT FILES π
https://fbb-git.gitlab.io/bobcat/: the gitlab project page, your one stop shop;
Debian Bobcat project files:
libbobcat6: debian package containing the shared library, changelog and copyright note;libbobcat-dev: debian package containing the static library, headers, manual pages, and developer info;
π BOBCAT π
Bobcat is an acronym of Brokkenβs Own Base Classes And Templates.
it's giving recursive naming and we love that for it π
β COPYRIGHT β
This is free software, distributed under the terms of the GNU General Public License (GPL).
free software, bestie. freedom, GPL, the whole vibe π
π AUTHOR π
Frank B. Brokken (f.b.brokken@rug.nl).
and that's a wrap on this one, bestie!! this whole thing shipped in libbobcat-dev_6.15.01, dated 2005-2026, and it's the FBB::Pattern(3bobcat) page β¨ go forth and match responsibly, and remember: gaps are "", end() counts them, and you WILL be checking .empty() first πΈπ
π«Ά
π¦ where this page comes from
- ubuntu stonking: libbobcat-dev 6.15.01-1 Β· GPL-3.0-or-later WITH OpenSSL exception
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms