473,289 Members | 1,959 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,289 software developers and data experts.

Regular Expression to Match Blank Characters outside Multiline Comments

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
Nov 17 '05 #1
1 7381
I wrote another one:

(?<=(?:\*/)(?:[^/\*]|(?<!/)\*|/(?!\*))*)\s+(?=(?:[^\*/]|(?<!\*)/|\*(?!/))*(?:/\*))|(?<=\A(?:[^/\*]|(?<!/)\*|/(?!\*))*)\s+(?=(?:[^\*/]|(?<!\*)/|\*(?!/))*(?:/\*))|(?<=(?:\*/)(?:[^/\*]|(?<!/)\*|/(?!\*))*)\s+(?=(?:[^\*/]|(?<!\*)/|\*(?!/))*\z)|(?<=\A(?:[^/\*]|(?<!/)\*|/(?!\*))*)\s+(?=(?:[^\*/]|(?<!\*)/|\*(?!/))*\z)
I did some simple test, and it works:)
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


Nov 17 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Oriana | last post by:
Hi! I'm trying to 'clean up' this source file using regular expressions in Python. My problem is, that when I try to delete extra lines, my code fails. Here's an example.... /** * *...
1
by: Kenneth McDonald | last post by:
I'm working on the 0.8 release of my 'rex' module, and would appreciate feedback, suggestions, and criticism as I work towards finalizing the API and feature sets. rex is a module intended to make...
4
by: Neri | last post by:
Some document processing program I write has to deal with documents that have headers and footers that are unnecessary for the main processing part. Therefore, I'm using a regular expression to go...
3
by: Gianluca | last post by:
Hi, I'm using regular expressions to extract some information from my vb.net source code files. I have something like this: 1: '<class name="xyz" description="xxxxxx"/> 2: Class xyz ......
9
by: Pete Davis | last post by:
I'm using regular expressions to extract some data and some links from some web pages. I download the page and then I want to get a list of certain links. For building regular expressions, I use...
2
by: teo | last post by:
match word with interpunctuation Hallo, I need to build a regular expression able to match a specified word, preceded and followed by few chars (mainly interpunctuation) Below the code. ...
11
by: Steve | last post by:
Hi All, I'm having a tough time converting the following regex.compile patterns into the new re.compile format. There is also a differences in the regsub.sub() vs. re.sub() Could anyone lend...
1
by: AAaron123 | last post by:
I found this on the Internet and tried a few of them and they worked in VS2008. Actually it was in a different form but I converted to make a smaller file. The data is the same as the original. ...
14
by: Andy B | last post by:
I need to create a regular expression that will match a 5 digit number, a space and then anything up to but not including the next closing html tag. Here is an example: <startTag>55555 any...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.