Communities for your favorite technologies. Explore all Collectives
Stack Overflow for Teams is now called Stack Internal. Bring the best of human thought and AI automation together at your work.
Bring the best of human thought and AI automation together at your work. Learn more
Find centralized, trusted content and collaborate around the technologies you use most.
Stack Internal
Knowledge at work
Bring the best of human thought and AI automation together at your work.
Why is my regular expression adding am line break before the closing tag?
string = '\s ప్రపంచ ప్రారంభం' re.sub(r'\\s (.*)', r'<h3>\1</h3>', string)
what I get
<h3>ప్రపంచ ప్రారంభం </h3>
what I want
<h3>ప్రపంచ ప్రారంభం</h3>
regex
string = '<h3>%s</h3>'%string[3:] if string.startswith(r'\s ') else '<h3>%s</h3>'%string
string
Take a look at the text part of your string, make sure there's nothing there creating a line break. When I tried:
s = '\s hi world' re.sub(r'\\s (.*)', r'<h3>\1</h3>', s)
I got:
'<h3>hi world</h3>'
I was using Python 2.7.3.
Add a comment
Required, but never shown
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.
Explore related questions
See similar questions with these tags.
regexfor this?string = '<h3>%s</h3>'%string[3:] if string.startswith(r'\s ') else '<h3>%s</h3>'%stringseems like it would work ...stringif possible.