Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: OptiPerls Pattern Replace (Regexpses for one Line)

by tphyahoo (Vicar)
on Mar 02, 2005 at 09:44 UTC ( [id://435742]=note: print w/replies, xml ) Need Help??


in reply to OptiPerls Pattern Replace (Regexpses for one Line)

Never heard of OptiPerl.

If you read your code in line by line, which is the normal way in perl, the special variable $_ matches the current line automatically, so you don't need a regex to match it.

Or, I guess,

^.*$

will also match an entire line if you have to use a regex. Or

blah.*$

will match everything from the first blah out to the end of the line.

Or maybe you could rephrase the question if that isn't quite what you wanted.

use warnings; use strict; while (<DATA>) { print '$_: ' . "$_"; /^.*$/; print '/^.*$/: ' . "$&\n"; # print the match /blah.*$/; print '/blah.*$/: ' . "$&\n"; print "\n"; } #outputs: #$_: blah #/^.*$/: blah #/blah.*$/: blah # #$_: foo blah #/^.*$/: foo blah #/blah.*$/: blah # #$_: foo bar blah blah blah #/^.*$/: foo bar blah blah blah #/blah.*$/: blah blah blah # #$_: #/^.*$/: #/blah.*$/: __DATA__ blah foo blah foo bar blah blah blah

Replies are listed 'Best First'.
Re^2: OptiPerls Pattern Replace (Regexpses for one Line)
by Ben Win Lue (Friar) on Mar 02, 2005 at 10:06 UTC
    Thanks for your fast answer. My Problem is not using the RegExp IN the code but ON the code. (Editing / Generating code in the editor). I know the $ meta-character. But somehow i cound not make it work correctly.

    Here is an example, I had something like:
     my $foobar = substr($line,20,30);  # this is :;- a comment
    which I changed to

    #***** # this is :;- a comment #***** sub foobar($){ return substr($_[0],20,30); }
    for a couple of hundreds of variables.
    for matching the comment at the end of the line, I did something like:
     (#[a-zA-Z0-9 -_;:()]+) I think there must be a more elegant way to do this.
      This help?
      use warnings; use strict; while (<DATA>) { #everything before the # /^[^#]*/; print 'everything before the #: ' . "$&\n"; #everything from the # to the end of the line /#.*/; print '/#.*/: ' . "$&\n"; print "\n"; } #outputs: #everything before the #: blah #/#.*/: #this is a comment # #everything before the #: foo blah #/#.*/: #this is a comment # #everything before the #: foo bar blah blah blah #/#.*/: #this is a comment # #everything before the #: # #/#.*/: # __DATA__ blah #this is a comment foo blah #this is a comment foo bar blah blah blah #this is a comment

Log In?
Username:
Password:

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

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

    No recent polls found