2

This is my log

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

i want to get everything after Search | for example Humana papa | iphone 12

i am using regex , i try this code but its only get Humana and iphone r'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)

what is best way to get full word ?

4
  • Just use r'SEARCH\s*\|\s*(\S.*)' Commented Jun 29, 2022 at 20:43
  • Can you explain what this mean ? \s*(\S.*
    – CaptainPy
    Commented Jun 29, 2022 at 20:45
  • See the answer. Commented Jun 29, 2022 at 20:47
  • To avoid escaping special characters you can use the (backtick) instead of " when quoting strings. For example \w+` is the same as "\\w+".
    – CaptainPy
    Commented Jun 29, 2022 at 20:56

1 Answer 1

2

You can use

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

Note the inp.strip() where the inp string is stripped from the intial/trailing whitespace.

The regex matches

  • SEARCH - a SEARCH word
  • \s*\|\s* - a | char enclosed with zero or more whitespaces
  • (\S.*) - Group 1: a non-whitespace and then the rest of the line.

Output:

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

See the regex demo.

3
  • why is this error when i try (( this code --- > r'SEARCH\s*\|\s*(\S.*)' )) in Loki grafana using regexp but there is error Make sure that all special characters are escaped with \
    – CaptainPy
    Commented Jun 29, 2022 at 20:50
  • @pataradzmaa The answer is for Python. I do not know how you use it in Loki grafana. Check this documentation. Commented Jun 29, 2022 at 20:53
  • To avoid escaping special characters you can use the (backtick) instead of " when quoting strings. For example \w+` is the same as "\\w+".
    – CaptainPy
    Commented Jun 29, 2022 at 20:56

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.