在 Python 中使用 .net 正则表达式

发布于 2024-10-01 00:44:19 字数 224 浏览 0 评论 0原文

我有一些为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

唔猫 2024-10-08 00:44:19

.NET 正则表达式中的大多数功能都可以在 Python 中使用;然而,还缺少一些东西:

  1. Python不支持lookbehinds内的变量重复,仅支持固定长度。
  2. Python 对命名捕获使用不同的语法。
  3. Python 以不同的方式匹配 Unicode 字符。
  4. Python 没有原子组 (?>...) 或所有格量词 ++*+?+
  5. 在 Python 中,请使用 r"..." 原始字符串作为正则表达式,否则您会因反斜杠而出错。

可以在此处找到全面的差异列表

如果您需要经常进行正则表达式转换,RegexBuddy 可以为您完成(只要您不使用当然,目标正则表达式引擎没有的功能)。

Most of the features in .NET regexes are available in Python; however there are a few things missing:

  1. Python doesn't support variable repetition inside lookbehinds, only fixed-length.
  2. Python uses a different syntax for named captures.
  3. Python matches Unicode characters differently.
  4. Python doesn't have atomic groups (?>...) or possessive quantifiers ++, *+, ?+.
  5. In Python, use 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).

拥抱没勇气 2024-10-08 00:44:19

re 模块 的文档涵盖了 Python 正则表达式语法。 FWIW,除了替换之外,语法非常相似。

The docs for the re module cover Python regex syntax. FWIW, the syntax is very similar, except for the substitutions.

岁月打碎记忆 2024-10-08 00:44:19

在实现方面,我认为 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.

遮了一弯 2024-10-08 00:44:19

确定有不兼容的地方吗?至少对于正则表达式中的基本到中级的东西,所有实现在语法上都非常一致,除了是否应该转义括号之外。

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.

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