How do i remove the last occurrence of a character from a string in python?

If I have a string as follows:

foo_bar_one_two_three

Is there a clean way, with RegEx, to return: foo_bar_one_two?

I know I can use split, pop and join for this, but I'm looking for a cleaner solution.

Tot Zam

7,9779 gold badges49 silver badges72 bronze badges

asked Sep 11, 2013 at 0:51

2

result = my_string.rsplit['_', 1][0]

Which behaves like this:

>>> my_string = 'foo_bar_one_two_three'
>>> print[my_string.rsplit['_', 1][0]]
foo_bar_one_two

See in the documentation entry for str.rsplit[[sep[, maxsplit]]].

answered Sep 11, 2013 at 0:54

TadeckTadeck

128k28 gold badges149 silver badges197 bronze badges

One way is to use rfind to get the index of the last _ character and then slice the string to extract the characters up to that point:

>>> s = "foo_bar_one_two_three"
>>> idx = s.rfind["_"]
>>> if idx >= 0:
...     s = s[:idx]
...
>>> print s
foo_bar_one_two

You need to check that the rfind call returns something greater than -1 before using it to get the substring otherwise it'll strip off the last character.

If you must use regular expressions [and I tend to prefer non-regex solutions for simple cases like this], you can do it thus:

>>> import re
>>> s = "foo_bar_one_two_three"
>>> re.sub['_[^_]*$','',s]
'foo_bar_one_two'

answered Sep 11, 2013 at 0:56

paxdiablopaxdiablo

826k227 gold badges1548 silver badges1915 bronze badges

Similar the the rsplit solution, rpartition will also work:

result = my_string.rpartition["_"][0]

You'll need to watch out for the case where the separator character is not found. In that case the original string will be in index 2, not 0.

doc string:

rpartition[...]

S.rpartition[sep] -> [head, sep, tail]

Search for the separator sep in S, starting at the end of S, and return the part before it, the separator itself, and the part after it. If the separator is not found, return two empty strings and S.

answered Sep 11, 2013 at 1:09

nakedfanaticnakedfanatic

3,0082 gold badges27 silver badges33 bronze badges

Here is a generic function to remove everything after the last occurrence of any specified string. For extra credit, it also supports removing everything after the nth last occurrence.

def removeEverythingAfterLast [needle, haystack, n=1]:
    while n > 0:
        idx = haystack.rfind[needle]
        if idx >= 0:
            haystack = haystack[:idx]
            n -= 1
        else:
            break
    return haystack

In your case, to remove everything after the last '_', you would simply call it like this:

updatedString = removeEverythingAfterLast['_', yourString]

If you wanted to remove everything after the 2nd last '_', you would call it like this:

updatedString = removeEverythingAfterLast['_', yourString, 2]

answered Dec 26, 2020 at 6:09

roublerouble

14.8k16 gold badges101 silver badges97 bronze badges

I know is python, and my answer may be a little bit wrong in syntax, but in java you would do:

String a = "foo_bar_one_two_three";
String[] b = a.split["_"];
String c = "";
for[int i=0; i

Chủ Đề