String 在python中循环遍历字符串的每个元素并处理通配符大小写
我想知道String 在python中循环遍历字符串的每个元素并处理通配符大小写,string,python-2.7,for-loop,substring,wildcard,String,Python 2.7,For Loop,Substring,Wildcard,我想知道string1是否是string2的子字符串。 e、 g.string1=“abc”,string2=“afcabcdfg” 我想添加通配符,例如,“*”可以替换“a”和“c”,“y”可以替换“f”或“d”。因此,“*by”应该是“afcabcdfg”的子字符串 一般的编码方式是什么?如何循环?对于您提供的示例,请尝试使用字典定义所有替换,然后循环字符串的字符,如下所示: string2="afcabcdfg" table = {'a': '*', 'c': '*', 'f': 'y',
string1
是否是string2
的子字符串。
e、 g.string1=“abc”
,string2=“afcabcdfg”
我想添加通配符,例如,“*”
可以替换“a”
和“c”
,“y”
可以替换“f”
或“d”
。因此,“*by”
应该是“afcabcdfg”
的子字符串
一般的编码方式是什么?如何循环?对于您提供的示例,请尝试使用字典定义所有替换,然后循环字符串的字符,如下所示:
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
使用re和一些字符串操作使自己成为正则表达式
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"
顺便说一句,你应该用你的问题来添加更多或更新的信息,而不是打开一个窗口。