在 Python 中使用 .net 正则表达式
我有一些为 Microsoft 的 .net 正则表达式格式编写的正则表达式 我想在 Linux 机器上的 python 程序中使用它们。是否有兼容的库或覆盖它们的方法?如果我必须手动完成,您知道备忘单或指南吗?
I have some regular expressions written for the Microsoft's .net regex format and I want to use them in a python program on a Linux machine. Is there a compatibly library or a method of covering them? If I have to do it by hand, do you know of a cheat sheet or a guide?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
.NET 正则表达式中的大多数功能都可以在 Python 中使用;然而,还缺少一些东西:
(?>...)
或所有格量词++
、*+
、?+
。r"..."
原始字符串作为正则表达式,否则您会因反斜杠而出错。可以在此处找到全面的差异列表。
如果您需要经常进行正则表达式转换,RegexBuddy 可以为您完成(只要您不使用当然,目标正则表达式引擎没有的功能)。
Most of the features in .NET regexes are available in Python; however there are a few things missing:
(?>...)
or possessive quantifiers++
,*+
,?+
.r"..."
raw strings for regular expressions, or you'll trip up on backslashes.A comprehensive list of differences can be found here.
And if you need to do regex conversions often, RegexBuddy can do it for you (as long as you're not using features the destination regex engine doesn't have, of course).
re
模块 的文档涵盖了 Python 正则表达式语法。 FWIW,除了替换之外,语法非常相似。The docs for the
re
module cover Python regex syntax. FWIW, the syntax is very similar, except for the substitutions.在实现方面,我认为 CLR 和 Python 正则表达式引擎非常接近,比它们与 Perl 或 PCRE 等其他语言更接近。例如,我使用 Expresso 来创建和测试后来改编的复杂正则表达式到Python代码。
也就是说,我认为我从未见过真正的转换器,但由于它们非常相似,因此手动转换它们应该相对容易(同样,比我们谈论其他实现更容易)。
此页面对不同引擎之间进行了很好的比较,尽管它侧重于功能而不是相似性句法。
In terms of implementation I think the CLR and Python regex engines are very close, more so than either of them to other languages like Perl or the PCRE. For example, I've used Expresso to create and test complicated regular expressions that I've later adapted to Python code.
That said, I don't think I've ever seen an actual converter, but since they're so similar it should be relatively easy to convert them manually (again, more so than if we were talking about other implementations).
This page has good comparisons between the different engines, although it focuses on capabilities rather than similarity of syntax.
确定有不兼容的地方吗?至少对于正则表达式中的基本到中级的东西,所有实现在语法上都非常一致,除了是否应该转义括号之外。
Are you sure there's an incompatibility? At least for the basic to middle-level stuff in regexes, all the implementations pretty much agree on the syntax, except for maybe whether parens should be escaped or not.