Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

OptiPerls Pattern Replace (Regexpses for one Line)

by Ben Win Lue (Friar)
on Mar 02, 2005 at 09:28 UTC ( #435740=perlquestion: print w/ replies, xml ) Need Help??
Ben Win Lue has asked for the wisdom of the Perl Monks concerning the following question:

Hi, Brothers and Sisters of the spirit! I'm using OptiPerl for my perl coding. There is a Search/Replace feature in the editor, that is using uses Perlish RegExpses. My problem is, that this feature is working always for the whole code, I can't make it work for single lines. Is there any hidden switch in OptiPerl that makes it behave in a more vi-like way?

If not, what is a good RegExp for matching "anything to the end of the line"?

By the way: I found by Super Search very few OptiPerl-Nodes. Is there something heretic about using OptiPerl?

Comment on OptiPerls Pattern Replace (Regexpses for one Line)
Re: OptiPerls Pattern Replace (Regexpses for one Line)
by tphyahoo (Vicar) on Mar 02, 2005 at 09:44 UTC
    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
      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
Re: OptiPerls Pattern Replace (Regexpses for one Line)
by gellyfish (Monsignor) on Mar 02, 2005 at 10:04 UTC

    OptiPerl is a commercial 'IDE' - perhaps you should contact the support desk of the vendor. If you want vi-like behaviour and you want it on windows perhaps you should consider something that actually does that like 'gvim' or 'viper' for emacs.

    /J\

Re: OptiPerls Pattern Replace (Regexpses for one Line)
by Anonymous Monk on Mar 02, 2005 at 10:22 UTC
    what is a good RegExp for matching "anything to the end of the line"

    /.*/ should do, as a dot matches anything that's not a newline. However, I'm trying to get rid of the habit, and try to learn myself to use /[^\n]*/ instead. This makes it easier to slap /s on your regex if you want dot to really match everything. And in Perl6, a dot will match everything anyway. If only Perl5 would recognize \N.

      Thanks a lot!

      The /[^\n]*/ worked!
      I always thought that the backslash would lose its metacharacter behaviour in the brackets, so I didn't get the idea.

      The /.*/ did not work since I don't have a /s switch.

Re: OptiPerls Pattern Replace (Regexpses for one Line)
by tphyahoo (Vicar) on Mar 02, 2005 at 10:37 UTC
    I am happy with jgsoft's editpad, which has awesome regex find and replace.
      ditto re Fookes' NoteTab (and even the "pro" vers is only USD 20.)

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (8)
As of 2014-12-25 00:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (159 votes), past polls