1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
|
The regular expression matching function tt(std::regex_match)hi(regex_match)
returns tt(true) if the regular expression defined in its provided
tt(regex) argument em(fully) matches the provided target text. This means that
tt(match_results::prefix) and tt(match_results::suffix) must return empty
strings. But defining sub-expressions is OK.
The following overloaded variants of this function are available:
itemization(
ittq(bool regex_match(BidirConstIter first, BidirConstIter last,
std::match_results &results,
std::regex const &re))
(tt(BidirConstIter) is a bidirectional const iterator. The range
rangett(first, last) defines the target text. The match results are
returned in tt(results). The types of the iterators must match the
type of the tt(match_results) that's used. E.g., a tt(cmatch) should
be used if the iterators are of tt(char const *) types, and a
tt(smatch) should be used if the iterators are of
tt(string::const_iterator) types. Similar correspondence requirements
hold true for the other overloaded versions of this function;)
ittq(bool regex_match(BidirConstIter first, BidirConstIter last,
std::regex const &re))
(this function behaves like the previous function, but does not
return the results of the matching process in a tt(match_results)
object;)
ittq(bool regex_match(char const *target,
std::match_results &results, std::regex const &re))
(this function behaves like the first overloaded variant, using
the characters in tt(target) as its target text;)
ittq(bool regex_match(char const *str, std::regex const &re))
(this function behaves like the previous function but does not return
the match results;)
ittq(bool regex_match(std::string const &target,
std::match_results &results, std::regex const &re))
(this function behaves like the first overloaded variant, using
the characters in tt(target) as its target text;)
ittq(bool regex_match(std::string const &str, std::regex const &re))
(this function behaves like the previous function but does not return
the match results;)
itt(bool regex_match(std::string const &&, std::match_results &,
std::regex &) = delete)
(the tt(regex_match) function does not accept temporary tt(string)
objects as target strings, as this would result in invalid string
iterators in the tt(match_result) argument.)
)
Here is a small example: the regular expression matches the matched text
(provided by tt(argv[1])) if it starts with 5 digits and then
merely contains letters (tt([[:alpha:]])). The digits can be retrieved as
sub-expression 1:
verb(
#include <iostream>
#include <regex>
using namespace std;
int main(int argc, char const **argv)
{
regex re("(\\d{5})[[:alpha:]]+");
cmatch results;
if (not regex_match(argv[1], results, re))
cout << "No match\n";
else
cout << "size: " << results.size() << ": " <<
results.str(1) << " -- " << results.str() << '\n';
}
)
|