473,785 Members | 2,458 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 7403
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
1799
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.... /** * * Project: MyProject *
1
4182
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 regular expressions easier to create and use (and in my experience as a regular expression user, it makes them MUCH easier to create and use.) I'm still working on formal documentation, and in any case, such documentation isn't necessarily the...
4
3230
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 over each document, find out if it contains a header and/or a footer and extract only the main content part. The headers and the footers have no specific format and I have to detect and remove them using a list of strings that may appear as...
3
1413
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 ... other lines of code ...
9
3358
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 an app call The Regulator, which makes it pretty easy to build and test regular expressions. As a warning, I'm real weak with regular expressions. Let's say my regular expression is:
2
1933
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
3112
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 a hand? import regsub
1
1595
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. I'm confused about how regular expressions work in different systems. I suspect that each system may have some things that do not work in other systems. So my question is: Do the things in the table below work on VS2008? And what is Posix and...
14
4992
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 text</aClosingTag> I need a Regex that will get all of the text between the html tags above (the html tags are random and i do not know them before hand). The match string always starts with at least 5 digits.
0
9645
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9480
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10147
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10091
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9950
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8972
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7499
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6739
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4050
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.