class Matcher |
Mutable object used on instances of a Pattern class
![]() | MATCH_ENTIRE_STRING Used internally by match to signify we want the entire string matched |
![]() | findAll () Returns a vector of every substring in order which matches the given pattern. |
![]() | findFirstMatch () Scans the string for the first substring matching the pattern. |
![]() | findNextMatch () Scans the string for the next substring matching the pattern. |
![]() | getEndingIndex (const int groupNum = 0) const Returns the ending index of the specified group. |
![]() | getFlags () const The flags currently being used by the matcher. |
![]() | getGroup (const int groupNum = 0) const Returns the specified group. |
![]() | getGroups (const bool includeGroupZero = 0) const Returns every capture group in a vector |
![]() | getStartingIndex (const int groupNum = 0) const Returns the starting index of the specified group. |
![]() | getString () const Same as getText. |
![]() | getText () const The text being searched by the matcher. |
![]() | matches () Scans the string from start to finish for a match. |
![]() | replaceWithGroups (const std::string & str) Replaces the contents of str with the appropriate captured text.
|
![]() | reset () Resets the internal state of the matcher |
![]() | setString (const std::string & newStr) Sets the string to scan |
![]() | ~Matcher () Cleans up the dynamic memory used by this matcher |
![]() | ends An array of the ending positions for each group |
![]() | flags The flags with which we were made |
![]() | gc The number of capturing groups we have |
![]() | groupIndeces An array of private data used by NFANodes during matching |
![]() | groupPos An array of private data used by NFANodes during matching |
![]() | groups An array of private data used by NFANodes during matching |
![]() | lm The ending index of the last match |
![]() | matchedSomething Whether or not we have matched something (used only by findFirstMatch and findNextMatch) |
![]() | ncgc The number of non-capturing groups we havew |
![]() | pat The pattern we use to match |
![]() | start The starting point of our match |
![]() | starts An array of the starting positions for each group |
![]() | str The string in which we are matching |
![]() | clearGroups () Called by reset to clear the group arrays |
A matcher is a non thread-safe object used to scan strings using a given Pattern object. Using aMatcher
is the preferred method for scanning strings. Matchers are not thread-safe. Matchers require very little dynamic memory, hence one is encouraged to create several instances of a matcher when necessary as opposed to sharing a single instance of a matcher.The most common methods needed by the matcher are
matches
,findNextMatch
, andgetGroup
.matches
andfindNextMatch
both return success or failure, and further details can be gathered from their documentation.Unlike Java's
Matcher
, this class allows you to change the string you are matching against. This provides a small optimization, since you no longer need multiple matchers for a single pattern in a single thread.This class also provides an extremely handy method for replacing text with captured data via the
replaceWithGroups
method. A typical invocation looks like:char buf[10000]; std::string str = "\\5 (user name \\1) uses \\7 for his/her shell and \\6 is their home directory"; FILE * fp = fopen("/etc/passwd", "r"); Pattern::registerPattern("entry", "[^:]+"); Pattern * p = Pattern::compile("^({entry}):({entry}):({entry}):({entry}):({entry}):({entry}):({entry})$", Pattern::MULTILINE_MATCHING | Pattern::UNIX_LINE_MODE); Matcher * m = p->createMatcher(""); while (fgets(buf, 9999, fp)) { m->setString(buf); if (m->matches()) { printf("%s\n", m->replaceWithGroups(str).c_str()); } } fclose(fp);Calling any of the following functions before first calling
matches
,findFirstMatch
, orfindNextMatch
results in undefined behavior and may cause your program to crash.
- replaceWithGroups
getStartingIndex getEndingIndex getGroup getGroups The function
findFirstMatch
will attempt to find the first match in the input string. The same results can be obtained by first callingreset
followed byfindNextMatch
.To eliminate the necessity of looping through a string to find all the matching substrings,
findAll
was created. The function will find all matching substrings and return them in avector
. If you need to examine specific capture groups within the substrings, then this method should not be used.
str
with the appropriate captured
text. str
should have at least one back reference, otherwise
this function does nothing.
Alphabetic index HTML hierarchy of classes or Java