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

后端 未结 3 2028
情深已故
情深已故 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: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:])
    

提交回复
热议问题