问题描述
我正在尝试使用正则表达式来回答这个问题,并且发现虽然 regex_match
找到匹配项,但 regex_search
没有.
以下程序是用g++ 4.7.1编译的:
#include <regex>#include <iostream>主函数(){const std::string s = "/home/toto/FILE_mysymbol_EVENT.DAT";std::regex rgx(".*FILE_(.+)_EVENT\.DAT.*");std::smatch 匹配;if (std::regex_match(s.begin(), s.end(), rgx))std::cout <<正则表达式匹配:匹配 ";别的std::cout <<"regex_match: 不匹配 ";if (std::regex_search(s.begin(), s.end(), match, rgx))std::cout <<"regex_search: 匹配 ";别的std::cout <<"regex_search: 不匹配 ";}
输出:
<上一页>正则表达式匹配:匹配regex_search: 不匹配我的假设是两者都应该匹配错误,还是 GCC 4.7.1 中的库有问题?
解决方案您的正则表达式 工作正常(两者都匹配,这是正确的)在 VS 2012rc 中.
在 g++ 4.7.1
(-std=gnu++11)
中,如果使用:
".*FILE_(.+)_EVENT\.DAT.*"
,regex_match
匹配,但regex_search
不匹配.".*?FILE_(.+?)_EVENT\.DAT.*"
,regex_match
和regex_search
都不匹配 (O_o).
所有变体都应该匹配,但有些不匹配(原因已由 betabandido 指出).在 g++ 4.6.3 (-std=gnu++0x)
中,行为与 g++ 4.7.1 相同.
Boost (1.50) 正确匹配所有内容 两种模式变体.
总结:
<上一页>正则表达式匹配正则表达式搜索-----------------------------------------------------g++ 4.6.3 linux OK/- -g++ 4.7.1 linux OK/- -与 2010 年相比 好的 好的vs 2012rc OK OK提升 1.50 胜利 OK OKboost 1.50 linux OK OK-----------------------------------------------------关于你的模式,如果你意思是一个点字符'.'
,那么你应该这样写("\."
).您还可以通过使用非贪婪修饰符 (?
) 来减少回溯:
".*?FILE_(.+?)_EVENT\.DAT.*"
I was experimenting with regular expression in trying to make an answer to this question, and found that while regex_match
finds a match, regex_search
does not.
The following program was compiled with g++ 4.7.1:
#include <regex>
#include <iostream>
int main()
{
const std::string s = "/home/toto/FILE_mysymbol_EVENT.DAT";
std::regex rgx(".*FILE_(.+)_EVENT\.DAT.*");
std::smatch match;
if (std::regex_match(s.begin(), s.end(), rgx))
std::cout << "regex_match: match
";
else
std::cout << "regex_match: no match
";
if (std::regex_search(s.begin(), s.end(), match, rgx))
std::cout << "regex_search: match
";
else
std::cout << "regex_search: no match
";
}
Output:
regex_match: match regex_search: no match
Is my assumption that both should match wrong, or might there a problem with the library in GCC 4.7.1?
Your regex works fine (both match, which is correct) in VS 2012rc.
In g++ 4.7.1 (-std=gnu++11)
, if using:
".*FILE_(.+)_EVENT\.DAT.*"
,regex_match
matches, butregex_search
doesn't.".*?FILE_(.+?)_EVENT\.DAT.*"
, neitherregex_match
norregex_search
matches (O_o).
All variants should match but some don't (for reasons that have been pointed out already by betabandido). In g++ 4.6.3 (-std=gnu++0x)
, the behavior is identical to g++ 4.7.1.
Boost (1.50) matches everything correctly w/both pattern varieties.
Summary:
regex_match regex_search ----------------------------------------------------- g++ 4.6.3 linux OK/- - g++ 4.7.1 linux OK/- - vs 2010 OK OK vs 2012rc OK OK boost 1.50 win OK OK boost 1.50 linux OK OK -----------------------------------------------------
Regarding your pattern, if you mean a dot character '.'
, then you should write so ("\."
). You can also reduce backtracking by using non-greedy modifiers (?
):
".*?FILE_(.+?)_EVENT\.DAT.*"
这篇关于regex_match 和 regex_search 之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!