Reputation: 208
I have text of the type:
I need to extract everything that is after the :
or =
I have tried to go about it this way:
import regex as re
r = re.compile(r'Choice(.+?)selected')
r.split(str)
I don't know how to capture the :
or =
Upvotes: 1
Views: 97
Reputation: 18357
You can use this regex,
[:=]\s*(.*)
And get your value from group1
This regex starts by capturing either :
or =
and then optionally \s*
matches optional space and then (.*)
captures the remaining text in the line and captures in group1
Python code,
import regex as re
arr = ['Choice values selected: Option 1, or Option 2, or Option 3','Choice value selected: Option 1, or Option 2, or Option 3','Choice value selected = Option 1 , or Option 2, or Option 3']
for s in arr:
m = re.search(r'[:=]\s*(.*)', s)
if m:
print(s, '-->', m.group(1))
Output,
Choice values selected: Option 1, or Option 2, or Option 3 --> Option 1, or Option 2, or Option 3
Choice value selected: Option 1, or Option 2, or Option 3 --> Option 1, or Option 2, or Option 3
Choice value selected = Option 1 , or Option 2, or Option 3 --> Option 1 , or Option 2, or Option 3
Also, in case you want to use re.split
then you can split it using [=:]
regex which represents either =
or :
import regex as re
arr = ['Choice values selected: Option 1, or Option 2, or Option 3','Choice value selected: Option 1, or Option 2, or Option 3','Choice value selected = Option 1 , or Option 2, or Option 3']
for s in arr:
r = re.compile(r'[:=]')
print(r.split(s)[1])
Output,
Option 1, or Option 2, or Option 3
Option 1, or Option 2, or Option 3
Option 1 , or Option 2, or Option 3
Upvotes: 1
Reputation: 20500
You don't need to use regex, just use re.split
to split both on :
and =
li = ["Choice values selected: Option 1, or Option 2, or Option 3", "Choice value selected: Option 1, or Option 2, or Option 3",
"Choice value selected = Option 1 , or Option 2, or Option 3"]
import re
for item in li:
#Split on : and =, get the last element from list and strip it
print(re.split(':|=',item)[1].strip())
The output will be
Option 1, or Option 2, or Option 3
Option 1, or Option 2, or Option 3
Option 1 , or Option 2, or Option 3
Upvotes: 4