Reputation: 167
I'd like to extract the two words FIRST and SECOND from the phrase below, i've tried with this regex, to get the word before the slash but it doesn't work : / btw it's on python:
import re
data = "12341 O:EXAMPLE (FIRST:/xxxxxx) R:SECOND/xxxxx id:1234"
data2 = "12341 O:EXAMPLE:FIRST2:/xxxxxx) R:SECOND2/xxxxx id:1234"
result = re.findall(r'[/]*',data)
result2 = re.findall(r'[/]*',data2)
print result,result2
Upvotes: 1
Views: 3716
Reputation: 336158
Try
result = re.findall(r'\w+:?(?=/)',data)
Explanation:
\w+ # Match one or more alphanumeric characters
:? # Match an optional colon
(?=/) # Assert that the next character is a slash
If you don't want the colon to be part of the match (your question is unclear on this), put the optional colon into the lookahead assertion:
result = re.findall(r'\w+(?=:?/)',data)
Upvotes: 6