Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: Stripping Comments

by varian (Chaplain)
on Jun 09, 2007 at 09:18 UTC ( [id://620171]=note: print w/replies, xml ) Need Help??


in reply to Stripping Comments

Try this:
#!/usr/bin/perl use strict;use warnings; while (my $line=<DATA>) { $line=~s/(?<!\\)#.*//; print $line; } __DATA__ \#Not a comment #Comment \#Not a comment #But this is a comment arbitrary text #Comment arbitrary text \#Not a comment arbitrary text \#Not a comment #But this is a comment arbitrary text #Comment \#This is part of the comment
Prints:
\#Not a comment \#Not a comment arbitrary text arbitrary text \#Not a comment arbitrary text \#Not a comment arbitrary text
The '(?<!' construct looks backward to ensure that the escape character is not preceeding the comment character.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (None)
    As of 2024-04-25 02:05 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      No recent polls found