0

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?

2
  • 1
    You might also find this interesting Approximate string search in Python Commented Dec 31, 2021 at 9:18
  • 1
    Try splitting out the prefix and suffix of interest from your string using .split() or partition() and then use .startswith() and .endswith() to check for a match. Commented Dec 31, 2021 at 10:02

3 Answers 3

2

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

Sign up to request clarification or add additional context in comments.

Comments

1

You can use regex:

import re
all_locs = re.findall('www\.test\.com/[0-9]+/page/', source_string)

This will return all locations where the substring appears in. An empty list means it didn't appear

Comments

1

you can simply use:

if substring in string.lower():
    print("Exists")
else:
    print("Does not exist")

or regex:

import re
for match in re.findall('www\.test\.com/[0-9]+/page/', str):
print(match)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.