Reputation: 51
I have the following data:
policy output MRSUU_1:6-828
How can i use regex to search my data which will always be in this format:
policy output MRSUU_(digit)(colon)(digit)(hyphen)828
I want to be able to search my data as above but get me anything after hyphen.. so my search would always look for the string policy output MRSUU_
and then a digit, colon, digit, hyphen and then return me whatever is after...
output answer wanted : 828
Upvotes: 0
Views: 36
Reputation: 42411
Sometimes a couple of really simple tools (prefix check and split) will work just as well as one complex tool (regex). Which approach is better will depend on the nature of your input data and how robust your code needs to be, but my hard-earned parsing advice is prefer simpler techniques until you are certain that they won't work.
lines = [
'foo',
'policy output MRSUU_1:6-828',
'bar',
]
PREFIX = 'policy output MRSUU_'
for line in lines:
if line.startswith(PREFIX):
n = line.split('-')[-1]
print(n)
Upvotes: 1
Reputation: 12503
This should work for you:
import re
text = "policy output MRSUU_1:6-828"
res = re.search(r"MRSUU_\d+:\d-(\d+)", text)
res.groups()
output:
('828',)
Upvotes: 2