Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: Remove comments between delimiters

by swiftone (Curate)
on Sep 20, 2002 at 13:24 UTC ( #199460=note: print w/ replies, xml ) Need Help??


in reply to Remove comments between delimiters

First, do you realize that your <> regex may not work as intended if you have nested or broken pairs? For example: <This works>
<<This will break>>
<<As will this>

Second, I'm not sure I understand your question. Are you trying to remove /*This*/
Or turn it into //
Or are you just trying to get rid of *This*?

Assuming the latter, s/\*[^*]*\*//g should do what you want.

The trick is that in a matched pair (<>) you can use the presence of a starting tag (<) to stop a greedy match from eating the rest of your regexp. In an unmatched pair (*foo*) a greedy regexp will consume everything until the final matching element. So we switch to non-greedy matching with a dot star, or greedy without consuming our match (the [^*])

I hope that answers your question.

Update: Death to Dot Star! is an excellent discussion of greediness, the greedy dot-star, and why non-greediness isn't always the best solution.
Update2: Removed my non-greedy match that was in defiance of my own advice :)


Comment on Re: Remove comments between delimiters
Select or Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://199460]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (11)
As of 2015-07-03 13:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (53 votes), past polls