我无法将RegEx表达式转换为python。我知道'(\\d+)'
是单个整数的表达式,但我无法弄清楚如何得到一个[2-9]的整数。
RegEx表达式如下:
[2-9][p-z][a-h][2-9][a-z]*[p-z][2-9][p-z][2-9][p-z]
这是我目前的表达,但它产生了许多误报,因为它不够具体:
re1='(\\d+)' # Integer Number 1 re2='([a-z])' # Any Single Word Character (Not Whitespace) 1 re3='([a-z])' # Any Single Word Character (Not Whitespace) 2 re4='(\\d+)' # Integer Number 2 re5='((?:[a-z][a-z]+))' # Word 1 re6='(\\d+)' # Integer Number 3 re7='([a-z])' # Any Single Word Character (Not Whitespace) 3 re8='(.)' # Any Single Character 1 re9='([a-z])' # Any Single Word Character (Not Whitespace) 4
## Regex search for passcodes ## Thanks to Pierluigi Failla
rg = re.compile(re1+re2+re3+re4+re5+re6+re7+re8+re9,re.IGNORECASE|re.DOTALL)
m = rg.search(txt)
if m:
int1=m.group(1)
w1=m.group(2)
w2=m.group(3)
int2=m.group(4)
word1=m.group(5)
int3=m.group(6)
w3=m.group(7)
c1=m.group(8)
w4=m.group(9)
txt2='"'+int1+w1+w2+int2+word1+int3+w3+c1+w4+'"'
return [txt2]
答案 0 :(得分:2)
您应该能够在Python中使用2-9范围,如下所示:re1 = re.compile(r'[2-9]')
。然后我的控制台中的测试显示re1.match('7')
会根据需要返回MatchObject
,而re1.match('0')
也会根据需要返回None
。
您似乎也使用了[a-z]
中的范围re2
,您说您想要[p-z]
- 其他字符范围内的类似问题。
答案 1 :(得分:1)
我根据您在问题中看到的内容提出此代码:
import re
pat = ('([2-9])' # Integer Number 1
'([p-z])' # Any Single Word Character (Not Whitespace) 1
'([a-h])' # Any Single Word Character (Not Whitespace) 2
'([2-9])' # Integer Number 2
'([a-z]*[p-z]+)' # Word 1
'([2-9])' # Integer Number 3
'([p-z])' # Any Single Word Character (Not Whitespace) 3
'(.)' # Any Single Character 1
'([p-z])' # Any Single Word Character (Not Whitespace) 4
)
rg = re.compile(pat)
txt = 'jiji4pa6fmlgkfmoaz8p#q,,,,,,,,,,'
m = rg.search(txt)
if m:
text2 = "%s%s%s%s%s%s%s%s%s" % m.groups()
print text2
# prints 4pa6fmlgkfmoaz8p#q
text2 = ''.join(m.groups()) # is better