regexpr.h
regex
library is widely available, but as of this writing the regex library is not supported on gcc and some other major C++ compilers.
Here is a short example usage of the functions in this library:
string s = "aaaaaab"; if (regexMatch(s, "a*b")) { s = regexReplace(s, "a{3}b", "X"); // returns "aaaX" } ...
The regular expression functions here are implemented by sending the strings and regexes to the Java Back-End to run the operations in Java. This is a bit kludgy but we don't want to write our own regex parser from scratch. Using Java's is a compromise for now. Note that the performance of this implementation is slow and should not be relied upon in tight loops or performance-critical code.
The syntax of regular expressions accepted is the same as that accepted by Java's java.util.regex.Pattern
class and as described at the following URL:
Available since: 2014/03/01 version of C++ library
Functions | |
Returns true if the given string matches the given regular expression as a substring. | |
Returns the number of times the given regular expression is found inside the given string. | |
Replaces all occurrences of the given regular expression in the given string with the given replacement text, and returns the resulting string. |
bool regexMatch(string s, string regexp);
true
if the given string matches the given regular expression as a substring.
If the regular expression is not matched, returns false
.
Usage:
if (regexMatch(s, regexp)) { ... }
int regexMatchCount(string s, string regexp);
0
if there are no matches for the regular expression.
Usage:
int count = regexMatchCount(s, regexp);
string regexReplace(string s, string regexp, string replacement);
Usage:
s = regexReplace(s, regexp, replacement);