Python正则表达式,两个单词之间的单词匹配正确,但不匹配

时间:2014-11-03 17:58:59

标签: python regex

我知道.是一个元字符,当我想搜索它时,我必须逃避它。 所以我在做:

import re

fileName = "A011C009_140914_FPS025/Proxy/A011C009_140914_FPS25.1001.jpg"
regExpression = "FPS([^\\]]+)\\."
m = re.findall(regExpression, fileName)

myString = m[0]
print(myString.strip("0"))

我希望结果 25 ,但我得到 25 / Proxy / A011C009_140914_FPS25.1001

搜索另一个元字符/可以正常工作:

regExpression = "FPS([^\\]]+)\\/Proxy"
m = re.findall(regExpression, fileName)

2 个答案:

答案 0 :(得分:1)

你的正则表达式应该是:

regExpression = "FPS([^./]+)\\."

<强>代码:

import re

fileName = "A011C009_140914_FPS025/Proxy/A011C009_140914_FPS25.1001.jpg"
regExpression = "FPS([^./]+)\\."

print ( re.findall(regExpression, fileName) )
['25']

答案 1 :(得分:1)

正则表达式怎么样

FPS([^\/]+)\/

将匹配字符串中025后的FPS

>>> regExpression = "FPS([^\/]+)\/"
>>> m = re.findall(regExpression, fileName)
>>> print m
['025']