0

I'm trying to expand a minified CSS file (don't ask) to make it human readable. I've managed to get most of the expanding done but I'm stuck at a very weird case that I can't figure out.

I have CSS that looks like this:

.innerRight {
border:0;color:#000;width:auto;padding-top:0;margin:0;
}
a {
color:#000;text-decoration:underline;font-size:12px;
}
p,small,ul,li {
color:#000;font-size:12px;padding:0;
}

I've tried (.+):(.+); as the search and \t\1: \2;\n as the replace. The find RegEx is valid, the only problem is that it matches the entire line of attributes. I've tried the non-greedy character, but I must not be putting it in the right place.

What the above find RegEx matches is:

0: border:0;color:#000;width:auto;padding-top:0;margin:0;
1: color:#000;text-decoration:underline;font-size:12px;
2: color:#000;font-size:12px;padding:0;

While those are technically correct matches, I need it to match border:0;, color:#000;, etc separately for my replace to work.

5
  • What programming language are you doing this with? Commented Nov 24, 2010 at 13:23
  • 1
    Wouldn't it be easier just to insert a line break directly after each semicolon and ignoring the context. Or do you need a general solution which will work with a semicolon for eaxmple inside a string (which is quite unlikely in CSS)? Commented Nov 24, 2010 at 13:24
  • Replace ';' with ';\n\r' ... wich programming-language? Commented Nov 24, 2010 at 13:25
  • Or you could just use one of the many CSS formatters/beautifiers online or built into an editor: google.de/search?q=css+(beautifier+OR+formatter) Commented Nov 24, 2010 at 13:27
  • Programming language independent. I could use a formatter/beautifier, but I'm taking this opportunity to get some RegEx practice in. Commented Nov 24, 2010 at 13:42

2 Answers 2

3

Try this - use non-greedy matching. This works for me

(.+?):(.+?);
Sign up to request clarification or add additional context in comments.

Comments

0

Forget the colon. Just replace all semicolons with ";\n".

In Javascript, for example, you could write:

text = text.replace(/;/gm,";\n");

I would further refine that to address leading-space issues, etc., but this will put every style rule on its own line.

2 Comments

Do this only if there is no comment, URL, or string that contains that character. You might also want to check if there is not already a new line following the ;.
@Gumbo: True. I should post a macro I have that does all this formatting, and I will once I get in to the office.

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.