Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Split a text by specific word or phrase and keep the word in Python

Is there any elegant way of splitting a text by a word and keep the word as well. Although there are some works around split with re package and pattern like (Python RE library String Split but keep the delimiters/separators as part of the next string), but none of them works for this scenario when the delimiter is repeated multiple times. For example:

 s = "I want to split text here, and also keep here, and return all as list items"

Using partition:

 s.partition("here")
>> ('I want to split text ', 'here', ', and also keep here, and return all as list items')

Using re.split():

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

re.split("here",s)
>> ['I want to split text ', ', and also keep ', ', and return all as list items']

The desired output should be something to the following list:

['I want to split text', 'here', ' , and also keep ', 'here', ' , and return all as list items']

>Solution :

Yes. What you’re looking for is a feature of the re.split() method. If you use a capture group in the expression, it will return the matched terms as well:

import re

s = "I want to split text here, and also keep here, and return all as list items"

r = re.split('(here)', s)

print(r)

Result:

['I want to split text ', 'here', ', and also keep ', 'here', ', and return all as list items']

If you define multiple capture groups, it will return each of them individually. So you can return just a part of the delimiter, or multiple parts that each get returned. I’ve done some fairly crazy things with this feature in the past. It can replace an appreciable amount of code that would otherwise be necessary.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading