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

Re-write regex expression number filtering Python

Given a string like ‘hello 0796XXXXXX. TODAY IS UR LUCKY DAY£500 Cash’, I use the following regex

re.findall(r"(\b07\d*|\b08\d*|\b09\d*)", t) to receive the numbers that start with 07 | 08 | 09 and are followed by 0 or more digits. ['0796'] is the result.

How do I have to re-write the code so that \b and \d* are not repeated? I tried re.findall(r"\b(07|08|09)\d*)", t) e.g., but unfortunately it does not work and only returns [07].

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

Thanks

>Solution :

Avoid the parentheses, and put the 0 aside that is also repeated:

re.findall(r"\b0[789]\d*", t)
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