Python正则表达式查找一个大写字母后跟小写字母的序列
当需要使用正则表达式查找大写字母后跟小写字母的序列时,定义了一个名为“match_string”的方法,该方法使用“搜索”方法来匹配正则表达式。在方法之外,定义了字符串,并通过传递字符串在其上调用方法。
示例
下面是相同的演示
import re
def match_string(my_string):
pattern = '[A-Z]+[a-z]+$'
if re.search(pattern, my_string):
return('The string meets the required condition \n')
else:
return('The string doesnot meet the required condition \n')
print("字符串是:")
string_1 = "Python"
print(string_1)
print(match_string(string_1))
print("字符串是:")
string_2 = "python"
print(string_2)
print(match_string(string_2))
print("字符串是:")
string_3 = "PythonInterpreter"
print(string_3)
print(match_string(string_3))输出结果字符串是: Python The string meets the required condition 字符串是: python The string doesn’t meet the required condition 字符串是: PythonInterpreter The string meets the required condition
解释
导入所需的包。
定义了一个名为“match_string”的方法,它将字符串作为参数。
它使用“搜索”方法来检查是否在字符串中找到了特定的正则表达式。
在方法之外,定义了一个字符串,并显示在控制台上。
通过将此字符串作为参数传递来调用该方法。
输出显示在控制台上。