Python Regex extract variables

I try to extract 3 variables from a string with Python3 regex.

The String:

document.getElementById('dlbutton').href = "/d/05UJmMTa/" + (213059 % 51245 + 213059 % 913) + "/Cool%20Customer%20-%20In%20Your%20Face%20%28Original%20Mix%29.mp3";

I would like extract:

  • /d/05UJmMTa/
  • 213059 % 51245 + 213059 % 913
  • /Cool%20Customer%20-%20In%20Your%20Face%20%28Original%20Mix%29.mp3

>Solution :

Using re.findall we can try:

inp = "document.getElementById('dlbutton').href = \"/d/05UJmMTa/\" + (213059 % 51245 + 213059 % 913) + \"/Cool%20Customer%20-%20In%20Your%20Face%20%28Original%20Mix%29.mp3\";"
parts = re.findall(r'\.href\s*=\s*"(.*?)"\s*\+\s*\((.*?)\)\s*\+\s*"(.*?)"', inp)
print(parts[0])

This prints:

['/d/05UJmMTa/',
 '213059 % 51245 + 213059 % 913',
 '/Cool%20Customer%20-%20In%20Your%20Face%20%28Original%20Mix%29.mp3']

Leave a Reply