I am using C++ 11's <regex>
support, and would like to check whether the beginning of a string matches a regular expression. [I can switch to Boost if that helps, but my impression is that they're basically the same.]
Obviously if I have control of the actual textual representation of the expression, I can just stick a ^
at the beginning of it as an anchor.
However, what if I just have a regex
(or basic_regex
) object? Can I modify the regular expression it represents to add the anchor? Or do I have to use regex_search
, get the result, and check whether it starts at position 0?
The meta character “^” matches the beginning of a particular string i.e. it matches the first character of the string. For example, The expression “^\d” matches the string/line starting with a digit. The expression “^[a-z]” matches the string/line starting with a lower case alphabet.
As usual, the regex engine starts at the first character: 7. The first token in the regular expression is ^. Since this token is a zero-length token, the engine does not try to match it with the character, but rather with the position before the character that the regex engine has reached so far.
[] denotes a character class. () denotes a capturing group. [a-z0-9] -- One character that is in the range of a-z OR 0-9.
You could add the std::regex_constants::match_continuous
flag when using regex_search
, for example, the following prints "1" and "0":
#include <regex>
#include <string>
int main()
{
std::regex rx ("\\d+");
printf("%d\n", std::regex_search("12345abc1234", rx,
std::regex_constants::match_continuous));
printf("%d\n", std::regex_search("abc12345", rx,
std::regex_constants::match_continuous));
return 0;
}
The flag means (C++11 §28.5.2/1 = Table 139):
The expression shall only match a sub-sequence that begins at
first
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With