TR1 :: regex_match-不使用整个单词进行匹配 [英] TR1::regex_match - not matching using whole words
问题描述
我正在使用TR1 :: Regex,并且希望在字符串中某些单词不匹配时使用.每个关键字之前和之后都可以包含其他文本.
例如:测试字符串中除"Rhino"外,与"Elephant"匹配.
Source ="Elephant Rhino"-没有匹配项
Source ="Elephant Crocodile"-Match
Source ="The Gray Elephant看到了犀牛的奔跑"-没有匹配结果
来源=灰象看见了鳄鱼游泳" =匹配
这在能力范围内吗?
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
推荐答案
您将要检查是否存在子字符串;例如
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)
将返回true
,因为正则表达式与str
的子字符串匹配.
will return true
because the regular expression matches a substring of str
.
[添加到ctwi001发布的内容中.]
在两个调用中执行此操作会更简单,在第一个调用中检查您的"Except"字样.如果搜索结果为假,则在第二次调用中检查您的包含"字词.如果该检查为真,您就可以继续.
[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.
这篇关于TR1 :: regex_match-不使用整个单词进行匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!