Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex split string by last occurrence of pattern

Tags:

python

regex

I am using regex to split a string <book name> by <author name> into book and author names.

re.split(r'\bby\b', text, 0, re.I)

But problem arises when the book name contains the word "by" (e.g. Death by Blackhole by Tyson => ['Death', 'by Black...'])

How do I split the string by the last occurrence of the search pattern?

I have a hunch -/+ve look-ahead/behind could be useful here, but currently splitting hairs trying to construct the proper syntax.

like image 573
masroore Avatar asked Mar 11 '17 10:03

masroore


People also ask

How do you split a string by the occurrences of a regex pattern?

split() method split the string by the occurrences of the regex pattern, returning a list containing the resulting substrings.

How do you split the last character of a string?

To split a string on the last occurrence of a substring:, use the lastIndexOf() method to get the last index of the substring and call the slice() method on the string to get the portions before and after the substring you want to split on.

How do you split a string with regular expressions in Python?

If you want to split a string that matches a regular expression (regex) instead of perfect match, use the split() of the re module. In re. split() , specify the regex pattern in the first parameter and the target character string in the second parameter.


1 Answers

You could use findall with a greedy .* before the by:

re.findall(r'(.*)\s+by\s+(.*)', text, re.I)

See it on repl.it

like image 57
trincot Avatar answered Oct 09 '22 02:10

trincot