Python (re)

CSS Custom Property (Variable) in PY

Match CSS custom properties (variables) like `--brand-color` or `--font-size-lg`.

Try it in the PY tester →

Pattern

regexPY
--[a-zA-Z][a-zA-Z0-9\-]*   (flags: g)

Python (re) code

pyPython
import re

pattern = re.compile(r"--[a-zA-Z][a-zA-Z0-9\-]*")
input_text = ":root { --brand-color: #10b981; --space-4: 1rem; }"
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

-- is the literal double-dash that prefixes every CSS custom property. [a-zA-Z] requires the next character to be a letter (digits and hyphens are allowed elsewhere but not in lead position by convention). [a-zA-Z0-9\-]* allows the rest of the name: any combination of letters, digits, and hyphens.

Examples

Input

:root { --brand-color: #10b981; --space-4: 1rem; }

Matches

  • --brand-color
  • --space-4

Input

var(--text-primary, #fff)

Matches

  • --text-primary

Input

.foo { color: red; }

No match

Same pattern, other engines

← Back to CSS Custom Property (Variable) overview (all engines)