Regex split string by last occurrence of pattern

2024/9/8 9:01:08

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.

Answer

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

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

See it on repl.it

https://en.xdnf.cn/q/72615.html

Related Q&A

How to write a unit-test where each test case has different input but does the same?

I need to create a unit-test for some python class. I have a database of inputs and expected results which should be generated by the UUT for those inputs.Here is the pseudo-code of what I want to do:f…

Unable to pass authentication information to NetSuites REST interface using Python

Ive been trying to get a NetSuite Restlet to work with Python 3.3 using urllib, but I cant seem to get the authorization to take and continually return a urllib.error.HTTPError: HTTP Error 401: Authori…

letsencrypt failed with ImportError: No module named interface

Im using Amazon linux, and I followed some steps for using letsencrypt that easily found in google search, but all it fails with:Error: couldnt get currently installed version for /root/.local/share/le…

ModuleNotFoundError: No module named skimage.util.montage

Im trying to import montage2d module from scikit-image:from skimage.util.montage import montage2dBut this error popped up:ModuleNotFoundError: No module named skimage.util.montageIm pretty sure I insta…

upgrading python module within code

My question relates to this question: Installing python module within code, but involves upgrading the module.Ive triedpackages=[apscheduler,beautifulsoup4,gdata]def upgrade(packages):for package in pa…

Django - 403 Forbidden CSRF verification failed

I have a contact form in Django for my website and when I was testing it locally it was working fine but now when I try to submit my contact form "live" it always comes up with 403 Forbidden …

Python model object validation

Im writing an interface to be used by two applications. This interface should use some DoSomethingRequest and DoSomethingResponse classes to do the communication.Is there any library that does some mod…

Cassandra 1.2 inserting/updating a blob column type using Python and the cql library

IntroI have a blob column on a Cassandra 1.2 column family, the table is defined as follows:CREATE TABLE objects (id text,obj blob,PRIMARY KEY (id) );The problem:The problem is that when I…

Using python with subprocess Popen

I am struggling to use subprocesses with python. Here is my task:Start an api via the command line (this should be no different than running any argument on the command line) Verify my API has come …

ipdb, multiple threads and autoreloading programs causing ProgrammingError

I am using ipdb debugger to debug multithreaded web applications locally (Django, Plone). Often ipdb seems to get confused because of the autoreload which happens when I am on the debug prompt. The res…