How to search for the last occurrence of a regular expression in a string in python?

后端 未结 3 2009
情深已故
情深已故 2021-01-02 03:52

In python, I can easily search for the first occurrence of a regex within a string like this:

import re
re.search(\"pattern\", \"target_text\")
相关标签:
3条回答
  • 2021-01-02 04:26

    One approach is to prefix the regex with (?s:.*) and force the engine to try matching at the furthest position and gradually backing off:

    re.search("(?s:.*)pattern", "target_text")
    

    Do note that the result of this method may differ from re.findall("pattern", "target_text")[-1], since the findall method searches for non-overlapping matches, and not all substrings which can be matched are included in the result.

    For example, executing the regex a.a on abaca, findall would return aba as the only match and select it as the last match, while the code above will return aca as the match.


    Yet another alternative is to use regex package, which supports REVERSE matching mode.

    The result would be more or less the same as the method with (?s:.*) in re package as described above. However, since I haven't tried the package myself, it's not clear how backreference works in REVERSE mode - the pattern might require modification in such cases.

    0 讨论(0)
  • 2021-01-02 04:40
    import re
    re.search("pattern(?!.*pattern)", "target_text")
    

    or

    import re
    re.findall("pattern", "target_text")[-1]
    

    You can use these 2 approaches.

    If you want positions use

    x="abc abc abc"
    print [(i.start(),i.end(),i.group()) for i in re.finditer(r"abc",x)][-1]
    
    0 讨论(0)
  • 2021-01-02 04:49

    One approach is to use split. For example if you wanted to get the last group after ':' in this sample string:

    mystr = 'dafdsaf:ewrewre:cvdsfad:ewrerae'
    ':'.join(mystr.split(':')[-1:])
    
    0 讨论(0)
提交回复
热议问题