0

I want to find if string1 is substring of string2. e.g. string1="abc", string2="afcabcdfg".

I want to add Wildcard cases, e.g. "*" can substitute "a" and "c", "y" can substitute "f" or "d". As a result, "*by" should be substring of "afcabcdfg".

What is general way to code it? How should I loop it?

1

2 Answers 2

0

For the example provided by you try using a dictionary to define all the replacements then loop over the characters of the string like this:

string2="afcabcdfg"
table = {'a': '*', 'c': '*', 'f': 'y', 'd': 'y'}
new_string = ''

for c in string2:
    if c in table and table[c] not in new_string: new_string += table[c]
    elif c not in table: new_string += c
Sign up to request clarification or add additional context in comments.

Comments

0

Use re, and a little string manipulation to make yourself a regex.

import re
string1 = 'abc'
string2 = 'zbcdef'
wildcards = 'a'

# . is wildcard in a regex.
my_regex = string1.replace(wildcards, '.')

# If there is a match, re returns an object. We don't care
# about what info the object holds, just that it returns.
if re.match(my_regex, string2):
    print "Success"

# If there's no match, None is returned.
if not re.match(my_regex, string3):
    print "Also success" 

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.