Boost RegEx:具体问题
我正在尝试使用这个表达式:
Expression: "\w{1,}\s*?\-\-(\>)?\s*?\w{1,}"
请记住,我在代码中用第二个 \ 转义了 \ 。
在下面的字符串中搜索时。我想我很接近,但没有雪茄。我希望上面的表达式能够在下面的文本中找到匹配项。我哪里出错了?
Text: "AB --> CD"
Text: "AB --> Z"
Text: "A --> 123d"
etc.
使用的资源:
更新
评论对我有帮助。我仍然希望看到人们在我的线程上发布,出于记录目的,帮助他们掌握正则表达式的正则表达式网站。无论如何,我的代码(主要是从 boost 网站复制的)是。
/* All captures from a regular expression */
#include <boost/regex.hpp>
#include <iostream>
/* Compiled with g++ -o regex_tut -lboost_regex -Wall ./regex_tut.cpp */
void print_captures(const std::string& regx, const std::string& text)
{
boost::regex e(regx);
boost::smatch what;
std::cout << "Expression: \"" << regx << "\"\n";
std::cout << "Text: \"" << text << "\"\n";
if(boost::regex_match(text, what, e, boost::match_extra))
{
unsigned i;
std::cout << "** Match found **\n Sub-Expressions:\n";
for(i = 0; i < what.size(); ++i) {
std::cout << " $" << i << " = \"" << what[i] << "\"\n";
}
}
else
{
std::cout << "** No Match found **\n";
}
}
int main(int argc, char* argv[ ])
{
print_captures("^\\w+\\s*-->?\\s*\\w+\\s*(\\(\\d+\\))?", "AB --> CD (12)" );
return 0;
}
似乎有效。请尽管这样我可以接受您最喜欢的网站上的答案,并给新手一些指示 =)。
I am trying to use this expression:
Expression: "\w{1,}\s*?\-\-(\>)?\s*?\w{1,}"
Keep in mind I am escaping the \ with a second \ in my code.
When searching in the strings below. I think I am close, but no cigar. I want the expression above to be able to find matches in the text below. Where am I going wrong?
Text: "AB --> CD"
Text: "AB --> Z"
Text: "A --> 123d"
etc.
Resources Used:
UPDATE
The comment helped me. I would still like to see people post on my thread, for record keeping purposes, regex sites that have helped them master regex. Anyways my code (mostly copied from the boost website) is.
/* All captures from a regular expression */
#include <boost/regex.hpp>
#include <iostream>
/* Compiled with g++ -o regex_tut -lboost_regex -Wall ./regex_tut.cpp */
void print_captures(const std::string& regx, const std::string& text)
{
boost::regex e(regx);
boost::smatch what;
std::cout << "Expression: \"" << regx << "\"\n";
std::cout << "Text: \"" << text << "\"\n";
if(boost::regex_match(text, what, e, boost::match_extra))
{
unsigned i;
std::cout << "** Match found **\n Sub-Expressions:\n";
for(i = 0; i < what.size(); ++i) {
std::cout << " $" << i << " = \"" << what[i] << "\"\n";
}
}
else
{
std::cout << "** No Match found **\n";
}
}
int main(int argc, char* argv[ ])
{
print_captures("^\\w+\\s*-->?\\s*\\w+\\s*(\\(\\d+\\))?", "AB --> CD (12)" );
return 0;
}
Seems to work. Please though so I can accept an answer post your favorite site up and give a newb a few pointers =).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不确定我是否正确理解您的问题,但如果您希望您的正则表达式匹配
"AB --> CD"AB
和CD
代码> 您可以使用以下正则表达式:Not sure if i understood your question correctly, but if you want your regex to match for example
AB
andCD
in"AB --> CD"
you can use the following regex: