![](/img/trans.png)
[英]Regular expression in python that check if the string just contains letters, numbers and dot(.)
[英]python regular expression to check if string contains substring
我想创建代码,如果字符串包含单词“ post”,它将返回true。 因此,当我输入代码字符串时,例如“ postgre”,“ postgres”,“ sqlpost”,它将返回true。 我该怎么做?
您不需要正则表达式。 只需使用in
运算符:
>>> word = 'post'
>>> word in 'postgres'
True
>>> word in 'sqlpost'
True
>>> word in 'fish'
False
如果您希望以比if ' post ' in mystring
更好的方式匹配一个单词,则可以使用正则表达式单词边界 ,这将强制您的模式仅匹配单词本身,而不匹配单词中的子字符串。 例如,
>>> re.search(r"\bpost\b", "my post")
<_sre.SRE_Match object at 0x7fcb95165b28>
>>> re.search(r"\bpost\b", "my postgres")
>>>
匹配post
但不匹配postgres
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.