简体   繁体   中英

Regular expression to extract words before a slash

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 

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM