Use regex in python to parse stylesheets and return information regarding font-faces…
- User
Use regex in python to parse stylesheets and return information regarding font-faces declared
Assistant
To parse
@font-facedeclarations from a CSS stylesheet using regular expressions in Python, we can follow a structured approach that extracts the block of each@font-facerule and then parses its internal properties.
✅ Goal
Extract all
@font-faceblocks from a CSS string and return a list of dictionaries, each representing the properties of a font-face declaration.
🧠 Key Considerations
@font-faceblocks are defined by the@font-facekeyword followed by a block of CSS properties.- Properties inside the block are in the form:
property: value; - Property names can include letters, numbers, hyphens, and underscores.
- Values may span multiple lines and contain spaces, commas, or special characters (e.g.,
url(...)).
🧩 Implementation
Here's a Python function that uses regex to extract and parse
@font-facedeclarations:import re def parse_font_faces(css): """ Parses a CSS string to extract information from @font-face declarations. Args: css (str): A string containing CSS code. Returns: list of dict: A list of dictionaries, each representing a @font-face declaration. """ font_faces = [] # Regex to match @font-face blocks font_face_pattern = re.compile(r'@font-face\s*\{([^}]*)\}') for match in font_face_pattern.finditer(css): block = match.group(1).strip() # Regex to extract key-value pairs inside the block property_pattern = re.compile(r'([a-zA-Z0-9_-]+)\s*:\s*([^;]+);', re.DOTALL) properties = {} for prop, value in property_pattern.findall(block): properties[prop] = value.strip() font_faces.append(properties) return font_faces
📌 Example Usage
css = """ @font-face { font-family: 'MyFont'; src: url('myfont.woff2') format('woff2'), url('myfont.woff') format('woff'); font-weight: 400; font-style: normal; } @font-face { font-family: 'AnotherFont'; src: url('another.woff'); } """ font_face_info = parse_font_faces(css) for face in font_face_info: print(face)Output:
{ 'font-family': "'MyFont'", 'src': "url('myfont.woff2') format('woff2'),\n url('myfont.woff') format('woff')", 'font-weight': '400', 'font-style': 'normal' } { 'font-family': "'AnotherFont'", 'src': "url('another.woff')" }
🧪 Limitations and Notes
- This implementation assumes well-formed CSS and does not handle complex cases like nested rules or invalid syntax.
- It does not support CSS variables or advanced features.
- For more robust parsing, consider using a CSS parser like
cssutilsorpyquery.
🧠 Summary
This solution uses regular expressions to:
- Match and extract
@font-faceblocks. - Parse the internal properties using a regex that handles:
- Hyphens and underscores in property names.
- Multiline values.
- Optional whitespace around colons and semicolons.
It provides a simple and effective way to extract font-face metadata from CSS using only standard Python libraries.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.