首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >在Python中匹配全部或部分字符串

在Python中匹配全部或部分字符串
EN

Stack Overflow用户
提问于 2018-06-13 05:02:08
回答 2查看 0关注 0票数 0

如何检查如果输入字符串子字符串,后面的字符开始匹配太多,如果提供,例如,我想匹配mysmystring,换句话说,比赛mysmystmystrmystrimystrinmystring,但不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”的纯粹子字符串

EN

回答 2

Stack Overflow用户

发布于 2018-06-13 13:58:18

>>> match = re.search("^mys.+", foo)
>>> if (match): ...
票数 0
EN

Stack Overflow用户

发布于 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
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/-100005357

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档