python 正则表达式判断字符串是否以j、jc、J、JC开头
可以使用正则表达式的^符号来匹配字符串的开头,然后使用[jJ][cC]来匹配j、jc、J、JC的组合。下面是一个示例代码:
import re
def is_start_with_jc(string):
pattern = r'^[jJ][cC]'
if re.match(pattern, string):
return True
else:
return False
# 测试
print(is_start_with_jc("java")) # True
print(is_start_with_jc("JC")) # True
print(is_start_with_jc("Python")) # False
print(is_start_with_jc("Jc")) # True
print(is_start_with_jc("jc")) # True
输出结果为:
True
True
False
True
True
原文地址: https://www.cveoy.top/t/topic/ins4 著作权归作者所有。请勿转载和采集!