1

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>
4
  • 2
    Interesting. I don't get that. I get what you want. Commented Aug 20, 2012 at 20:44
  • I changed my OP. Maybe it has something to do with the fact the language is rtl? Commented Aug 20, 2012 at 20:45
  • It works just fine for me ... but do you really need regex for this? string = '<h3>%s</h3>'%string[3:] if string.startswith(r'\s ') else '<h3>%s</h3>'%string seems like it would work ... Commented Aug 20, 2012 at 20:50
  • Just as an added point it's not a good idea to overwrite inbuilt classes... avoid using string if possible. Commented Aug 20, 2012 at 21:07

1 Answer 1

1

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.

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

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.