Connecting Tech Pros Worldwide Help | Site Map

Regular Expression to Match Blank Characters outside Multiline Comments

Laser Lu
Guest
 
Posts: n/a
#1: Nov 17 '05
Hi, all,
I'm now writing a program to compress JavaScript code. One puzzle is how
to write a regular expression to find out and remove all the redundent blank
spaces. However, those blank spaces that are in the comments should be kept
intact.

I've tried to write some Regexs, and I list them here for your information:
regex = new Regex(@"/\*[\s\S]*?\*/"); // pattern used to match a multiline
comment block
regex = new Regex(@"//[^@\n]*\n"); // pattern used to match a single-line
comment
regex = new Regex(@"(?<!(?://[^\n]*))\s+"); // pattern used to match blank
characters outside single-line comments

Now the problem is how to match all the continuous blank characters outside
multiline comments?
regex = new Regex(@"(?<!(?:/\*[\s\S]*))\s+");
but this does not work, and will lead to a CPU dead-loop with no reaction
for a long time.

I'm a newbie in Regular Expression. Can sombody help me? Thanks a lot!

Best regards,
Laser L


Laser Lu
Guest
 
Posts: n/a
#2: Nov 17 '05

re: Regular Expression to Match Blank Characters outside Multiline Comments


I wrote another one:

(?<=(?:\*/)(?:[^/\*]|(?<!/)\*|/(?!\*))*)\s+(?=(?:[^\*/]|(?<!\*)/|\*(?!/))*(?:/\*))|(?<=\A(?:[^/\*]|(?<!/)\*|/(?!\*))*)\s+(?=(?:[^\*/]|(?<!\*)/|\*(?!/))*(?:/\*))|(?<=(?:\*/)(?:[^/\*]|(?<!/)\*|/(?!\*))*)\s+(?=(?:[^\*/]|(?<!\*)/|\*(?!/))*\z)|(?<=\A(?:[^/\*]|(?<!/)\*|/(?!\*))*)\s+(?=(?:[^\*/]|(?<!\*)/|\*(?!/))*\z)


I did some simple test, and it works:)
[color=blue]
> Hi, all,
> I'm now writing a program to compress JavaScript code. One puzzle is
> how
> to write a regular expression to find out and remove all the redundent
> blank
> spaces. However, those blank spaces that are in the comments should be
> kept
> intact.
> I've tried to write some Regexs, and I list them here for your
> information:
> regex = new Regex(@"/\*[\s\S]*?\*/"); // pattern used to match a
> multiline
> comment block
> regex = new Regex(@"//[^@\n]*\n"); // pattern used to match a
> single-line
> comment
> regex = new Regex(@"(?<!(?://[^\n]*))\s+"); // pattern used to match
> blank
> characters outside single-line comments
> Now the problem is how to match all the continuous blank characters
> outside
> multiline comments?
> regex = new Regex(@"(?<!(?:/\*[\s\S]*))\s+");
> but this does not work, and will lead to a CPU dead-loop with no
> reaction
> for a long time.
>
> I'm a newbie in Regular Expression. Can sombody help me? Thanks a lot!
>
> Best regards, Laser Lu
>[/color]



Closed Thread