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.
split() method split the string by the occurrences of the regex pattern, returning a list containing the resulting substrings.
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.
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.
You could use findall
with a greedy .*
before the by
:
re.findall(r'(.*)\s+by\s+(.*)', text, re.I)
See it on repl.it
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With