Rahul Sharma
Rahul Sharma

Reputation: 857

python regex match anything between string multiple conditions

I am trying to get my hands dirty on regex.

Sample String

Info:Somestring-103409115825.Call
Info: BIL*ONL*00003.Avbl

Currently This Matches

>>> print(re.search(r'Info:(.*?).Call', payload).group(1))
Somestring-103409115825

>>> print(re.search(r'Info:(.*?).Avbl', payload).group(1))
BIL*ONL*00003

how to make regex to match both conudtion like Info -- AnyString -- .Call|.Avbl ?

Upvotes: 0

Views: 216

Answers (2)

The fourth bird
The fourth bird

Reputation: 163457

You can escape the dot and place it before using an alternation to match either Call or Avbl

\bInfo:(.*?)\.(?:Call|Avbl)\b

Regex demo

import re
 
pattern = r"\bInfo:(.*?)\.(?:Call|Avbl)\b"
 
print(re.search(pattern, "Info:Somestring-103409115825.Call").group(1))
print(re.search(pattern, "Info: BIL*ONL*00003.Avbl").group(1))

Output

Somestring-103409115825
 BIL*ONL*00003

If you don't want the leading space before the group value, you can use:

\bInfo:\s*(.*?)\.(?:Call|Avbl)\b

See a Python demo

Upvotes: 2

Mr. Chip
Mr. Chip

Reputation: 126

Please try this code.

import re

payload1 = 'Info:Somestring-103409115825.Call'
payload2 = 'Info: BIL*ONL*00003.Avbl'

print(re.search(r'Info:(.*?)((.Call)|(.Avbl))', payload1).group(1))
print(re.search(r'Info:(.*?)((.Call)|(.Avbl))', payload2).group(1))

Upvotes: 1

Related Questions