如何在字符串中间抓取数字?(蟒蛇)

Tak*_*kun 8 python regex

random string
this is 34 the string 3 that, i need 234
random string
random string
random string
random string

random string
this is 1 the string 34 that, i need 22
random string
random string
random string
random string

random string
this is 35 the string 55 that, i need 12
random string
random string
random string
random string
Run Code Online (Sandbox Code Playgroud)

在一个字符串中有多行.其中一条线重复,但每次都有不同的数字.我想知道如何将数字存储在这些行中.数字将始终位于行中的相同位置,但可以是任意数量的数字.

编辑:随机字符串中也可以包含数字.

Rom*_*huk 7

使用正则表达式:

>>> import re
>>> comp_re = re.compile('this is (\d+) the string (\d+) that, i need (\d+)')
>>> s = """random string
this is 34 the string 3 that, i need 234
random string
random string
random string
random string

random string
this is 1 the string 34 that, i need 22
random string
random string
random string
random string

random string
this is 35 the string 55 that, i need 12
random string
random string
random string
random string
"""
>>> comp_re.findall(s)
[('34', '3', '234'), ('1', '34', '22'), ('35', '55', '12')]
Run Code Online (Sandbox Code Playgroud)