I have this string :
"ALPHABET, LETTER_A=a;0, LETTER_B=b;1, LETTER_C=c;2"
I need a pattern to get each match to be the data.
I have already found some patterns.
"a"
as match: r"(?<=LETTER_A=)([^;]+)"
"b"
as match: r"(?<=LETTER_B=)([^;]+)"
"c"
as match: r"(?<=LETTER_C=)([^;]+)"
But for 1
,2
or3
, I can't find a pattern whose match just returns 1
,2
or3
. The logic would be to find first "LETTER_B="
, then find the first ";"
after that and collect everything between that and a ","
or the end of the string.
I tried this: r"(?<=(?<=LETTER_A=)\w;)(.*?)(?=,|$)"
and the match is 1
which is what I want but if the string is "ALPHABET, LETTER_A=aa;0, LETTER_B=b;1, LETTER_C=c;2"
[The changement is only adding a char to a
], it does not work anymore.
By match, I mean what I get when I do match.group()
, not match.group(1)
.
Use findall
and use .*?
so your matches aren't greedy.
Grabbing c;2
a little more difficult since it doesn't have a comma, so we optionally look for end of string $
there.
s = "ALPHABET, LETTER_A=aa;0, LETTER_B=b;1, LETTER_C=c;2"
patt = "LETTER_[A-Z]{1}=.*?;(.*?)(?:,|$)"
re.findall(patt, s)