Keyple C++
0.9.0
Keyple, an Open Source SDK for Ticketing
|
#include <Matcher.h>
Public Member Functions | |
Matcher (const Pattern *parent, const std::string &text) | |
bool | match (const int from, const int anchor) const |
bool | matches () |
std::string | replaceAll (const std::string &replacement) const |
bool | find () |
bool | find (const int start) |
const std::string | group (const int group) const |
const std::string | group () const |
bool | search (const int from) |
int | getTextLength () const |
Matcher * | reset () |
keyple::common::Matcher::Matcher | ( | const Pattern * | parent, |
const std::string & | text | ||
) |
All matchers have the state used by Pattern during a match.
bool keyple::common::Matcher::find | ( | ) |
Attempts to find the next subsequence of the input sequence that matches the pattern.
This method starts at the beginning of this matcher's region, o, if a previous invocation of the method was successful and the matcher has not since been reset, at the first character not matched by the previous match.
If the match succeeds then more information can be obtained viw the start, end, group methods.
bool keyple::common::Matcher::find | ( | const int | start | ) |
Resets this matcher and then attempts to find the next subsequence of the input sequence that matches the pattern, starting at the specified index.
If the match succeeds then more information can be obtained viw the start, end, group methods, and subsequent invocations of the find() method will start at the first character not matched by this match.
IndexOutOfBoundException | if start is less than zero of if start is greated than the length of the input sequence. |
int keyple::common::Matcher::getTextLength | ( | ) | const |
Returns the end index of the text.
const std::string keyple::common::Matcher::group | ( | ) | const |
Returns the text that matched the whole regular expression.
IllegalStateException | if no successful match has been made |
const std::string keyple::common::Matcher::group | ( | const int | group | ) | const |
Returns the text that matched a given group of the regular expression. Explicit capturing groups in the pattern are numbered left to right in order of their opening parenthesis, starting at 1. The special group 0 represents the entire match (as if the entier pattern is surrounded by an implicit capturing group).
For example, "a((b)c)" matching "abc" would give the following groups: 0 "abc" 1 "bc" 2 "b"
An optional capturing group that failed to match as part of an overall sucessful match (for example "a(b)?c" matching "ac") returns null. A capturing group that matched the empty string (for example, "a(b?)c" matching "ac") returns the empty string.
IllegalStateException | if no successful match has been made |
bool keyple::common::Matcher::match | ( | const int | from, |
const int | anchor | ||
) | const |
Initiates a search for an anchored match to a Pattern within the given bounds. The groups are filled with default values and the match of the root of the state machine is called. The state machine will hold the state of the match as it proceeds in this matcher.
bool keyple::common::Matcher::matches | ( | ) |
Attempts to match the entire region against the pattern.
If the match succeeds then more information can be obtained via the start
, end
, and group
methods.
true
if, and only if, the entire region sequence matches this matcher's pattern std::string keyple::common::Matcher::replaceAll | ( | const std::string & | replacement | ) | const |
Replaces every subsequence of the input sequence that matches the pattern with the given replacement string.
This method first resets this matcher. It then scans the input sequence looking for matches of the pattern. Characters that are not part of any match are appended directly to the result string; each match is replaced in the result by the replacement string. The replacement string may contain references to captured subsequences as in the appendReplacement() method.
Note that backslashes (\
) and dollar signs ($
) in the replacement string may cause the results to be different than if it were being treated as a literal replacement string. Dollar signs may be treated as references to captured subsequences as described above, and backslashes are used to escape literal characters in the replacement string.
Given the regular expression a*b
, the input "aabfooaabfooabfoob"
, and the replacement string "-"
, an invocation of this method on a matcher for that expression would yield the string "-foo-foo-foo-"
.
Invoking this method changes this matcher's state. If the matcher is to be used in further matching operations then it should first be reset.
replacement | The replacement string |
Matcher * keyple::common::Matcher::reset | ( | ) |
Resets this matcher.
Resetting a matcher discards all of its explicit states information and sets its append position to zero. The matcher's region is set to the default region, which is its entire character sequence. The anchoring and transparency of this matcher's region boundaries are unaffected.
bool keyple::common::Matcher::search | ( | const int | from | ) |
Initiates a search to find a Pattern within the given bounds. The groups are filled with defaults values and the match of the root of the state machine is called. The state machine will hold the state of the match as it proceeds in this matcher.
Matcher.from is not set here, because it is the 'hard' boundary of the start of the search which anchors will set to. The from param is the 'soft' boundary of the start of the search, meaning that the regex tries to match at that index but won't match there. Subsequent calls to the search methods start at a new 'soft' boundary which is the end of the previous match.