Python正则表达式查找一个大写字母后跟小写字母的序列
当需要使用正则表达式查找一个大写字母后跟小写字母的序列时,定义了一个名为“match_string”的方法,该方法使用“search”方法来匹配正则表达式。在方法外部,定义了字符串,并通过传递字符串来调用该方法。
示例
以下是相同内容的演示
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("The string is :")
string_1 = "Python"
print(string_1)
print(match_string(string_1))
print("The string is :")
string_2 = "python"
print(string_2)
print(match_string(string_2))
print("The string is :")
string_3 = "PythonInterpreter"
print(string_3)
print(match_string(string_3))输出
The string is : Python The string meets the required condition The string is : python The string doesn’t meet the required condition The string is : PythonInterpreter The string meets the required condition
解释
导入所需的包。
定义了一个名为“match_string”的方法,该方法将字符串作为参数。
它使用“search”方法检查字符串中是否存在特定的正则表达式。
在方法外部,定义了一个字符串,并在控制台上显示。
通过传递此字符串作为参数来调用该方法。
输出显示在控制台上。
广告
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP