如何将 Perl 正则表达式转换为与 Boost::Regex 一起使用?
对于以 ing
或 ed
或 en
结尾的单词,与此 Perl 正则表达式等效的 Boost::Regex 是什么?
/ing$|ed$|en$/
...
What is the Boost::Regex equivalent of this Perl regex for words that end with ing
or ed
or en
?
/ing$|ed$|en$/
...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
最重要的区别是 C++ 中的正则表达式是字符串,因此所有正则表达式特定的反斜杠序列(例如
\w
和\d
都应该用双引号引起来 ("\\ w"
和"\\d"
)The most important difference is that regexps in C++ are strings so all regexp specific backslash sequences (such as
\w
and\d
should be double quoted ("\\w"
and"\\d"
)应该变成
C++ 中不存在特殊的 Perl 正则表达式分隔符
/
,因此正则表达式只是一个字符串。在这些字符串中,您需要注意正确转义反斜杠(原始正则表达式中的每个\
都是\\
)。但在你的例子中,所有这些反斜杠都是不必要的,所以我完全放弃了它们。还有其他注意事项;据我所知,Boost 库中不存在一些 Perl 功能(例如可变长度后向查找)。因此,可能无法简单地翻译任何正则表达式。不过,你的例子应该没问题。虽然有些很奇怪。
.*[0-9].*
将匹配任何包含数字的字符串,而不是所有数字
。should become
The special Perl regex delimiter
/
doesn't exist in C++, so regexes are just a string. In those strings, you need to take care to escape backslashes correctly (\\
for every\
in your original regex). In your example, though, all those backslashes were unnecessary, so I dropped them completely.There are other caveats; some Perl features (like variable-length lookbehind) don't exist in the Boost library, as far as I know. So it might not be possible to simply translate any regex. Your examples should be fine, though. Although some of them are weird.
.*[0-9].*
will match any string that contains a number somewhere, notall numbers
.