What is the regular expression for /* several lines comments */

Find, replace, find in files, replace in files, regular expressions

What is the regular expression for /* several lines comments */

Postby air_bob » Thu Mar 04, 2010 8:26 am

For example:
/* comment1
comment2
comment3 */


I want delete the comments (including /*and*/)
One way is using macro. How to do this using just replace command?
How to represent it in regular expression ?
I tried ^(/*^)^(*^)^(*/^) or ^(\/\*^)^(*^)^(\*\/^) or ^(^/^*^)^(*^)^(^*^/^), all cannot work.
I also tried in VIM: %s/\(\/\*\)\(*\)\(\*\/\)//, also doesn't work, I know * limits us to only one line search,
so any solution to describe the contents that have several lines?

Thanks a lot
air_bob
Newbie
 
Posts: 2
Joined: Tue Mar 02, 2010 9:49 pm

Re: What is the regular expression for /* several lines comments */

Postby pietzcker » Thu Mar 04, 2010 8:51 am

As a Perl regex, when nested comments are not allowed (/* This /* comment is */ nested */):

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

If you want to allow one level of nesting:

(?s)/\*(?:(?!\*/|/\*).)*+(?:/\*(?:(?!\*/|/\*).)*+\*/(?:(?!\*/|/\*).)*+)*+.*?\*/

If you want to allow two levels of nesting, you'd better give up.

And no, I didn't come up with the second regex here, it's from RegexBuddy's library.
User avatar
pietzcker
Master
Master
 
Posts: 241
Joined: Sun Aug 22, 2004 11:00 pm

Re: What is the regular expression for /* several lines comments */

Postby Bracket » Thu Mar 04, 2010 2:36 pm

Note that UltraEdit does *not* support RegEx fully. UltraEdit cripples RegEx by applying it on a line by line basis, whereas RegEx is supposed to apply to the entire document for true multi-line support.

I love UltraEdit, but when I need more advanced RegEx support, I use JGSoft's EditPad Pro. I wish UltraEdit would support RegEx fully, but despite numerous complaints and requests, it has still never happened. So, until they do, I'm stuck using two text editors.
User avatar
Bracket
Basic User
Basic User
 
Posts: 35
Joined: Fri Oct 26, 2007 11:00 pm

Re: What is the regular expression for /* several lines comments */

Postby pietzcker » Thu Mar 04, 2010 3:04 pm

That is true; I could have written the same thing, word for word. The regex should still work in UE, though :)
User avatar
pietzcker
Master
Master
 
Posts: 241
Joined: Sun Aug 22, 2004 11:00 pm

Re: What is the regular expression for /* several lines comments */

Postby air_bob » Thu Mar 04, 2010 8:20 pm

Great, thanks to both of you :P
air_bob
Newbie
 
Posts: 2
Joined: Tue Mar 02, 2010 9:49 pm

Re: What is the regular expression for /* several lines comments */

Postby spaceBAR » Tue Jun 22, 2010 10:38 pm

This perl style regular expression will match a comment block from the first "/" in "/*" to the last "/" in "*/":
Code: Select all
((?:\/\*(?:[^*]|(?:\*+[^*\/]))*\*+\/)|(?:\/\/.*))

I am using UEStudio v10 and it finds/highlights comment blocks when this perl regex is used, it even handles the case where there are multiple '*' after the first "/" and/or multiple '*' before the last "/".

hth
User avatar
spaceBAR
Basic User
Basic User
 
Posts: 18
Joined: Tue Oct 19, 2004 11:00 pm


Return to Find/Replace/Regular Expressions