问题
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.
回答1:
You could use findall with a greedy .* before the by:
re.findall(r'(.*)\s+by\s+(.*)', text, re.I)
See it on repl.it
回答2:
You can reconstruct from the split:
parts = re.split(r'\bby\b', text, 0, re.I)
book, author = 'by'.join(parts[:-1]), parts[-1]
Or do a complete match:
match = re.match(r'(.*)\bby\b(.*)', text, re.I)
回答3:
You can try this, it will match the last by, tested on your example
by(?!.*by.*)
回答4:
You can use this single regex:
re.search('((.*( by )?.*) by (.*))',text).group(2,4)
来源:https://stackoverflow.com/questions/42733954/regex-split-string-by-last-occurrence-of-pattern