Regular Expression matching biggest possible

P

petermichaux

Hi,

I would like to remove comments from a JavaScript file with a Ruby
script. I discovered that my regular expression matching seems to match
the biggest possible substring.

def show_regexp(a,re)
if a =~ re
"#{$`}<<#{$&}>>#{$'}"
else
"no match"
end
end
puts show_regexp("/* some comment */ some code /* some other comment
*/", /\/\*[\w\W]*\*\//)

This outputs

<</* some comment */ some code /* some other comment */>>

and I was hoping for it to find the individual comments.

<</* some comment */>> some code <</* some other comment */>>

What should my regular expression be?

Thanks,
Peter
 
P

petermichaux

Chris said:
You can make a regexp non greedy with a ?, that is

/\/\*[\w\W]*?\*\//

Thanks Chris,

Now I can easily find what i need in the manual:)

Peter
 
D

Damphyr

Hi,

I would like to remove comments from a JavaScript file with a Ruby
script. I discovered that my regular expression matching seems to match
the biggest possible substring.
Wow, I only had to wait 20 minutes before posting (or you could have
checked the regexp madness thread).
Seems today is greedy regexp day :)
V.-

--
http://www.braveworld.net/riva

____________________________________________________________________
http://www.freemail.gr - äùñåÜí õðçñåóßá çëåêôñïíéêïý ôá÷õäñïìåßïõ.
http://www.freemail.gr - free email service for the Greek-speaking.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,142
Latest member
DewittMill
Top