简体   繁体   English

Python正则表达式字符串与不同的搜索字符串匹配

[英]Python regex string matching with varying search string

Is there anyway in python to be able to perform: python是否有能力执行:

"DDx" should match "01x", "10x", "11x, "00x"

in an elegant way in Python? 用Python优雅地?

The easiest way I see to do this is by using regex, which in this case would be: 我看到的最简单的方法是使用正则表达式,在这种情况下,它是:
re.search('\\d\\dx',line)

Is there anyway to dynamically update this regex? 反正有动态更新此正则表达式吗?
In case the input is: 如果输入是:
"D0x" then regex: \\d0x "D0x"然后是正则表达式: \\d0x

Please help. 请帮忙。

Using Python 2.7 使用Python 2.7

EDIT 编辑

In simpler terms, my question is: 简单来说,我的问题是:

>>> str = "DDx"
>>> str.replace('\d','D')
>>> re.search(<use str here>,line)

Or any alternate approach 或任何其他方法

I think I found the answer: 我想我找到了答案:

>>> s = "DDx"
>>> s = s.replace('D','\d')
>>> p = "01x"
>>> c = re.search(s,p)
>>> print c.group(0)
>>> "01x"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM