curriculum/challenges/english/blocks/daily-coding-challenges-python/68cae5b538ff798bbd4da008.md
Given a string of a valid HTML element, return the attributes of the element using the following criteria:
attribute="value".["attribute1, value1", "attribute2, value2"].extract_attributes('<span class="red"></span>') should return ["class, red"].
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(extract_attributes('<span class="red"></span>'), ["class, red"])`)
}})
extract_attributes('<meta charset="UTF-8" />') should return ["charset, UTF-8"].
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(extract_attributes('<meta charset="UTF-8" />'), ["charset, UTF-8"])`)
}})
extract_attributes("<p>Lorem ipsum dolor sit amet</p>") should return [].
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(extract_attributes("<p>Lorem ipsum dolor sit amet</p>"), [])`)
}})
extract_attributes('<input name="email" type="email" required="true" />') should return ["name, email", "type, email", "required, true"].
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(extract_attributes('<input name="email" type="email" required="true" />'), ["name, email", "type, email", "required, true"])`)
}})
extract_attributes('<button id="submit" class="btn btn-primary">Submit</button>') should return ["id, submit", "class, btn btn-primary"].
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(extract_attributes('<button id="submit" class="btn btn-primary">Submit</button>'), ["id, submit", "class, btn btn-primary"])`)
}})
def extract_attributes(element):
return element
import re
def extract_attributes(element):
pattern = r'([\w-]+)="([^"]*)"'
matches = re.findall(pattern, element)
return [f"{attr}, {val}" for attr, val in matches]