如何检查如果输入字符串子字符串,后面的字符开始匹配太多,如果提供,例如,我想匹配mys
到mystring
,换句话说,比赛mys
,myst
,mystr
,mystri
,mystrin
或mystring
,但不mysr
>>> foo="mystring"
>>> foo.startswith("mys")
True
#This is good
>>> foo = "mysr"
>>> foo.startswith("mys")
True
>>>#This is not good, its not substring of "mystring".
>>>#so I may have to write as below, there should be a better way?
>>> foo.startswith("mys") or foo.startswith("myst") or foo.startswith("mystr") or foo.startswith("mystri") or foo.startswith("mystrin") or foo.startswith("mystring")
这部分做我想要的:
>>> foo.startswith("mys") and foo in "mystring"
True
我希望ti仅匹配“mystring”的纯粹子字符串
发布于 2018-06-13 13:58:18
>>> match = re.search("^mys.+", foo)
>>> if (match): ...
发布于 2018-06-13 14:25:50
foo = 'mystring'
if 'mys' in foo:
print True
True
foo = 'mysring'
if 'mys' in foo and 'mysr' not in foo:
print True
else:
print False
False
https://stackoverflow.com/questions/-100005357
复制相似问题