Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: can u suggest a regex for this ?

by hv (Prior)
on Feb 03, 2023 at 15:26 UTC ( [id://11150129]=note: print w/replies, xml ) Need Help??


in reply to can u suggest a regex for this ?

Your "fugly code" example suggests that you want to allow nesting of (cat ... ^cat) blocks, and that everything in the outer block should then get a single '#' inserted; and further, that if the final block is missing its closing "^cat" tag it should comment to the end of file. If both of those behaviours are required, then I'd probably prefer to stick with your example code - it makes it pretty clear what is going on, and could easily be extended to insert extra '#' characters for nested blocks if desired.

jo37's solution is nice, but does not handle nested blocks: it will stop commenting as soon as it sees the first "^cat".

If the "comment to end of file" behaviour is not desired, you may be better off with a recursive regexp that would only match balanced blocks.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (3)
As of 2024-04-25 19:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found