如何将 Perl 正则表达式转换为与 Boost::Regex 一起使用?

发布于 2024-08-26 13:19:03 字数 154 浏览 4 评论 0原文

对于以 ingeden 结尾的单词,与此 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 技术交流群。

扫码二维码加入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")

嗼ふ静 2024-09-02 13:19:03
/^[\.:\,()\'\`-]/

应该变成

"^[.:,()'`-]"

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, not all numbers.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文