【问题标题】:Rewriting regular expression in python在python中重写正则表达式
【发布时间】:2014-11-12 13:24:21
【问题描述】:
re.compile('(?<!\(\s*)SELECT.*?FROM')

我想在 python 2.6 中编译正则表达式,但编译时出错。

python中的re模块不支持*/+/?,在正则表达式的lookbehind中,长度未确定。

有人评论建议我使用正则表达式,这是一个第三方模块。但是,我无法在公司服务器中使用第三方。

有人可以帮我重写正则表达式吗?

Target: "( SELECT blablabla" <- This could not be matched. 
"  SELECT blablabla" <- This could be matched, I mean no matter how many whitespace in front of the SELECT the string will always be matched. 

【问题讨论】:

  • 您在后视中使用 \s*。它们的大小必须是固定的。
  • 您不能有可变长度的后视。量词**? 可以是任意长度,因此是可变的,因此在python 中是不合法的。
  • @Jerry regex 模块支持可变长度的lookbehind。
  • 给我们错误和你要编译的表达式示例!
  • 所以你的意思是第三方进口在 python 中是非法的。

标签: python regex python-2.6 regular-language


【解决方案1】:

Python 2.x 不支持可变长度后视,可以使用模块 regex 代替 re 或更改您的模式:

re.compile('(?<!\()\s*SELECT.*?FROM')

【讨论】:

  • 纠正这个Python 2.x not support variable length lookbehind,我不知道2.7以下的版本,但2.7是通过正则表达式模块完成的。
猜你喜欢
  • 2012-06-13
  • 1970-01-01
  • 2016-02-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-11-15
  • 2021-10-26
相关资源
最近更新 更多