other versions
- jessie 3.23.01-1
- stretch 4.04.00-1
- testing 4.08.06-1
- unstable 4.08.06-1
- experimental 4.09.00-1
other sections
FBB::Pattern(3bobcat) | Pattern matcher | FBB::Pattern(3bobcat) |
NAME¶
FBB::Pattern - Performs RE pattern matchingSYNOPSIS¶
#include <bobcat/pattern>DESCRIPTION¶
Pattern objects may be used for Regular Expression (RE) pattern matching. The class is a wrapper around the 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., *, +, ?, ^, $, |, (, ), [, ], {, } should be escaped when used as literal characters). The Pattern class supports the use of the following (Perl-like) special escape sequences:- o
- Special escape sequences (like \d) are converted to character
classes. E.g.,
--------------------------------------------------------- Specify: Converts to: regcomp uses: Matches: --------------------------------------------------------- \d [[:digit:]] [[:digit:]] 3 ---------------------------------------------------------
- o
- Ordinary escape sequences (like \x) are kept as-is. E.g.,
--------------------------------------------------------- Specify: Converts to: regcomp uses: Matches: --------------------------------------------------------- \x \x x x ---------------------------------------------------------
- o
- To specify a literal escape sequence, it must be written twice. E.g.,
--------------------------------------------------------- Specify: Converts to: regcomp uses: Matches: --------------------------------------------------------- \\x \\x \x \x ---------------------------------------------------------
NAMESPACE¶
FBBINHERITS FROM¶
-TYPEDEF¶
- o
- Pattern::Position:
CONSTRUCTORS¶
- o
- Pattern():
- o
- Pattern(std::string const &pattern, bool caseSensitive =
true, size_t nSub = 10, int options = REG_EXTENDED |
REG_NEWLINE):
- REG_EXTENDED:
- REG_NOSUB:
- REG_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.
MEMBER FUNCTIONS¶
All members of std::ostringstream and std::exception are available, as Pattern inherits from these classes.- o
- std::string before() const:
- o
- std::string beyond() const:
- o
- size_t end() const:
- o
- void match(std::string const &text, int options = 0):
- Options may be:
- REG_NOTBOL:
- REG_NOTEOL:
- o
- std::string matched() const:
- o
- std::string const &pattern() const:
- o
- Pattern::Position position(size_t index) const:
- o
- void setPattern(std::string const &pattern, bool
caseSensitive = true, size_t nSub = 10, int options =
REG_EXTENDED | REG_NEWLINE):
- o
- void swap(Pattern &other):
OVERLOADED OPERATORS¶
- o
- Pattern &operator=(Pattern &other):
- o
- std::string operator[](size_t index) const:
- o
- Pattern &operator<<(int matchOptions):
- o
- bool operator<<(std::string const &text):
EXAMPLE¶
/* driver.cc */ #include "driver.h" #include <bobcat/pattern> using namespace std; using namespace FBB; void showSubstr(string const &str) { static int count = 1; cout << "String " << count++ << " is ’" << str << "’\n"; } int main(int argc, char **argv) { { Pattern one("one"); Pattern two(one); Pattern three("a"); Pattern four; three = two; } try { Pattern pattern("aap|noot|mies"); { Pattern extra(Pattern(pattern)); } if (pattern << "noot") cout << "noot matches\n"; else cout << ": noot doesn’t match\n"; } catch (exception const &e) { cout << e.what() << ": compilation failed" << endl; } string pat = "\\d+"; while (true) { cout << "Pattern: ’" << pat << "’\n"; try { Pattern patt(pat, argc == 1); // case sensitive by default, // any arg for case insensitive cout << "Compiled pattern: " << patt.pattern() << endl; Pattern pattern; pattern = patt; // assignment operator while (true) { cout << "string to match : "; string st; getline(cin, st); if (st == "") break; cout << "String: ’" << st << "’\n"; try { pattern.match(st); Pattern p3(pattern); cout << "before: " << p3.before() << "\n" "matched: " << p3.matched() << "\n" "beyond: " << pattern.beyond() << "\n" "end() = " << pattern.end() << endl; for (size_t idx = 0; idx < pattern.end(); ++idx) { string str = pattern[idx]; if (str == "") 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 &e) { cout << e.what() << ": " << st << " doesn’t match" << endl; continue; } } } catch (exception const &e) { cout << e.what() << ": compilation failed" << endl; } cout << "New pattern: "; if (!getline(cin, pat) || !pat.length()) return 0; } }
FILES¶
bobcat/pattern - defines the class interfaceSEE ALSO¶
bobcat(7), regcomp(3), regex(3), regex(7)BUGS¶
Using 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., 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 Pattern, initialzing the pointer when, e.g., first used.DISTRIBUTION FILES¶
- o
- bobcat_3.23.01-x.dsc: detached signature;
- o
- bobcat_3.23.01-x.tar.gz: source archive;
- o
- bobcat_3.23.01-x_i386.changes: change log;
- o
- libbobcat1_3.23.01-x_*.deb: debian package holding the libraries;
- o
- libbobcat1-dev_3.23.01-x_*.deb: debian package holding the libraries, headers and manual pages;
- o
- http://sourceforge.net/projects/bobcat: public archive location;
BOBCAT¶
Bobcat is an acronym of `Brokken’s Own Base Classes And Templates’.COPYRIGHT¶
This is free software, distributed under the terms of the GNU General Public License (GPL).AUTHOR¶
Frank B. Brokken ( f.b.brokken@rug.nl).2005-2014 | libbobcat-dev_3.23.01-x.tar.gz |