Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python String Split on pattern without removing delimiter

I have a long string, and I want to break it into smaller stinger whenever a certain pattern showed up: (in below case 123 my)

my_str = '123 my string is long 123 my string is very long 123 my string is so long'

I want the result to be:

result = ['123 my string is long ', '123 my string is very long ', '123 my string is so long ']

Length of string is unknown. and I don't want to remove anything from the main string.

like image 893
Fatemeh Feyzi Avatar asked Feb 24 '16 12:02

Fatemeh Feyzi


People also ask

How do you split part of a string in Python?

Python split() method is used to split the string into chunks, and it accepts one argument called separator. A separator can be any character or a symbol. If no separators are defined, then it will split the given string and whitespace will be used by default.

Does split modify the original string?

The splitter can be a single character, another string, or a regular expression. After splitting the string into multiple substrings, the split() method puts them in an array and returns it. It doesn't make any modifications to the original string.


2 Answers

You can also use a look ahead regex:

import re
re.split(r'.(?=123 my)', my_str)
=>
['123 my string is long',
 '123 my string is very long',
 '123 my string is so long']
like image 85
dimid Avatar answered Sep 24 '22 02:09

dimid


You can split on the delimiter and then add it back in with a list comprehension:

my_str = '123 my string is long 123 my string is very long 123 my string is so long'
delimiter = '123 my'
result = ['{}{}'.format(delimiter, s) for s in my_str.split(delimiter) if s]
print(result)

Output

['123 my string is long ', '123 my string is very long ', '123 my string is so long']

I don't know where the trailing space in the last list item comes from in your desired output, it's not in the original string and so should be absent in the result.

Note that this only works if the delimiter begins at the start of the string

like image 40
mhawke Avatar answered Sep 24 '22 02:09

mhawke