2020年12月24日星期四

How to split a string by a substring without white spaces, while keeping its original white spaces?

I am looking for a way to split a string with white spaces (this includes spaces, \n, \t) by a target phrase that has its white spaces removed. This should be able to be done for both before and after the target phrase. It is also necessary that I keep the original string with its white spaces.

As there can be n-th occurrence of the target phrase, I am only looking to split by the first occurrence and getting the characters before it, and split by the last occurrence and getting the characters after it.

For example:

str = 'This is a test string for my test string example only.'  target_phrase = 'teststring'  

Intended output:

('This is a', 'test string for my test string example only.) #Split by target phrase and getting characters prior to it  ('This is a test string for my test string', 'example only.') #Split by target phrase and getting characters after it  

emphasized text

Any hints gratefully received.

https://stackoverflow.com/questions/65433672/how-to-split-a-string-by-a-substring-without-white-spaces-while-keeping-its-ori December 24, 2020 at 11:39AM

没有评论:

发表评论