00问答网
所有问题
python re 中findall如何返回和pattern匹配的字符串而不是括号中内容,由于用到后向引用,无法避免括号
比如用(.)..\1 匹配 abbaa,期望得到abba而不是a
举报该问题
推荐答案 2017-07-12
先再套一层括号,返回后,再只取第一项
print [i[0] for i in re.findall(r'((.)..\2)', 'abbaacddccc')]好像有点笨,只能帮到这了。。。
温馨提示:答案为网友推荐,仅供参考
当前网址:
http://00.wendadaohang.com/zd/jBDZnrjeBTBIrrZjeZ.html
其他回答
第1个回答 2017-07-12
可能要在各个分组里找一下
相似回答
Python
标准库模块 -
re
答:
[2-9] [1-3]:能够做组合匹配 { }:用于标记前面
的字符
出现的频率,有如下情况:{n,m}:代表前面字符最少出现n次,最多出现m次 {n,}:代表前面字符最少出现n次,最多不受限制 {,m}:代表前面字符最多出现n次,最少不受 {n}:前面的字符必须出现n次 ...
python
匹配字符串
输出问题?
答:
print(re.
findall
(r"a(.+?)b",str)[0])这三种方法都可以输出'123'
,而不
带中括号。
python
字符串匹配
答:
pattern
= re.compile(r\d+)方法五:re.
findall
在
字符串中
找到正则表达式所匹配的所有子串,并返回一个列表,如果没有找到
匹配的,
则返回空列表。import re line=this hdr-biz model args= server patt=rserver pattern = re.compile(patt)result = pattern.findall(line)方法六:re.finditer 和...
python 中括号匹配
答:
python的
正则中用()会进行
匹配,
所以返回结果是['',''],就是两个()中
的匹配
。要想达到原来的匹配效果,就是把4匹配出来,有两种解决方法:1.最外层加个大
括号,
变成:m = re.
findall
('(([0-9])*4([0-9])*)', '[4]')
,返回
结果的第一个元素就是匹配结果了。2.去除()的匹配...
大家正在搜
python import re
enumerate python
python 正则findall
python findall
python chardet
callable python
python encoding
python append
compile python
相关问题
用python 中的findall如何提取括号中字符串的内容
findall函数带了括号,如何将匹配到的整个结果(包含括号...
python如何匹配返回括号中的字符串
Python正则表达式问题,为什么图中这样写会返回字符串中间...
python 正则表达式如何截取字符串中间的内容
python 正则表达式findall匹配问题
python的re正则 findall 怎么知道有没有截取到...
python re.findall返回结果怎样转换成字符串