简体   繁体   English

正则表达式在斜杠前提取单词

[英]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: 我想从下面的短语中提取两个单词FIRST和SECOND,我已经尝试过使用此正则表达式,使其在斜杠前得到,但它不起作用:/ btw它在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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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