1

I have the following sentence:

sentence = "Work \nExperience \n\n First Experience..."

Work 
Experience 

 First Experience...

So, I want to remove the "\n" between Work and Experience, but at the same time I don't want remove "\n\n" after Experience.

Work Experience 

First Experience...

I've tried different solution like:

string = re.sub(" \n{1}[^\n]"," ",sentence)

but all of them remove the first character after \n (E).

Update: I managed to find the solution thanks to @Wiktor

print(re.sub(r'\w .*?\w+', lambda x: x.group().replace('\n', ''), sentence, flags=re.S))

0

1 Answer 1

1

If you want to make it a generic solution to remove any amount of \n, a newline, in between two strings, you can use

import re
sentence = "Work \nExperience \n\n First Experience..."
print( re.sub(r'Work.*?Experience', lambda x: x.group().replace('\n', ''), sentence, flags=re.S) )

See the Python demo. Output:

Work Experience 

 First Experience...

The Work.*?Experience with re.S matches any substrings between (and including) Work and Experience and then the match data object (x) is processed upon each match when all newlines are removed usign .replace('\n', '') and these modified strings are returned as replacement patterns to re.sub.

Sign up to request clarification or add additional context in comments.

7 Comments

Thanks for the answer ! What if I had a big text in which this problem is repeated not only for this case, but also for other sentences? I've tried intuitively with: re.sub(r'\w .*?\w', lambda x: x.group().replace('\n', ''), sentence, flags=re.S) but obviousely it doesn't work well
@sergiozavota Start from the very beginning with requirements. What should you match start with? What should it end with?
The words are not "static", so you could have sentence1 = "Work \nExperience \n\n First Experience..." sentence2 = "Sergio \nZavota \n\n Second Experience..." sentence3 = "Wiktor \nStribiżew \n\n Third Experience..." etc..
@sergiozavota So what is the pattern? What is regular here? How can we know that we need to match from Wiktor to Stribiżew?
@sergiozavota Glad you found it out. Also, you could use a word boundary rather than space: r'\w\b.*?\w+'
|

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.