Regex获取数据后|



这是我的日志

2022-06-29 12:45:04.652 | INFO     | product.views.product_view:list:28 - SEARCH | Humana papa 
2022-06-29 12:45:04.652 | INFO     | product.views.product_view:list:28 - SEARCH | iPhone 12

我想得到Search |之后的所有东西人类爸爸|iphone 12

我使用正则表达式,我尝试这个代码,但它只得到Humana和iphoner' search | (w+).*'

import re
from collections import Counter
inp = """2022-06-29 12:45:04.652 | INFO     | product.views.product_view:list:28 - SEARCH | Humana
2022-06-29 12:45:04.652 | INFO     | product.views.product_view:list:28 - SEARCH | Car
2022-06-29 12:45:04.652 | INFO     | product.views.product_view:list:28 - SEARCH | Phone 12 pro
2022-06-29 12:45:04.652 | INFO     | product.views.product_view:list:28 - SEARCH | Humana papa """
terms = re.findall(r'SEARCH | (w+).*', inp)
print(Counter(terms))  # Counter({'Humana': 2, 'Car': 1, 'Phone': 1})
print(Counter(terms).most_common(1)[0])  # ('Humana', 2)

获取完整单词的最好方法是什么?

可以使用

terms = re.findall(r'SEARCHs*|s*(S.*)', inp.strip())

注意inp.strip(),其中inp字符串从初始/末尾空格中剥离。

正则表达式匹配

  • SEARCH- aSEARCH
  • s*|s*-包含0个或多个空格的|字符
  • (S.*)-第1组:非空白,然后是其余的行。
  • 输出:

>>> terms
['Humana', 'Car', 'Phone 12 pro', 'Humana papa']

参见regex演示。

最新更新