从字符串中提取一个单词

时间:2019-01-16 14:27:17

标签: python python-3.x

我正在寻找一种使用正则表达式的方法来从无空格的句子中提取单词“ MONT”。我想提取“ WORD

之后的下一个数字

例如:

s = valoirfinalieMONT:23maning => MONT 23

s = montdj34meaing  => mont 34

s = thisisthelastmontitwillwork98help => mont 98

感谢您的帮助

2 个答案:

答案 0 :(得分:2)

尝试一下:

import re

s='valoirfinalieMONT:23maning '
print(re.findall('(mont)\D*(\d*)', s, re.IGNORECASE))

正则表达式将捕获“ mont”任意数量的非数字(\ D)字符,然后捕获任意数量的数字(\ d)

添加了忽略大小写,因此mont以及MONT和MoNt也会被捕获

答案 1 :(得分:2)

您也可以这样尝试。

  

re.I用于不区分大小写的匹配。您可以检查https://docs.python.org/3/library/re.html以获得更多详细信息。

import re

s = "valoirfinalieMONT:23maning"
s2 = "montdj34meaing"
s3 = "thisisthelastmontitwillwork98help"

m = re.match(r".*(?P<name>mont)\D+(?P<number>\d+).*", s, re.I)
print(m.group(1)) # MONT
print(m.group(2)) # 23

# Same as above (2nd way)
print(m.group('name'));
print(m.group('number'))

m2 = re.match(r".*(?P<name>mont)\D+(?P<number>\d+).*", s2, re.I)
print(m2.group(1)) # mont
print(m2.group(2)) # 34

m3 = re.match(r".*(?P<name>mont)\D+(?P<number>\d+).*", s3, re.I)
print(m3.group(1)) # mont
print(m3.group(2)) # 98

这是您在评论中提到的问题的解决方案。

>>> import re
>>>
>>> s = 'valoir13-10-2012finalie13/10/2012MONT:23,00maning';
>>> m = re.match(r".*(\d{2}-\d{2}-\d{4}).*(\d{2}/\d{2}/\d{4}).*(MONT).*(\d{2},\
d{2})", s, re.I)
>>> m
<_sre.SRE_Match object; span=(0, 43), match='valoir13-10-2012finalie13/10/2012M
ONT:23,00'>
>>>
>>> m.group(0)
'valoir13-10-2012finalie13/10/2012MONT:23,00'
>>>
>>> d = m.group(1)
>>> d
'13-10-2012'
>>> arr = d.split("-")
>>> arr
['13', '10', '2012']
>>>
>>> '-'.join(arr[:2] + [arr[2][-2:]])
'13-10-12'
>>>
>>> ans1 = '-'.join(arr[:2] + [arr[2][-2:]])
>>> ans1
'13-10-12'
>>>
>>> ans2 = m.group(2)
>>> ans2
'13/10/2012'
>>>
>>> ans3 = m.group(3)
>>> ans3
'MONT'
>>>
>>> ans4 = m.group(4)
>>> ans4
'23,00'
>>>
>>> output = ' '.join([ans1, ans2, ans3, ans4])
>>> output
'13-10-12 13/10/2012 MONT 23,00'
>>>