This topic created in 1975 days ago, the information mentioned may be changed or developed.
我想要匹配"[ )"或者 "( ]"中的内容,于是我写了这样一个正则:
str = '[123)(abc]'
m = re.findall('\[(.*?)\)|\((.*?)]',str)
我想要得到[’123‘ , ’abc‘]这样一个数组结果
但结果是[('123', ' '), (' ', 'abc')]
8 replies • 2021-01-01 15:15:51 +08:00
 |
|
1
ClericPy Jan 1, 2021
import re
string = '[123)(abc]'
m = re.findall(r'[\[()](.*?)[)\]]', string) print(m) # ['123', 'abc']
这样吗?
|
 |
|
2
ClericPy Jan 1, 2021
上面发错, 被自动补全了括号
# -*- coding: utf-8 -*-
import re
string = '[123)(abc]'
m = re.findall(r'[\[(](.*?)[)\]]', string) print(m)
|
 |
|
3
ClericPy Jan 1, 2021
睡晕了... 上面这俩回复都不对... 会有误判
|
 |
|
4
crclz Jan 1, 2021
('123', '') 表示 123 在第一个 group(括号)内被匹配。 ('', 'abc') 表示 abc 在第二个 group(括号)内被匹配。
|
 |
|
5
crclz Jan 1, 2021
import re
def single(l): assert len(l) == 1 return l[0]
s = '[123)(abc]'
# m = re.findall() m = re.findall('\[(.*?)\)|\((.*?)]',s) m = [single([q for q in p if len(q)>0]) for p in m] print(m)
|
 |
|
6
ClericPy Jan 1, 2021
# -*- coding: utf-8 -*-
import re
string = '[123)(abc]'
m = re.findall(r'(?<=\[).*?(?=\))|(?<=\().*?(?=\])', string) print(m) # ['123', 'abc']
|