[英]python regex finditer
I have question about re, I tried to look answer on re documentary but I think I am to newbie for this. 我对re有疑问,我试图在re纪录片上寻找答案,但我想我是新手。
I have string like this 我有这样的字符串
string = "id=186 s_id=0 channel_name=[cspacer0]---BlaBla--- number=2"
I want to retrive all result after '=' so I used 我想检索'='之后的所有结果,所以我用了
re.finditer("=[\w]*", string)
My result was as follow 我的结果如下
How should my pattern look to get the channel_name as well? 我的模式也应该如何获得channel_name?
The \\w
token only matches word characters, to allow metacharacters I would use \\S
(any non-white space character) instead. \\w
令牌仅与单词字符匹配,为了允许使用元字符,我将改用\\S
(任何非空白字符)。 Also, instead of finditer
you can use findall
for this task: 另外,您可以使用
findall
代替finditer
来完成此任务:
>>> import re
>>> s = 'id=186 s_id=0 channel_name=[cspacer0]---BlaBla--- number=2'
>>> re.findall(r'=(\S+)', s)
['186', '0', '[cspacer0]---BlaBla---', '2']
The orginal string looks like this, I want to get everything starting with
=
skip=ok
andidx=0
原始字符串看起来像这样,我想让所有内容以
=
skip=ok
和idx=0
开头
>>> s = 'error idx=0 msg=ok id=186 s_id=0 channel_name=[cspacer0]---BlaBla--- number=2'
>>> re.findall(r'(?<!idx)=(?!ok)(\S+)', s)
['186', '0', '[cspacer0]---BlaBla---', '2']
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.