Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

I want to convert multiple characters to Python regular expressions

I want to convert only numbers in this str

"ABC234TSY65234525erQ"

I tried to change only areas with numbers to the * sign

This is what I wanted

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

"ABC*TSY*erQ"

But when I actually did it, it came out like this

"ABC***TSY********erQ"

How do I change it?

Thanks you!

>Solution :

use \d+. + in a regular expression means "match the preceding character one or more times"

import re
s = re.sub(r'\d+', '*', s)

output:

'ABC*TSY*erQ'
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading