2

Is there a way to add some text after pattern in python string?

For example I can do it easily with sed:

sed 's/my [a-z]\+ pattern/& lost_text/'

where & means the whole matched pattern.

search and man pages didn't help me :(

1 Answer 1

3

You're looking for re.sub:

Example:

>>> import re
>>> re.sub(r'(\d+)',r'\1--','abcd1cf123vf')
'abcd1--cf123--vf'
Sign up to request clarification or add additional context in comments.

2 Comments

Oh, that was so easy. I've searched in incorrect direction. Thank you. :)
It's impossible to accept the answer immediately due to restrictions in SO engine. So I did it only now.

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.