'Regex match whole string
I have the following pattern:
[ \n\t]*([a-zA-Z][a-zA-Z0-9_]*)[ \n\t]+((char)[ \n\t]*\[[ \n\t]*([0-9]+)[ \t\n]*\]|(char)|(int)|(double)|(bool)|(blob)[ \n\t]*\[[ \n\t]*([0-9]+)[ \t\n]*\])[ \n\t]*
You can try it here: http://regex101.com/r/vA0xG9
In the first capturing group ([a-zA-Z][a-zA-Z0-9_]*)
, I want to grab words that only starts with a-zA-Z
.
The two following strings matches equally:
cpf char[12]
,
9cpf char[12]
It ignores the 9
digit and matches equally to the first string.
I've tried to use this capturing group: (ˆ[a-zA-Z][a-zA-Z0-9_]*$)
, but it didn't work.
I'm using lib regex.h
.
What should I do?
Thanks.
Solution 1:[1]
Put ^ at the beginning of the whole thing and $ at the end
^[ \n\t]*([a-zA-Z][a-zA-Z0-9_]*)[ \n\t]+((char)[ \n\t]*\[[ \n\t]*([0-9]+)[ \t\n]*\]|(char)|(int)|(double)|(bool)|(blob)[ \n\t]*\[[ \n\t]*([0-9]+)[ \t\n]*\])[ \n\t]*$
I would also suggest \s instead of [ \n\t] if you want to match whitespace.
Solution 2:[2]
In C++, there is a handy regex method that anchors the match to the whole string automatically: std::regex_match
:
Determines if the regular expression e matches the entire target character sequence, which may be specified as std::string, a C-string, or an iterator pair.
This way, you will avoid issues with mistyped ^
as ˆ
as well as cases when you have alternation (e.g. ^A|B$
won't match strings only equal to A
or B
, you need ^(A|B)$
or ^(?:A|B)$
).
Note that there is an equivalent boost::regex_match
method.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | user3352495 |
Solution 2 | Wiktor Stribiżew |