Regex split string by last occurrence of pattern

让人想犯罪 __ 提交于 2021-01-27 07:38:48

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!