【发布时间】:2012-08-21 02:01:09
【问题描述】:
现在我用的是VC++ 2010,但是VC++ 2010的syntax_option_type只包含以下选项:
static const flag_type icase = regex_constants::icase;
static const flag_type nosubs = regex_constants::nosubs;
static const flag_type optimize = regex_constants::optimize;
static const flag_type collate = regex_constants::collate;
static const flag_type ECMAScript = regex_constants::ECMAScript;
static const flag_type basic = regex_constants::basic;
static const flag_type extended = regex_constants::extended;
static const flag_type awk = regex_constants::awk;
static const flag_type grep = regex_constants::grep;
static const flag_type egrep = regex_constants::egrep;
它不包含 perl_syntax_group(Boost 库有选项)。 但是,我不想使用 Boost 库。
用 Perl 编写的正则表达式有很多,所以我想将现有的 Perl 正则表达式转换为 ECMAScript(或 VC++ 2010 支持的任何一个)。转换后我可以直接在VC++ 2010中使用等价的正则表达式,而无需使用第三方库。
一个例子:
const boost::tregex e(__T("\\A(\\d{3,4})[- ]?(\\d{4})[- ]?(\\d{4})[- ]?(\\d{4})\\z"));
const CString human_format = __T("$1-$2-$3-$4");
CString human_readable_card_number(const CString& s)
{
return boost::regex_replace(s, e, human_format);
}
CString credit_card_number = "1234567887654321";
credit_card_number = human_readable_card_number(credit_card_number);
assert(credit_card_number == "1234-5678-8765-4321");
在上面的例子中,我想做的是将e 和format 转换为ECMAScript 样式表达式。
是否有可能找到一种通用方法将所有 Perl 正则表达式转换为 ECMAScript 样式?
是否有一些工具可以做到这一点?
任何帮助将不胜感激!
【问题讨论】:
-
ECMA 脚本正则表达式是 Perl 正则表达式的子集。因此,如果您使用 Perl 正则表达式独有的功能,则无法转换。
-
感谢您的回复。如果我不使用 Perl 正则表达式独有的功能,是否有可能找到一种通用的方法来进行转换?
标签: c++ regex windows linux visual-studio-2010