Python (re)

HTML Attribute in PY

Match HTML attributes of the form name="value" or name='value' and capture both parts.

Try it in the PY tester →

Pattern

regexPY
\b([a-zA-Z][a-zA-Z0-9\-]*)=["']([^"']*)["']   (flags: g)

Python (re) code

pyPython
import re

pattern = re.compile(r"\\b([a-zA-Z][a-zA-Z0-9\\-]*)=[\"']([^\"']*)[\"']")
input_text = "<a href=\"https://x.com\">"
for m in pattern.finditer(input_text):
    print(m.group(0))

Stdlib `re` module — no third-party dependency. Works on Python 3.6+.

How the pattern works

Group 1 captures the attribute name (letters, digits, hyphen). Group 2 captures the quoted value. Matches both double and single quoted forms.

Examples

Input

<a href="https://x.com">

Matches

  • href="https://x.com"

Input

<img alt='logo' src='/a.png'>

Matches

  • alt='logo'
  • src='/a.png'

Input

<div></div>

No match

Same pattern, other engines

← Back to HTML Attribute overview (all engines)