正则表达式误解或只是破坏执行? [英] Regular Expressions misunderstanding or just broken implementation?
问题描述
我尝试了一个非常简单的使用 regex_search
,不明白为什么我没有得到匹配:
唉,gcc-C ++ 0x-实现4.5似乎不工作,我得到一个链接错误这里
但这里是我的gcc-4.7.0尝试,很简单:
#include< iostream>
#include< string>
#include< regex>
using namespace std;
int main(){
regex rxWorld(world);
const string text =hello world!;
const auto t0 = text.cbegin();
smatch match;
const bool ok = regex_search(text,match,rxWorld);
/ * ... * /
}
ok == true
和在匹配
我把例子简化为一个非常简单的正则表达式。
但是通过在 / * ... * /
上打印代码,
cout< text:'<< text
<< 'ok:<< ok
<< size:< match.size();
cout<< pos:< match.position()
<< len:< match.length();
for(const auto& sub:match){
cout<< [<<(sub.first-t0)<...<<(sub.second-t0)
< :<< sub.matched
<< '<<< sub.str()
<< '];
}
cout<< endl;
输出为:
$ ./regex-search-01.x
text:'hello world!'ok:0 size:0 pos:-1 len:0
$ c $更新:我也尝试过 regex_search(t0,text.cend(),match,rxWorld) code>和 const char * text
,无变化。
`
我是否理解 regex_search
错了?我完全感到困惑。
解决方案正如你可以从 C ++ - 0xlbstdc ++的状态正则表达式支持不完整。
特别是match_results没有完成。
欢迎志愿者; - )
[As of gcc- 4.9] 2 将获得完全支持。
I tried a very simple use of regex_search
and can not understand why I do not get a match:
Alas, the gcc-C++0x-implementations 4.5 does not seem to be working, I get a link error here.
But here is my gcc-4.7.0 try, quite straightforward:
#include <iostream>
#include <string>
#include <regex>
using namespace std;
int main () {
regex rxWorld("world");
const string text = "hello world!";
const auto t0 = text.cbegin();
smatch match;
const bool ok = regex_search(text, match, rxWorld);
/* ... */
}
I think I should get ok==true
and something in match
as well. I reduced the example to a very simple regex for this. I tried slightly more complicated first.
But by printing code at /* ... */
says otherwise:
cout << " text:'" << text
<< "' ok:" << ok
<< " size:" << match.size();
cout << " pos:" << match.position()
<< " len:"<< match.length();
for(const auto& sub : match) {
cout << " ["<<(sub.first-t0)<<".."<<(sub.second-t0)
<< ":"<<sub.matched
<< "'"<<sub.str()
<< "']";
}
cout << endl;
The output is:
$ ./regex-search-01.x
text:'hello world!' ok:0 size:0 pos:-1 len:0
Update: I also tried regex_search(t0, text.cend(), match, rxWorld)
and const char* text
, no change.
`
Is my understanding of regex_search
wrong? I am completely baffled. Or is it just the gcc?
解决方案 As you can see from the C++-0x status of libstdc++ the regex support is incomplete.
In particular match_results are not finished. Iterators are not even started.
Volunteers are welcome ;-)
[EDIT] [As of gcc-4.9]2 will be fully supported.
这篇关于正则表达式误解或只是破坏执行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!