python中list去掉2个括号 python list去括号_正则去除list 括号


一、基础语法

1.1 语法速查


python中list去掉2个括号 python list去括号_正则_02


1.2 最简单的正则匹配

学习正则一般是从 matchsearch 函数开始,推荐教程。

match

match(pattern, string) 函数会从字符串的头部开始搜索,如果匹配到了 pattern 则将其结果存入 group 中,匹配到了几次就存入几次,如果没有匹配到则返回空。


import re
a = "Jack love Rose"
res = re.match(r"Jack", a)
if res:
    print(res.group(0))


输出结果


Jack


search

如果想在句子中匹配某个单词应该使用 search() 函数,使用match() 函数是搜索不到的


import re
a = "Jack love coding!"
res1 = re.match(r"love", a)
res2 = re.search(r"love", a)
if res1:
    print(res1.group(0))
if res2:
    print(res2.group(0))


输出结果


love


如果匹配了多个结果,那么同样的,输出 group(2)group(3)……


import re
a = "Jack love Rose and love coding!"
res = re.search(r"Jack (.*) and (.*) coding!", a)
if res:
    print(res.group(1))
    print(res.group(2))


输出结果


love Rose
love


matchsearch 函数适合于本身已经知道了有几个需要匹配项的情况,如果需要找出所有的匹配项,应该使用 findall,后面会讲到。

compile

函数声明


re.compile(pattern[, flags])


flags 是一个可选参数,表示匹配模式,比如忽略大小写,多行模式等,具体参数为:


python中list去掉2个括号 python list去括号_字符串_03


我们来尝试一下


import re
a = "Jack love Rose and love coding!"
# 忽略大小写
pattern = re.compile(r"JACK (.*) AND (.*) coding!", re.I)
res = pattern.match(a)
if res:
    print(res.group(1))
    print(res.group(2))


输出结果


love Rose
love


findall

在字符串中找到正则表达式所匹配的所有子串,并返回一个列表,如果没有找到匹配的,则返回空列表。

注意: matchsearch 是匹配一次, findall 匹配所有。


import re

pattern = re.compile(r'd+')   # 查找数字
result1 = pattern.findall('www.hao123baidu456.com')
result2 = pattern.findall('www.hao123baidu456.com', 0, 10)  # 指定匹配范围

print(result1)
print(result2)


输出结果


['123', '456']
['123']


1.3 转义字符匹配

转义字符的匹配需要 "" 做转义。

  • 匹配括号
import re
# 匹配括号里的内容
a = "www.hao123(baidu)456.com"
# 忽略大小写
pattern = re.compile(r"((.*))")
res = pattern.findall(a)
print(res[0])


输出内容


baidu


  • 匹配$

$是一个特殊的字符,在正则中表示以某字符结尾,假设我们要匹配 $$ 中间的内容(经常在公式转换中使用),应该怎么写呢?


import re
a = "$y=x^2$"
# 匹配$$中间的内容
pattern = re.compile(r"$(.*)$")
res = pattern.findall(a)
print(res[0])


输出结果


y=x^2


其他的转义字符使用方式类似。

1.4 正则替换

Python 的re模块提供了re.sub用于替换字符串中的匹配项。函数声明如下


re.sub(pattern, repl, string, count=0, flags=0)


【例 1】将字符串中的 s 替换为 A


import re
a = "this is hust"
pattern = re.compile(r"s")
res = re.sub(pattern, "A", a)
print(res)


输出结果


thiA iA huAt


【例 2】将字符串中的 $A$ 替换为 `$A$`


import re
# replace $A$ to `$A$`
a = "设集合$a={1,2,3}$,求集合$a$的子集个数。"
b = re.sub(r'$(.+?)$', r'`$1$`', a)
print(a)
print(b)


输出结果


设集合$a={1,2,3}$,求集合$a$的子集个数。
设集合`$a={1,2,3}$`,求集合`$a$`的子集个数。


这里有个注意的点, 1 表示匹配到的内容。

【例 2】将字符串中的无效,测试替换为空字符


import re
a = "华中科技大学机械学院(无效)"
b = "华中科技大学紫菘公寓(测试)"
c = "华中科技大学无效紫菘测试公寓(西区)"
pattern = re.compile(u"(无效|测试)")
res1 = re.sub(pattern, "", a)
res2 = re.sub(pattern, "", b)
res3 = re.sub(pattern, "", c)
print(res1)
print(res2)
print(res3)


输出结果


华中科技大学机械学院()
华中科技大学紫菘公寓()
华中科技大学紫菘公寓(西区)


二、进阶用法

2.1 判断字符串是否为数字


import re
a = "-1234"
b = "-12.34"
c = ".34"
print(re.match(r'^[+-]?d+$', a))           # 匹配int 1
print(re.match(r'^[+-]?[0-9].*$', a))       # 匹配int 2
print(re.match(r'^[+-]?d*(.)d+$', b))    # 匹配float
print(re.match(r'^[+-]?d*(.)d+$', c))    # 匹配float
print(re.match(r'^[+-]?d*(.)?d+$', a))   # 匹配int 或 float


输出结果


<re.Match object; span=(0, 5), match='-1234'>
<re.Match object; span=(0, 5), match='-1234'>
<re.Match object; span=(0, 6), match='-12.34'>
<re.Match object; span=(0, 3), match='.34'>
<re.Match object; span=(0, 5), match='-1234'>


2.2 去除括号中的内容

去除括号里的内容其实就是利用 re.sub 的替换功能


import re
a = "华中科技大学附属同济医院(西区)"
pattern = re.compile(u"$(.*)$")
res = re.sub(pattern, "", a)
print(res)


输出结果


华中科技大学附属同济医院


2.3 去除脏符号

【例 1】因为平时工作中接触数据很多,经常需要对数据进行清洗,其中一个很重要的流程就是去除字符串中除了中文、英文、括号之外的所有字符,这个实现如下


import re
a = "华中科技大学机械学院 B240 (研究生工作室;电话联系)。"
pattern = re.compile(u"[^u4e00-u9fa50-9a-zA-Z()()]")
res = re.sub(pattern, "", a)
print(res)


输出结果


华中科技大学机械学院B240(研究生工作室电话联系)


【例 2】括号内不是期或者区则删掉括号内容


import re
a = "华中科技大学紫菘公寓(一期)。"
b = "华中科技大学紫菘公寓(西)。"
pattern = re.compile(u"((?!.*[区期]).*)|((?!.*[区期]).*)")
res1 = re.sub(pattern, "", a)
res2 = re.sub(pattern, "", b)
print(res1)
print(res2)


输出结果


华中科技大学紫菘公寓(一期)。
华中科技大学紫菘公寓。