What is equivalent to rsplit () with re.split ()?

rsplit() starts splitting at the end of the line. How to start splitting at the end of a line when using re.split() ?

Example:

 import re splitme = "a!b?c!d" re.split(r"[!\?]", splitme, maxsplit = 1) 

Return:

 a 

But I want:

 d 

While I was writing this question, I realized that I can use

 re.split(r"[!\?]", splitme)[-1] 

But this does not seem to be the most efficient way, since it splits the entire line, and we can stop after the first match (on the right).

+5
source share
1 answer

There is no need to split if you want only the latter.

 match = re.search(r'[^!?]*$', splitme) if match: return match.group(0) 
+2
source

All Articles