5

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.

3 Answers 3

14

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']
Sign up to request clarification or add additional context in comments.

1 Comment

I think this is better than my effort because the delimiter can be anywhere in the string, e.g. delimiter "string is" works as you would expect.
3

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

Comments

0

So...A little hacky but you can do this in two steps

 1. Find and replace all matches with (the match plus some custom character sequence or "\n").

 2. Split the new string by the custom sequence.

I did mine like this:

delimiter = "\n"   # or some custom pattern that won't occur in the string 

def break_line(match):
   return delimiter + match.group()


lines = re.sub(regex_pattern, break_line, text_you_want_to_split)
lines = re.split(delimiter, lines)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.