简体   繁体   English

Python正则表达式提取包含数字和字母的子字符串

[英]Python regex extracting substrings containing numbers and letters

I am attempting to extract a substring that contains numbers and letters: 我试图提取包含数字和字母的子字符串:

string = "LINE     : 11m56.95s CPU    13m31.14s TODAY"

I only want 11m56.95s and 13m31.14s 我只想要11分56.95秒和13分31.14秒

I have tried doing this: 我尝试这样做:

re.findall('\d+', string)

that doesn't give me what I want, I also tried this: 那没有给我我想要的东西,我也尝试了这个:

re.findall('\d{2}[m]+\d[.]+\d|\+)

that did not work either, any other suggestions? 那也不起作用,还有其他建议吗?

Your current regular expression does not match what you expect it to. 您当前的正则表达式与您期望的不符。

You could use the following regular expression to extract those substrings. 您可以使用以下正则表达式提取这些子字符串。

re.findall(r'\d+m\d+\.\d+s', string)

Live Demo 现场演示

Example : 范例

>>> import re
>>> s = 'LINE     : 11m56.95s CPU    13m31.14s TODAY'
>>> for x in re.findall(r'\d+m\d+\.\d+s', s):
...     print x

11m56.95s
13m31.14s

Try this: 尝试这个:

re.findall("[0-9]{2}[m][0-9]{2}\.[0-9]{2}[s]", string)

Output: 输出:

['11m56.95s', '13m31.14s']

Your Regex pattern is not formed correctly. 您的Regex模式格式不正确。 It is currently matching: 当前匹配:

\d{2}  # Two digits
[m]+   # One or more m characters
\d     # A digit
[.]+   # One or more . characters
\d|\+  # A digit or +

Instead, you should use: 相反,您应该使用:

>>> import re
>>> string = "LINE     : 11m56.95s CPU    13m31.14s TODAY"
>>> re.findall('\d+m\d+\.\d+s', string)
['11m56.95s', '13m31.14s']
>>>

Below is an explanation of what the new pattern matches: 以下是新模式匹配的说明:

\d+  # One or more digits
m    # m
\d+  # One or more digits
\.   # .
\d+  # One or more digits
s    # s
\b   #word boundary
\d+  #starts with digit
.*?   #anything (non-greedy so its the smallest possible match)
s    #ends with s
\b   #word boundary

If your lines are all like your example split will work: 如果您的行都像您的示例拆分将工作:

s = "LINE     : 11m56.95s CPU    13m31.14s TODAY"

spl = s.split()

a,b = spl[2],spl[4]
print(a,b)
('11m56.95s', '13m31.14s')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM