#include <Pt/Regex.h>
Regular Expressions for Unicode Strings. More...
Public Member Functions | |
| Regex () | |
| Default Constructor. | |
| Regex (const Pt::Char *ex) | |
| Construct from regex string. | |
| Regex (const Pt::String &ex) | |
| Construct from regex string. | |
| Regex (const Regex &other) | |
| Copy constructor. | |
| ~Regex () | |
| Destructor. | |
| bool | match (const Pt::String &str, RegexSMatch &sm) const |
| Matches the regular experession to a string. More... | |
| bool | match (const Pt::String &str) const |
| Returns true if string matches. | |
| bool | match (const Char *str, RegexSMatch &sm) const |
| Matches the regular experession to a string. More... | |
| bool | match (const Char *str) const |
| Returns true if string matches. | |
| Regex & | operator= (const Regex &other) |
| Assignment operator. | |
The Pt::Regex class allows to match a string pattern in unicode text. It resembles the std::basic_regex class and can be used to support systems, where std::basic_regex is not available in the standard C++ implementation. The syntax for the match pattern is similar to the extended POSIX syntax. The following table shows the special characters that can be used to write regular expressions:
| . | Any character |
| [ ] | A character in a given set |
| [^ ] | A character not in a given set |
| ^ | Begin of line |
| $ | End of line |
| \< | Begin of a word |
| \> | End of a word |
| ( ) | A marked subexpression |
| * | Matches the preceding element zero or more times |
| ? | Matches the preceding element zero or one time |
| + | Matches the preceding element one or more times |
| | | Matches either the expression before or after the operator |
| \ | Escapes the next character |
The regular expression is constructed from a unicode string, either a Pt::String or a null-terminated sequence of unicode characters of type Pt::Char. It can then be used to match it against unicode strings as shown in the next example:
It is also possibe to match a regular expression against a unicode input string and find out what tokens in the string actually matched. The match() member function has an overload, which fills a Pt::RegexSMatch with the result. Note that the first result at index 0 is always the input string itself. The following example illustrates this:
| bool match | ( | const Pt::String & | str, |
| RegexSMatch & | sm | ||
| ) | const |
The result sm holds pointers into the original string that was matched and therefore should not be used after the original string was destroyed.
| bool match | ( | const Char * | str, |
| RegexSMatch & | sm | ||
| ) | const |
The result @ sm holds pointers into the original string that was matched and therefore should not be used after the original string was destroyed.