0

I'm wanting to scan through a css file and capture both comments and the css. I've came up with a regex that's almost there, however it's not quite perfect as it misses out properties with multiple declarations i.e.

ul.menu li a, # Won't capture this line
ul.nice-menu li a { text-decoration: none; cursor:pointer; }

Here's the regex that I'm working with:

(\/\*[^.]+\*\/\n+)?([\t]*[a-zA-Z0-9\.# -_:@]+[\t\s]*\{[^}]+\})

I've been testing this at rubular.com and here is what it currently matches, and what the array output is like.

Result 1

[0] /* Index */
/*
GENERAL

PAGE REGIONS
- Header bar region
- Navigation bar region
- Footer region           
SECTION SPECIFIC
- Homepage
- News */

[1] html { background: #ddd; }

Result 2

[0]
[1] body { background: #FFF; font-family: "Arial", "Verdana", sans-serif; color: #545454;}

I must point out that I'm still a new when it comes to regular expressions, so if anyone can help and show where I'm going wrong, it'd be much appreciated :)

BTW: I'm using PHP and preg_match_all

2
  • 1
    can you define what kind of output you want? "you want css and comments " is to global to determine what you want. specify an array of some sort Commented Oct 24, 2009 at 14:25
  • I've added what the expected output is currently like to the question, hope this help :) Commented Oct 24, 2009 at 14:46

2 Answers 2

6

CSS cannot be fully parsed with a regex (see CSS Grammar: http://www.w3.org/TR/CSS2/grammar.html). The {...} can be split over lines, for example, and your current version wouldn't handle this. If you need to do this, you should read the CSS spec and use a tool like ANTLR to generate a parser.

Here is an example from the W3C spec (http://www.w3.org/TR/CSS2/syndata.html):

@import "subs.css";
@import "print-main.css" print;
@media print {
  body { font-size: 10pt }
}
h1 {color: blue }

No normal regex is powerful enough to deal with nested {...} etc. let alone the contents of the imported stylesheets.

Sign up to request clarification or add additional context in comments.

8 Comments

remove all newlines and he'll be safe!
@Mauris then there will be a single line.
@Mauris he won't. just think of "{" inside comments, strings, ... he should definitely go with a specialized css parser.
I'm going for a simple case - no nested curly braces {...} The regex that I'm currently working with matches declarations that span over multiple lines. If someone can manage to tweak the current one to handle with the case outlined above, I'd be very grateful!
@Damian: yes, it's often possible to choose a specific subset of a language that you can write a parser for, but as soon as you get into the open world you will immediately find examples that break your tools. That's why it's important to adhere to standards and use existing tools rather that writing your own. You'll end up with a lot of work and it will still keep breaking
|
0

What language are you using?

You should probably just use a library to parse the CSS. Libraries can save you a lot of grief.

1 Comment

I'm using PHP, and preg_match_all

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.