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

How to match regular expression characters in right position, wrong order?

Say my string is bucs.
I would want to match buccaneers, tampa bay buccaneers, and bucs, but not "falcons".

I’m fairly new to regex, I tried:

re.findall("bucs", "buccaneers")

and it returned an empty list.

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

How would I go about doing this?

>Solution :

You can separate your characters by .* to match strings in which they appear with no or some other characters between them:

In [1]: strs = "buccaneers", "tampa bay buccaneers", "bucs", "falcons"

In [2]: [s for s in strs if re.findall("b.*u.*c.*s", s)]
Out[2]: ['buccaneers', 'tampa bay buccaneers', 'bucs']
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