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

Compare for a specific string containing a substring of ranging numbers

How may I compare for a specific string containing a substring of ranging numbers in python?

Example: I have the following strings "t (1)", "t (2)" and "t (3)". They’re all "t (*)" where * is always a number. In my usecase, it will always be "t " followed by a bracketed number.

I’m not sure how to essentially do:

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

if (string == "t (*)"):

where * is the range of numbers.

I googled variations of string comparison methods in python, but I don’t know what’s the right search term to use. I assume it involves regex.

>Solution :

Probably the easiest way to do this is using regex.

import re

s = "t (99)"
match = re.search(r't \(\d+\)', s)
if match:
  # found the string
else:
  # did not find the string

See demo on regex101.com.

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