1

I hope I find you well.

I realise that a generic regular expression to fit all css won't cut it in all cases, however I am parsing a particular piece of css so I can tweak this expression to fit my needs.

Having scoured SO, I did find some very useful regular expressions which I have combined with great success into the following:

parsed_css = re.findall(r'([\[\]=\"\#\.\w\-\,\s\n\r\t:@*]+)(\{[^\{\}].*?\})', css, re.DOTALL)

This produces something like:

Selector: '.div-wrapper' rules: '{position:absolute;z-index:4;background:none!important;border:none!important}'

However, I encounter problems when coming across keyframes rules like this:

@keyframes mymove { from {top: 0px;} to {top: 200px;}}

Obviously the curlies are the problem but I'm hoping someone has some look ahead / look behind magic to produce the following in these rare instances:

Selector: '@keyframes mymove'
Rules: '{from {top: 0px;} to {top: 200px;}}'

Many thanks! Darren

1 Answers1

1

You can use pyparsing to parse such nested parentheses.

import pyparsing as pp

string = '@keyframes mymove {from {top: 0px;} to {top: 200px;}}'
pattern = pp.Regex(r'^.*?(?= \{)') + pp.original_text_for(pp.nested_expr('{', '}'))
selector, rules = pattern.parse_string(string)

# Tests
assert selector == '@keyframes mymove'
assert rules == '{from {top: 0px;} to {top: 200px;}}'

* pyparsing can be installed by pip install pyparsing

See also this post: Python: How to match nested parentheses with regex?

quasi-human
  • 1,898
  • 1
  • 2
  • 13