首页 > 解决方案 > 如何仅在一个单词 Python regex 之后获取特定数字?

问题描述

我有一个这样的字符串:

'$."result"."000182"."200"', '$."result"."000490"."200"', '$."result"."000530"."200"'

我想在之后得到一个数字结果的数组

"result"."[WANTOGETTHISNUMBER]"."200"

我试过这样的(例子)

test_str = "'$.""result"".""000109"".""200""', '$.""result"".""000110"".""200""', '$.""result"".""000111"".""200""', '$.""result"".""000112"".""200""'"

x = re.findall('[0-9]+', test_str)

print(x)
#['000109', '200', '000110', '200', '000111', '200', '000112', '200']

但我希望输出为:['000109', '000110', '000111', '000112']

实现这一目标的正确方法是什么?

标签: pythonpython-3.xregex

解决方案


您可以使用此正则表达式:

>>> re.findall('result\.([0-9]+)', test_str)
['000109', '000110', '000111', '000112']

推荐阅读