简体   繁体   English

如何以重复模式匹配正则表达式?

[英]How to match regex in repetitive pattern?

I'm having a text of this format:我有这种格式的文本:

a=b, // c=d  f=0x83 t= "some_string"

and would like to go over it using regex pattern so each time i take the left-operand and the right operand of the "=" ie i want to extract first "a" and "b" then "c" and "d" etc...并想使用正则表达式模式对其进行 go,所以每次我取“=”的左操作数和右操作数,即我想先提取“a”和“b”,然后提取“c”和“d”等...

I tried this regex:我试过这个正则表达式:

".+=.+"

But the right operand give the rest of the sentence instead of only "b".但是右操作数给出了句子的 rest 而不是只有“b”。

If your data does not contain the empty string values and the characters from these - ",", " ", "/", then you can do this:如果您的数据不包含空字符串值和这些字符 - ","、" "、"/",那么您可以这样做:

import re

s = 'a=b, // c=d  f=0x83 t= "some_string"'
ss = re.split(r'[= ,/]', s)
print(ss)
sss = [x for x in ss if x != '']
print(sss)
['a', 'b', '', '', '', '', 'c', 'd', '', 'f', '0x83', 't', '', '"some_string"']
['a', 'b', 'c', 'd', 'f', '0x83', 't', '"some_string"']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM