2

Why does the following python regex not generate @Summary\n?

import re
re.sub('$~ ','@','~ Summary\n')

1 Answer 1

3

The anchor $ means "the position at the end of the string".

You need to use a different anchor for "the position at the start of the string":

re.sub(r'^~ ','@','~ Summary\n')
Sign up to request clarification or add additional context in comments.

6 Comments

You sure you don't mean replace it with ^?
And maybe, say re.sub(r'\$~ ','@','$~ Summary\n') instead! (in order to get the expected result)
@user2357112 Looking at the input, it seems that you're right.
@devnull: And that is why I always teach my students to be skeptical of what authority figures say, including myself :)
|

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.