Click here to Skip to main content
15,892,059 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm using the TR1::Regex and want to not match when certain words are in the string. Additional text can be before and after each key word.

Ex: Match with "Elephant" except if "Rhino" is in the test string.
Source="Elephant Rhino" - No match
Source="Elephant Crocodile" - Match

Source="The Grey Elephant saw the Rhino run" - No Match
Source="The Grey Elephant saw the Crocodile swim" = Match

Is this within the capabilities?

Thanks
Posted

You'll want to check for a substring existing or not; e.g.
std::string str = "Elephant Rhino Crocodile";
std::tr1::regex rxRhino("Rhino");


Then
regex_search(str.begin(), str.end(), rx)


will return true because the regular expression matches a substring of str.
 
Share this answer
 
Comments
ctwi001 7-Apr-11 21:44pm    
Sorry, thanks Nishant for noticing I hadn't answered the full question, you'll need to do 2 calls:
1 - check if the undesired string is contained
2 - check if the desired string is contained

If (1) is FALSE and (2) is TRUE, then your overall check passes.
[Adding to what ctwi001 posted.]

It would be simpler to do this in two calls, in the first one check for your "Except" words. If that search is false, then in the 2nd call check for your "Contains" words. If that check's true you know you can proceed.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900