Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ 11 regex: checking if string starts with regex

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?

like image 794
EvanED Avatar asked Aug 08 '12 18:08

EvanED


People also ask

How do you search for a regex pattern at the beginning of a string?

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.

Which character starts with in regex?

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.

What is difference [] and () in regex?

[] denotes a character class. () denotes a capturing group. [a-z0-9] -- One character that is in the range of a-z OR 0-9.


1 Answers

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.

like image 184
kennytm Avatar answered Oct 06 '22 09:10

kennytm