Skip to content
TheChatArchiveThe Chat Archive
Qwenqwen3-30b-a3b· Alibaba (Qwen)Hugging Face LMArena Human Preference 140K

Use regex in python to parse stylesheets and return information regarding font-faces…

From a Chatbot Arena battleThe same person sent this prompt to two anonymous models at once. Compare it with grok-3-mini-high’s answer.
  1. User

    Use regex in python to parse stylesheets and return information regarding font-faces declared

  2. Qwen Assistant

    To parse @font-face declarations from a CSS stylesheet using regular expressions in Python, we can follow a structured approach that extracts the block of each @font-face rule and then parses its internal properties.


    ✅ Goal

    Extract all @font-face blocks from a CSS string and return a list of dictionaries, each representing the properties of a font-face declaration.


    🧠 Key Considerations

    • @font-face blocks are defined by the @font-face keyword 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-face declarations:

    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 cssutils or pyquery.

    🧠 Summary

    This solution uses regular expressions to:

    1. Match and extract @font-face blocks.
    2. 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.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.