如何使用 python 脚本中的 PCRE 正则表达式? [英] How can I use PCRE regexes from a python script?

查看:84
本文介绍了如何使用 python 脚本中的 PCRE 正则表达式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 Python 脚本中的 PCRE 库 或类似的东西.这些脚本仅供个人使用,因此可移植性较差、快速且简洁的代码是可以接受的.如何告诉 python 向 pcre 发送一个字符串和一个正则表达式并返回结果?

I would like to use the PCRE library, or something very similar, from Python scripts. These scripts would be for personal use so less portable, fast and concise code would be acceptable. How can I tell python to send a string and a regex to pcre and get the result returned?

推荐答案

Tor,发现这个老问题,因为它是一个好问题,而且对于任何想要将项目从 PHP 迁移到 Python 的人来说仍然是及时的.

Tor, unearthing this old question because it's a good one, and still timely for anyone who wants to move a project from PHP to python.

我也是 PCRE 的忠实粉丝.在 Python 上,re 非常有限,但好消息是还有另一个名为 regex 的模块,它提供了 PCRE 的许多精彩功能,包括:

I too am a big fan of PCRE. On Python, re is quite limited, but the good news is that there's another module called regex that offers many of the wonderful features of PCRE, including:

  1. 原子组
  2. 所有格量词
  3. 递归
  4. 分支重置

还有更多,包括 PCRE 不提供的一些,例如类减法、可变长度后视和模糊匹配.

如果开发者将项目付诸实施,Python 将突然配备一个出色的正则表达式引擎,这将使其对一直对从 PHP 转换持观望态度的正则表达式爱好者更具吸引力.

If the developers carry the project to fruition, Python will suddenly be equipped with an outstanding regex engine, which will make it more attractive to regex lovers who have been on the fence about switching from PHP.

仔细阅读说明,因为该模块可以在两种模式下运行,一种是为了与 re 兼容,另一种是具有所有很酷的功能.您可以使用 (?V0)(?V1)

Read the directions carefully, because the module can be run in two modes, one for compatibility with re, the other with all the cool features. You specify the mode with (?V0) or (?V1)

在 Unix 上,使用 pip 安装非常简单.在 Windows 上,我还没有设法让 regex 模块与 64 位 python 一起工作.我卸载了 2.7 和 3.3 的 64 位安装,在 32 位中重新安装,并在两者上安装了 regex 模块.

On Unix, installation with pip is a snap. On Windows, I haven't managed to get the regex module to work with a 64-bit python. I uninstalled my 64-bit installs of 2.7 and 3.3, reinstalled in 32-bit, and installed the regex module on both.

从这里出发的地方:

  1. 正则表达式模块的下载页面.
  2. 关于 python 正则表达式模块的全面问题

这篇关于如何使用 python 脚本中的 PCRE 正则表达式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆