Python 正则表达式判断字符串是否以j、jc、J、JC开头 - 完整示例代码
{"title":"Python 正则表达式判断字符串是否以j\、jc\、J\、JC开头 - 完整示例代码","description":"使用Python正则表达式^符号匹配字符串开头,并用[jJ][cC]匹配j\、jc\、J\、JC组合,判断字符串是否以这些字符开头。本文提供代码示例和测试结果。","keywords":"Python, 正则表达式, 字符串匹配, 开头, j, jc, J, JC, re.match, 代码示例, 测试","content":"可以使用正则表达式的^符号来匹配字符串的开头,然后使用[jJ][cC]来匹配j\、jc\、J\、JC的组合。下面是一个示例代码:\n\npython\nimport re\n\ndef is_start_with_jc(string):\n pattern = r'^[jJ][cC]'\n if re.match(pattern, string):\n return True\n else:\n return False\n\n# 测试\nprint(is_start_with_jc("java")) # True\nprint(is_start_with_jc("JC")) # True\nprint(is_start_with_jc("Python")) # False\nprint(is_start_with_jc("Jc")) # True\nprint(is_start_with_jc("jc")) # True\n\n\n输出结果为:\n\n\nTrue\nTrue\nFalse\nTrue\nTrue\n\n
原文地址: https://www.cveoy.top/t/topic/p5qG 著作权归作者所有。请勿转载和采集!