Tahnoon Pasha
Tahnoon Pasha

Reputation: 6018

Python regex not working as expected

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

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

Upvotes: 2

Views: 308

Answers (1)

Tim Pietzcker
Tim Pietzcker

Reputation: 336108

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')

Upvotes: 3

Related Questions