python正则表达式语法(js正则表达式语法大全)
大家好,今天小编来为大家解答python正则表达式语法这个问题,js正则表达式语法大全很多人还不知道,现在让我们一起来看看吧!
Python正则表达式的几种匹配用法
下面列出: 1.测试正则表达式是否匹配字符串的全部或部分regex=ur""#正则表达式
if re.search(regex, subject): do_something()else: do_anotherthing() 2.测试正则表达式是否匹配整个字符串 regex=ur"/Z"#正则表达式末尾以/Z结束
if re.match(regex, subject): do_something()else: do_anotherthing() 3.创建一个匹配对象,然后通过该对象获得匹配细节(Create an object with details about how the regex matches(part of) a string) regex=ur""#正则表达式
match= re.search(regex, subject)if match:# match start: match.start()# match end(exclusive): atch.end()# matched text: match.group() do_something()else: do_anotherthing() 4.获取正则表达式所匹配的子串(Get the part of a string matched by the regex) regex=ur""#正则表达式
match= re.search(regex, subject)if match: result= match.group()else: result="" 5.获取捕获组所匹配的子串(Get the part of a string matched by a capturing group) regex=ur""#正则表达式
match= re.search(regex, subject)if match: result= match.group(1)else: result="" 6.获取有名组所匹配的子串(Get the part of a string matched by a named group) regex=ur""#正则表达式
match= re.search(regex, subject)if match:result= match.group"groupname")else:result="" 7.将字符串中所有匹配的子串放入数组中(Get an array of all regex matches in a string) result= re.findall(regex, subject) 8.遍历所有匹配的子串(Iterate over all matches in a string) for match in re.finditer(r"<(.*?)/s*.*?//1>", subject)# match start: match.start()# match end(exclusive): atch.end()# matched text: match.group() 9.通过正则表达式字符串创建一个正则表达式对象(Create an object to use the same regex for many operations) reobj= re.compile(regex) 10.用法1的正则表达式对象版本(use regex object for if/else branch whether(part of) a string can be matched) reobj= re.compile(regex)if reobj.search(subject): do_something()else: do_anotherthing() 11.用法2的正则表达式对象版本(use regex object for if/else branch whether a string can be matched entirely) reobj= re.compile(r"/Z")#正则表达式末尾以/Z结束
if reobj.match(subject): do_something()else: do_anotherthing() 12.创建一个正则表达式对象,然后通过该对象获得匹配细节(Create an object with details about how the regex object matches(part of) a string) reobj= re.compile(regex) match= reobj.search(subject)if match:# match start: match.start()# match end(exclusive): atch.end()# matched text: match.group() do_something()else: do_anotherthing() 13.用正则表达式对象获取匹配子串(Use regex object to get the part of a string matched by the regex) reobj= re.compile(regex) match= reobj.search(subject)if match: result= match.group()else: result="" 14.用正则表达式对象获取捕获组所匹配的子串(Use regex object to get the part of a string matched by a capturing group) reobj= re.compile(regex) match= reobj.search(subject)if match: result= match.group(1)else: result="" 15.用正则表达式对象获取有名组所匹配的子串(Use regex object to get the part of a string matched by a named group) reobj= re.compile(regex) match= reobj.search(subject)if match: result= match.group("groupname")else: result="" 16.用正则表达式对象获取所有匹配子串并放入数组(Use regex object to get an array of all regex matches in a string) reobj= re.compile(regex) result= reobj.findall(subject) 17.通过正则表达式对象遍历所有匹配子串(Use regex object to iterate over all matches in a string) reobj= re.compile(regex)for match in reobj.finditer(subject):# match start: match.start()# match end(exclusive): match.end()# matched text: match.group()字符串替换 1.替换所有匹配的子串#用newstring替换subject中所有与正则表达式regex匹配的子串
result= re.sub(regex, newstring, subject) 2.替换所有匹配的子串(使用正则表达式对象) reobj= re.compile(regex) result= reobj.sub(newstring, subject)字符串拆分 1.字符串拆分 result= re.split(regex, subject) 2.字符串拆分(使用正则表示式对象) reobj= re.compile(regex) result= reobj.split(subject)
Python其实很简单 第十一章 正则表达式
正则表达式是一个特殊的字符序列,它用来检查一个字符串是否与某种模式匹配。正则表达式在编译程序中至关重要,但并不是每个人都需要特别深入的学习和掌握。在此,只介绍一些最基本的应用。
1、元字符
元字符是构成正则表达式的一些特殊字符。在正则表达式中,元字符被赋予了新的含义。
下面介绍一些常用的元字符及其含义:
.匹配除换行符以外的任意字符。
w匹配字母、数字、下划线或汉字。
W匹配w所匹配的字符以外的字符。
s匹配单个空白符(包括Tab键和换行符)。
S匹配除s匹配的字符以外的字符。
d匹配数字。
b匹配单词的分界符,如:空格、标点符号或换行符。
^匹配字符串的开始
$匹配字符串的结束
2、限定符
限定符是在正则表达式中用来指定数量的字符。常用的限定符有:
?匹配前面的字符0或1次。如:zo?m可以匹配zom和zm,但不能匹配 zoom
+匹配前面的字符1或n次。如:zo?m可以匹配zom和zoom,但不能匹配zm
*匹配前面的字符0或n次。如:zo?m可以匹配zom、zoom和zm
{n}匹配前面的字符n次。如:zo{2}m可以匹配zoom,但不能匹配zom和zm
{n,}匹配前面的字符至少n次。如:zo{1,}m可以匹配zom和zoom,但不能匹配zm
{n,m}匹配前面的字符至少n次,最多m次。如:zo{1,2}m可以匹配zom和zoom,但不能匹配zm
3、方括号”[ ]”的用途
方括号“[ ]”里可以列出某个字符范围。如:[aeiou]表示匹配任意一个元音字母,[zqsl]表示匹配姓氏“赵钱孙李”的拼音第一个字母。
4、排除字符
方括号”[ ]”中的“^”字符表示排除的意思,如:[^aeiou]表示匹配任意一个非元音字母的字符。
5、选择字符
字符“|”相当于“或”。如:(^d{3}[-]d{8})|(^d{4}[-]d{7})$可以匹配形如”-”或“-”的电话号码格式。
6、转义字符
对于已经用于定义元字符和限定符的字符,需要加转义符“”来表示。
如:为了匹配形如“192.168.0.1”的IPv4地址(1~255.0~255.0~255.0~255),可以用这样的正则表达式:^(25[0-5]|2[0-4][0-9]|[0,1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]).(25[0-5]|2[0-4][0-9]|[0,1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0).(25[0-5]|2[0-4][0-9]|[0,1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0).(25[0-5]|2[0-4][0-9]|[0,1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])$
这里解释一下第一段IP地址的规则,取值范围为1~255,可分解为以下情况:
250~255:25[0-5];
200~249:2[0-4][0-9];
100~199:[01]{1}[0-9]{2};
0~99: [0-9]{1}[1-9]
再加上”.”:.
其他三段地址和第一段相似。
7、“()”可以用于分组
在正则表达式中,用“()”括起来的部分是一个整体。
8、r(或R)的意义
在正则表达式中,为了保证模式字符串为原生字符串(没有经过加工处理的字符串),可以在模式字符串前加上一个字符‘r’或‘R’。例如:
#这里用到对的re.match()方法接下来介绍
>>> import re#导入re模块
>>> re.match('bPy[a-z]+','Python')#表达式'bPy[a-z]+'不能匹配’Python’
>>> re.match('bPy[a-z]+','Python')#表达式'bPy[a-z]+'可以匹配’Python’
在上述代码中,原本要用作匹配单词开始或结束的元字符’b’在表达式中字符串中会被视为转义一个字符‘b’,为了转义’b’就不得不再加一个’’符号。
也可以采用下面的方法:
>>> re.match(r'bPy[a-z]+','Python')#加字符’r’,可以保证原生字符串
9、match()方法
Match()方法尝试从字符串的起始位置匹配一个模式,如果不是起始位置匹配成功的话,match()就返回 none。
语法格式:
re.match(pattern, string, [flags])
其中,pattern表示匹配的正则表达式;string是要匹配的字符串;flags表示标志位,用于控制正则表达式的匹配方式,如:re.I表示不区分大小写。
例:
import re#导入re模块
print(re.match('www','www.python.org/').span())#span()函数可以获取匹配的位置
print(re.match('org','www.python.org'))
输出结果为:
(0, 3)#在位置0到3(不包括3)匹配成功
None#从起始位置未能匹配成功
10、search()方法
search()方法用于在整个字符串中搜索第一个匹配的值,如果匹配成功,则返回Match对象,否则返回None。
语法格式:
re.search(pattern, string, [flags])
其中,pattern表示匹配的正则表达式;string是要匹配的字符串;flags表示标志位,用于控制正则表达式的匹配方式,如:re.I表示不区分大小写。
例如:
>>> re.search(r'Pyw+','It's easy to use Python, but it's not easy to learn Python.')
可以看出,目标字符串“It's easy to use Python, but it's not easy to learn Python.”中一共有两个‘Python’,search()方法可以从字符串的起始位置开始查找到‘Python’,当找到第一个匹配值后就停止查找,返回位置信息。
match()和search()的比较
match()要求目标字符串的起始位置就能匹配,search()对目标字符串全段进行逐次匹配,只要首次匹配成功就停止匹配。
请看下例:
>>> import re
>>> print(re.match(r'Pyw+','It's easy to use Python, but it's not easy to learn Python.'))
输出结果:None
11、findall()方法
findall()方法用于在整个字符串中搜索所有匹配的值,如果匹配成功,则返回以匹配值为元素的列表,否则返回空列表。
语法格式:
re.findall(pattern, string[, flags])
其中,pattern表示匹配的正则表达式;string是要匹配的字符串;flags表示标志位,用于控制正则表达式的匹配方式,如:re.I表示不区分大小写。
例:
>>> import re
>>>print(re.findall(r'Pyw+','It's easy to use Python, but it's not easy to learn Python.'))
输出结果:['Python','Python']
可以看出,findall()的结果没有指出匹配的具体位置。
12、正则表达式的应用
字符串替换
这里要用到sub()方法。它的语法格式如下:
re.sub(pattern, repl, string [,count] [,flgs])
其中,pattern是模式字符串;repl是用于替换的字符串;string是原字符串;可选参数count为模式匹配后替换的最大次数,省缺表示替换所有的匹配;可选参数flags的意义与前面的方法的该参数一致。
例:
>>> import re
>>> str1='x=36.567 y=123.234'
>>> str2=re.sub('.d+','',str1)#用空格代替小数点及其后的数字
>>> print(str2)
输出结果:x=36 y=123
分隔字符串
这里要用到split()方法。它的返回值为一个列表,它的语法格式如下:
re.split(pattern, string [,maxsplit] [,flgs])
其中,pattern是模式字符串;string是原字符串;可选参数maxsplit为最大拆分次数,省缺表示拆分所有的匹配;可选参数flags的意义与前面的方法的该参数一致。
例:
>>> import re
>>> str='白日依山尽,黄河入海流。欲穷千里目,更上一层楼!'
>>> re.split(r',|。|!',str)#按照“,”、“。”、“!”分隔字符串。
['白日依山尽','黄河入海流','欲穷千里目','更上一层楼','']
注意,返回值列表中多出了一个空字符。
Python中的正则表达式
因为正则表达式中有两组小括号,即两个分组
findall会以元组形式返回所有分组中的内容,即[('127.0.0.1','.1')]
其中'127.0.01'表示匹配最外层大括号的内容
'.1'表示匹配'\.[0-9]{1,3}'的内容(最后一次重复时为.1)
由于('\.[0-9]{1,3}')为需要重复三次的分组,该括号不能省略
而使用findall就一定会显示括号分组的内容
若想只显示127.0.0.1而不显示'.1',可考虑使用match方法
返回从字符串起始位置开始,第一次匹配正则表达式的内容
match返回的结果为re.Match对象,可通过group()显示匹配的字符串,即127.0.0.1
通过groups()显示匹配的所有分组,即('127.0.0.1','.1')
如图所示:
好了,本文到此结束,如果可以帮助到大家,还望关注本站哦!