[英]Regular expression to extract number and string
I need to extract some data from a string, the string is something like this fixed-string-<number>-<string>
. 我需要从字符串中提取一些数据,该字符串类似于此
fixed-string-<number>-<string>
。 Fixed string
is always the same, I need to extact number and its string. Fixed string
始终是相同的,我需要确切的数字和它的字符串。
In python 3.5, I'm using the next regular expression 在python 3.5中,我正在使用下一个正则表达式
str = 'initial-string/fixed-string-124-jeff-thompson'
result = re.match('fixed-string-([0-9]*)-(.*)', str)
print (result)
But result is always None
value, I checked the string and it's well formed. 但是结果始终是
None
值,我检查了字符串,它的格式正确。
What am I doing wrong? 我究竟做错了什么?
Update 更新
testing = 'first-string/fixed-string-123-jeff-thompson'
pattern = r'fixed-string-(\d+)-(.*)'
result = re.match(pattern, testing)
I tested this, and the code still returns me None
. 我对此进行了测试,但代码仍然返回
None
。
Thanks you. 谢谢。
The following works: 以下作品:
> s = 'fixed-string-345-abc'
> re.match(r'fixed-string-(\d+)-(.+)') # if num and string shouldn't be empty
# re.match(r'fixed-string-(\d*)-(.*)')
> m.group(1, 2)
('345', 'abc')
This works, too: 这也有效:
import re
s = 'fixed-string-123-456'
result = re.findall('(?<=fixed-string-)(\d+)-(.*)', s)
if result:
print (result[0])
#('123', '456')
You are using re.match, which tries to match the pattern at the beginning (ie from the first character) of your string. 您正在使用re.match,它尝试在字符串的开头(即从第一个字符开始)匹配模式。 Here, "initial-string/" prevents it from matching.
在此,“ initial-string /”阻止其匹配。
You can either include "initial-string/" in your pattern, or use re.search which will match starting at any position in your string. 您可以在模式中包括“ initial-string /”,也可以使用re.search,该匹配将从字符串中的任何位置开始。
Note that it's also better to use raw strings (r'my string with \\backslahes') to avoid the potential need for escaping in your pattern. 请注意,最好使用原始字符串(r'my字符串加上\\ backslahes'),以避免可能需要在模式中进行转义。
string = 'initial-string/fixed-string-124-jeff-thompson'
result = re.search(r'fixed-string-([0-9]*)-(.*)', str)
result.groups()
# ('124', 'jeff-thompson')
or 要么
result = re.match(r'initial-string/fixed-string-([0-9]*)-(.*)', str)
result.groups()
# ('124', 'jeff-thompson')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.