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

Python substring in string when parts of the substring is not consequential

Say this is my string: http://www.test.com/1/page/
And this is my sub_string: www.test.com/2/page/

Now if I run

sub_string in string

The result will obviously be False because 2 is not in the string. However, I don’t care about that part. The only parts I care about are www.test.com and page. I want only these parts to be checked, and don’t care about what’s in between them. How can I perform a search in that way, so the results would be True?

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

>Solution :

You can use regex.

import regex as re
test_string="www.test.com/2/page/"
len(re.findall(".*www\.test\.com\/[0-9]\/page\/",test_string))>0

Output:
True

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