Python字符串判断:如何检查是否不包含"ab"子字符串
使用正则表达式来判断一个字符串是否不包含"ab"。以下是一个示例代码:
import re
def is_contain_ab(s):
pattern = r'ab'
result = re.search(pattern, s)
if result:
return False
else:
return True
# 测试示例
print(is_contain_ab("abc")) # False
print(is_contain_ab("def")) # True
以上代码中,我们使用re模块的search函数来搜索字符串中是否存在"ab",如果存在则返回False,否则返回True。
原文地址: https://www.cveoy.top/t/topic/pSnf 著作权归作者所有。请勿转载和采集!