200字范文,内容丰富有趣,生活中的好帮手!
200字范文 > python使用正则化检查字符串的起始和结束字符相同

python使用正则化检查字符串的起始和结束字符相同

时间:2019-08-21 18:44:19

相关推荐

python使用正则化检查字符串的起始和结束字符相同

python使用正则化检查字符串的起始和结束字符相同

#python使用正则化检查字符串的起始和结束字符相同

# Python program to check if a string starts# and ends with the same character# import re module as it provides# support for regular expressionsimport re# the regular expressionregex = r'^[a-z]$|^([a-z]).*\1$'# function for checking the stringdef check(string):# pass the regular expression# and the string in the search() methodif(re.search(regex, string)):print("Valid")else:print("Invalid")if __name__ == '__main__' :sample1 = "abba"sample2 = "a"sample3 = "abc"check(sample1)check(sample2)check(sample3)

参考:Python – Check whether a string starts and ends with the same character or not (using Regular Expression)

参考:Python: Check whether a string starts with specified characters

参考:python re

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。