我需要从字符串中提取一些数据,字符串是这样的fixed-string-<number>-<string>
。 Fixed string
总是相同的,我需要提取数字和它的字符串。正则表达式提取数字和字符串
在蟒蛇3.5,我使用的是接下来的正则表达式
str = 'initial-string/fixed-string-124-jeff-thompson'
result = re.match('fixed-string-([0-9]*)-(.*)', str)
print (result)
但结果总是None
值,我查了字符串,并将其井形成。
我在做什么错?
更新
testing = 'first-string/fixed-string-123-jeff-thompson'
pattern = r'fixed-string-(\d+)-(.*)'
result = re.match(pattern, testing)
我进行了测试,代码仍然返回我None
。
谢谢你。
不要使用'str'作为变量名。你的正则表达式确实有效,参见[这个示例](https://ideone.com/g4r2yC)。请注意,'re.match'只在字符串start处寻找匹配项。 –
请提供您希望工作的试用字符串。 – kazemakase