Search code examples
pythonpython-re

re pattern - Find an expression located after two criteria


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.

  • to get "a" as match: r"(?<=LETTER_A=)([^;]+)"
  • to get "b" as match: r"(?<=LETTER_B=)([^;]+)"
  • to get "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).


Solution

  • 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)