Reputation: 1393
I have a section in file:
[Source]
[Source.Ia32]
[Source.Ia64]
I have created the expression as:
const boost::regex source_line_pattern ("(Sources)(.*?)");
Now, I am trying to match the string, but I am not able to match; it is always returning 0.
if (boost::regex_match ( sToken, source_line_pattern ) )
return TRUE;
Please note that sToken value is [Source]. [Source.Ia32]... and so on.
Thanks,
Upvotes: 0
Views: 179
Reputation: 153929
There are at least two problems with your code. First, the
regular expression you give contains the literal string
"Sources"
, and not "Source"
, which is what you seem to be
trying to match. The second is that boost::regex_match
is
bound: it must match the entire string. What you seem to want
is boost::regex_search
. Depending on what you are doing,
however, it might be better to try to match the entire string:
"\\[Source(?:\\.(\\w+))?\\]\\s*"
. Which provides for capture of
the trailing part, if present (but not the leading
"Source"
---no point, in general, in capturing something that is
a constant).
Note too that the sequence ".*?"
is very dubious. Normally,
I would expect the regular expression parser to fail if
a (non-escaped) '*'
is followed by a '?'
.
Upvotes: 2
Reputation: 21113
The issue is that boost::regex_match
only returns true if the entire input string is matched by the regex. So the '[' and ']' are not matched by your current regex, and it will fail.
Your options are either to use boost::regex_search
, which will search for a substring of the input that matches your regex, or modify your regex to accept the entire string being passed in.
Upvotes: 1