|
Boost : |
From: John Maddock (John_Maddock_at_[hidden])
Date: 2001-08-25 06:52:55
>This is most likely my error, but I am trying to do the following:
1) A user types in a search string
2) I add the prefix ".*" and suffix ".*" to the string
When I call regex_match, anything and everything matches, regardless of
what
the user typed. I would like the following results, and am guessing I am
not using the correct regular expression syntax:
a) User types "Test"
b) Internally, I create the regular expression ".*Test.*"
Results (String : result)
"A Test" : true
"Testing 123" : false
"foo" : false
Using regex, regex_match is returning true for "foo". I tried changing the
prefix to ".*?" to ensure a non-greedy search, but the results were the
same.
<
OK, first things first, heres the code I used to test this:
boost::regex e(".*Test.*");
boost::cmatch m;
if(regex_match("A Test", m, e))
{
cout << "matched \"A Test\"" << endl;
}
if(regex_match("Testing 123", m, e))
{
cout << "matched \"Testing 123\"" << endl;
}
if(regex_match("foo", m, e))
{
cout << "matched \"foo\"" << endl;
}
This gave the expected results:
"A Test": matched
"Testing 123": Matched
"foo": not matched
There is one possible source of error which may have confused you: if no
match is found then the regex_match structure is left unchanged - in other
words it may still contain a match from a previous invocation - you should
always check the return value from regex_match to see if a match occured,
and not the regex_match struct.
I would also suggest that rather than pre/post pending .*, that you use
regex_search instead - this is likely to be much more efficient.
Finally if you don't want to find the "Test" in "Testing", then prepend
with \< and append \> to ensure that only whole words are found.
- John Maddock
http://ourworld.compuserve.com/homepages/john_maddock/
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk