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.
Advertisement
Answer
JavaScript
x
2
1
result = my_string.rsplit('_', 1)[0]
2
Which behaves like this:
JavaScript
1
4
1
>>> my_string = 'foo_bar_one_two_three'
2
>>> print(my_string.rsplit('_', 1)[0])
3
foo_bar_one_two
4
See in the documentation entry for str.rsplit([sep[, maxsplit]])
.