regex Python正则表达式提取sentece [duplicate]中的单词(以特定的非重复字母结尾)

kse8i1jr  于 2023-01-27  发布在  Python
关注(0)|答案(2)|浏览(121)
    • 此问题在此处已有答案**:

How to match a whole word with a regular expression?(4个答案)
昨天关门了。
我要提取"xxm"部分的字符串。
我尝试了以下:

ss = ['The stick is 36mm wide 20m long white', 
'Another is 55mm wide 10m long black', 
'Last one the length is 360m']

for s in ss:
    found = re.findall(r' [0-9]+m', s)
    print (found)

所需结果分别为"20m"和"10m",但其输出:

[' 36m', ' 20m']
[' 55m', ' 10m']

我尝试将其更改为以下,但这不是解决方案。

r' [0-9]+m$'

如何提取以1 "m"(而不是"mm")结尾的部分?谢谢。

093gszye

093gszye1#

下面是一个可能的解决方案(使用\b作为字边界):

found = re.findall(r'\b[0-9]+m\b', s)

输出:

['20m']
['10m']
['360m']
gfttwv5a

gfttwv5a2#

您可以使用单词边界字符\b:

ss = ['The stick is 36mm wide 20m long white', 
'Another is 55mm wide 10m long black', 
'Last one the length is 360m']

for s in ss:
    found = re.findall(r"\b[0-9]+m\b",s)
    print(found)

输出:

# ['20m']
# ['10m']
# ['360m']

如果您只想包含两位数(因此在本例中不包含360m),您可以使用{min,max}设置允许的重复次数。

for s in ss:
    found = re.findall(r"\b[0-9]{1,2}m\b",s)
    print(found)

输出:

# ['20m']
# ['10m']

相关问题