Skip to content

Instantly share code, notes, and snippets.

@orangexception
Created October 17, 2011 14:53
Show Gist options
  • Save orangexception/1292778 to your computer and use it in GitHub Desktop.
Save orangexception/1292778 to your computer and use it in GitHub Desktop.
Minify CSS Regular Expression

Regular Expression

(?s)\s|/\*.*?\*/

Usage in ColdFusion

I've tested with ColdFusion 8 and Railo 3.3.

sCSSContent= sCCSContent.ReplaceAll( "(?s)\s|/\*.*?\*/" , "" );

Test Cases

/* Test 1 */
/* Test * / * 2 */
/* Test
. test
. test

test 3
*/
/* Test * / * 4 */ width:	0; /* Test 5 *//**/
/* Test 6 /*/
@boughtonp
Copy link

You don't need to write [\s\t\r\n] because \s is shorthand for [ \t\r\n] (also includes a few other whitespace characters you don't care about).

You also don't need to escape / nor do you need to escape * inside the character class, and not sure why you've got parentheses in there? :S

Actually, I'd probably just recommend using something like the YUI compressor, since that's built for this task and has already been extensively tested and so on.

@orangexception
Copy link
Author

Ah, yes. It seems Java doesn't do negative groups. The idea behind the [^(*/)]* block is to ensure that the comment block stops at the first *\, not some secondary comment close. For example, /* * / * */ width: 0; /**/ should turn into width:0;.

Nice to know about \s, I tend to overload the characters after dealing with quirky RegEx engines. I'll update the gist.

@orangexception
Copy link
Author

You made me think about the problem differently, which led to http://stackoverflow.com/questions/2503413/regular-expression-stop-at-first-match

I changed the RegEx block in question to .*?. This seems to pass my all of my tests so far.

@boughtonp
Copy link

Hmmm, I think you're a bit confused - Java does do negative character classes ([^abc] matches anything except a or b or c), and negative lookaheads ((?!abc) - matches any position not followed by abc string) - the latter is what you were attempting to do (with mixed up syntax), and would be /\*(?:(?!\*/).)*+\*/ if you were going to do it that way, but yeah - the simpler solution in this case is a lazy quantifier.

Have you tested against multi-line comments? By default . wont match a newline, so you may need to enable "dotall" mode, which can be done by placing a (?s) at the start of the pattern.
(The s is because other regex implementations call it "single-line" mode, just to be confusing.)

@orangexception
Copy link
Author

Java does negative characters, but it doesn't do negative groups. [^(abc)] should be anything except the string "abc."

Thanks again, the multiline test was failing without (?s). I had it buried where it wasn't easily seen. I changed my tests to be more visual and added them to the gist.

@boughtonp
Copy link

Still not sure where "negative groups" is coming from - I've not heard that term, nor seen the syntax [^(abc)] except as a negative character class (any char except those five) - not in Java/Python/Perl/PCRE or any others I'm aware of. Have you got a reference for it?

@orangexception
Copy link
Author

I don't have a reference offhand. Perhaps it's something floating in my brain from formal languages. It'd be rather handy. I know this isn't the first time I've tried to use it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment