Home > Software engineering >  Python Regex extract variables
Python Regex extract variables

Time:06-22

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 Customer - In Your Face (Original Mix).mp3";

I would like extract:

  • /d/05UJmMTa/
  • 213059 % 51245 213059 % 913
  • /Cool Customer - In Your Face (Original Mix).mp3

CodePudding user response:

Using re.findall we can try:

inp = "document.getElementById('dlbutton').href = \"/d/05UJmMTa/\"   (213059 % 51245   213059 % 913)   \"/Cool Customer - In Your Face (Original Mix).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 Customer - In Your Face (Original Mix).mp3']
  • Related