[英]Python regular expression searching for a number
I'm new to regex, but looking through a string to find whether or not a pattern exists. 我是regex的新手,但是通过字符串查找是否存在模式。
I've tried using the following python code: 我尝试使用以下python代码:
prog=re.compile('555.555.555')
m=prog.match(somestring)
if m: print somestring
I'm trying to find 3 groups of 5's separated by any number. 我正在尝试查找3组,每组5个,中间用任意数字分隔。 This code doesn't return what I'm looking for though.
这段代码不会返回我正在寻找的东西。
Any suggestions? 有什么建议么?
Edit: 编辑:
Here's some code to test a more basic version: 这是一些测试更基本版本的代码:
i,found=0,0
while found==0:
istr=str(i)
prog=re.compile(r'1\d2\d3')
m=prog.search(istr)
if m:
print i
found=1
break
i=i+1
This returns 1312 rather than 10203 这将返回1312而不是10203
Your regex is OK (sort of), but you're using it wrong. 您的正则表达式还可以(有点),但您使用的是错误的。 You need
你需要
m = prog.search(somestring)
or the regex will only find a match if it is at the beginning of the string . 否则regex 只会在字符串的开头找到匹配项 。
Also, if you really only want to allow a single digit between each group of 555
s, use 另外,如果您确实只想在每
555
秒之间允许一个数字,请使用
prog = re.compile(r'555\d555\d555')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.