正则表达式(Regular Expression):描述了一种字符串匹配的模式(patton),包括普通字符(例如,a和z之间的字母)和特殊字符(称为元字符)。
作用:
检查一个字符串是否含有某种子字符串
将匹配的子字符串替换
从某个子字符串中取出符合某个条件的子字符串
特点:繁琐却强大
⛏匹配网址的正则字符串:'https?😕/(?:[-\w.]|(?:%[\da-fA-F]{2}) )+'
'''一、正则表达式:''' ''' 定义:用来检索或者替换某个规则的文本 ''' import re def Find(string): # findall url = re.findall( 'https?://(?:[-\w.]|(?:%[\da-fA-F]{2}) )+' , string) return url string = 'Runnoob __的网址是:https://www.runoob.com,Google 的网址是:https://www.google.com' print("URLS:",Find(string)) '''正则表达式:是一个特殊的字符序列,它能帮助你方便的检查一个字符串是否与某种模式匹配''' #场景一、怎样从一个字符串中找到 数字 并输出 str = 'A1abcY123,AdefH&#____^!' patt1 = '[0-9]' # 匹配 数字 以 , 隔开 print(re.findall(patt1,str)) patt2 = '[a-z]' # 匹配 任何小写字母 print(re.findall(patt2,str)) patt3 = '[A-Z]' # 匹配 任何大写字母 print('part3 = ',re.findall(patt3,str)) patt4 = '[A-Z0-9]' # 匹配 任何 大写字母和数字 print(re.findall(patt4,str)) patt5 = '[\w]' # 匹配 数字、字母、下划线 <=> [a-zA-Z0-9_] patt6 = '[a-zA-Z0-9_]' print(re.findall(patt5,str)) print(re.findall(patt6,str)) print(r'\t','\\t') # 把 '\t' 当成普通字符输出 字符串2 = '百度的网址是:https://www.baidu.com,小米的网址是:https://www.mi.com' 正则表达式规则 = 'https?://www.' print(re.findall(正则表达式规则,字符串2)) str3 = '01Aa#!' patt7 = '[a]' print('测试:',re.findall(patt7,str3))’https?😕/(?:[-\w.]|(?:%[\da-fA-F]{2}) )+'
包括所有大写字母、小写字母、数字、标点符号、其他符号。