我想在C++中做一些正则表达式,所以我查看了interwebz(是的,我是C++的初学者/中级)并找到了这个SO答案.
我真的不知道在boost :: regex和boost :: xpressive之间做什么选择.有哪些优点/缺点?
我还读到与boost :: regex相对的boost :: xpressive是一个只有头的库.是不是很难在Linux和Windows上静态编译boost :: regex(我几乎总是编写跨平台应用程序)?
我也对编译时间的比较感兴趣.我有一个使用boost :: xpressive的当前实现,我对编译时间不太满意(但我没有与boost :: regex进行比较).
当然,我也对正则表达式实现的其他建议持开放态度.要求是免费的(如在啤酒中)并与http://nclabs.org/license.php兼容.
说我有一个sregex像这样的对象:
boost::xpressive::sregex::compile("(?P<firstword>\\w+) (?<secondword>\\w+)!");
Run Code Online (Sandbox Code Playgroud)
我没有在xpressive文档中找到任何关于此的参考,尽管xpressive支持命名组就好了.
我知道可以通过组迭代很好,但是如何访问组名(如果组有名称)?
那么,我将如何遍历命名组?
string str = "hello world!\r\naa=`xxx_1`\r\nhello world!";
sregex rx = sregex::compile(".+=`(.+)_1`");
smatch what;
if( regex_match( str, what, rx ) )
{
std::cout << what[1] << '\n';
}
Run Code Online (Sandbox Code Playgroud)
这行不通,我使用boost.xpressive而不是boost.regex,如何匹配多行文本?
我刚开始使用Boost :: xpressive并发现它是一个很棒的库...我浏览了文档并尝试使用!运算符(零或一)但它不编译(VS2008).
我想匹配一个SIP地址,它可能是也可能不是以"sip"开头的:
#include <iostream>
#include <boost/xpressive/xpressive.hpp>
using namespace boost::xpressive;
using namespace std;
int main()
{
sregex re = !"sip:" >> *(_w | '.') >> '@' >> *(_w | '.');
smatch what;
for(;;)
{
string input;
cin >> input;
if(regex_match(input, what, re))
{
cout << "match!\n";
}
}
return 0;
}`
Run Code Online (Sandbox Code Playgroud) 第一次切换到GCC,我对编译器在这里告诉我的内容感到有些困惑.基本上,它的行为类似于boost :: xpressive :: wsregex未定义(我相信).
这是相关代码:
#include "criterion.h"
#include <string>
#include <boost/xpressive/xpressive.hpp>
//More lines of code omitted here
class perlRegex : public regexClass
{
private:
std::wstring regexString;
boost::xpressive::wsregex regex; // This is the line complained about
public:
unsigned __int32 getPriorityClass() const;
BOOL include(fileData &file) const;
unsigned int directoryCheck(const std::wstring& /*directory*/) const;
std::wstring debugTree() const;
perlRegex(const std::wstring& inRegex);
};
Run Code Online (Sandbox Code Playgroud)
这是错误:
regex.h:46: error: using-declaration for non-member at class scope
regex.h:46: error: expected `;' before "regex"
Run Code Online (Sandbox Code Playgroud)
我在这里感到困惑的是,我宣布成员,但它抱怨我在其他地方使用其他成员.
我忘记了#include什么吗?
在此先感谢Billy3
(或者我认为)......
我正在使用boost :: xpressive作为我的正则表达式引擎来解析一些东西,我得到一个段错误.我怀疑递归和我的坏正则表达式是罪魁祸首,因为gdb显示超过300个堆栈帧.所以,这是我的(区分大小写)正则表达式,用perl/python表示法:
begin([^e]+)e((?:[^b]|b(?!egin))+)
Run Code Online (Sandbox Code Playgroud)
我期望匹配
beginHEADER HEREeFOLLOWED BY SOME LONG LONG TEXT THAT GOES UNTIL NEXTbegin
Run Code Online (Sandbox Code Playgroud)
第一组中的第一个大写文本(HEADER HERE)和第二组中的第二个大写文本.如果与第2组匹配的文本很长,我总是得到段错误.
为什么不能这样做?