如何继续regex搜索,在一行中匹配n个捕获



我正在尝试在行中执行连续搜索

2012-15-08 07:05 *** Error importing row no. 5: The import of 785 line failed because of 345 item

我正试图在行编号匹配后捕获3个数字,导入&分别因为

我知道如何搜索第一个单词,但我无法在单个正则表达式行中继续搜索第二个单词(导入(

到目前为止,我的代码,在行给我第一个匹配,如何在单行中继续下一个单词匹配

row no. (d+):

因此,预期的输出类似于,在行编号匹配后,导入&因为

5  785  345

代码

import re
txt = '2012-15-08 07:05 *** Error importing row no. 5: The import of 785 line failed because of 345 item'
regex = re.compile(r'row no. d+|import of d{3}|because of d{3}')
mo = re.findall(regex, txt)

输出

['row no. 5', 'import of 785', 'because of 345']

Regex解释

第一个备选row no. d+

  • row no从字面上匹配第no行字符(区分大小写(
  • .与字符匹配。字面(区分大小写(
  • 与字符完全匹配(区分大小写(
  • d匹配一个数字(相当于[0-9](
  • +在一次和无限次之间匹配上一个令牌,尽可能多次,根据需要回馈(贪婪(

第二个备选import of d{3}

  • import of与从字面上导入的字符匹配(区分大小写(
  • d匹配一个数字(相当于[0-9](
  • {3}与上一个令牌正好匹配3次

第三种备选because of d{3}

  • because of匹配字符是因为字面上(区分大小写(
  • d匹配一个数字(相当于[0-9](
  • {3}与上一个令牌正好匹配3次

要只输出数字,还可以使用sub方法。

代码

import re
txt = '2012-15-08 07:05 *** Error importing row no. 5: The import of 785 line failed because of 345 item'
mo = re.sub(r'D+', ' ', ''.join((re.findall(r'row no. d+|import of d{3}|because of d{3}', txt)))).strip()
print(mo)

输出

5 785 345

我发现这个网站在使用regex时很有用,regex 101

嗯,这不是最优雅的一个,但它有效。。。

>>> s = "2012-15-08 07:05 *** Error importing row no. 5: The import of 785 line failed because of 345 item"
>>> import re
>>> re.findall(r'[0-9]+', s.split('***')[1])
['5', '785', '345']

所以,基本上,我们只需将字符串除以***,就可以从字符串的右部分得到所有整数。

最新更新