Regexp help

Discussion in 'Perl Misc' started by mike, Oct 13, 2009.

  1. mike

    mike Guest

    Hi,

    I need to find if the line before 'public void aMethod()' has a

    // or */

    I have tried the following:

    if($current_line =~ /(?:\\\|\*\\)/){

    }

    br,

    //mike

    //here is some text
    public void aMethod(){

    }

    or

    /**
    * Here is some text
    */
    public void aMethod(){

    }
    mike, Oct 13, 2009
    #1
    1. Advertising

  2. mike

    Lars Eighner Guest

    In our last episode,
    <>,
    the lovely and talented mike
    broadcast on comp.lang.perl.misc:

    > Hi,


    > I need to find if the line before 'public void aMethod()' has a


    > // or */


    > I have tried the following:


    > if($current_line =~ /(?:\\\|\*\\)/){


    > }


    Why do you think a backslash, even when escaped with a backslash, will match
    a forward slash?

    > br,


    > //mike


    > //here is some text
    > public void aMethod(){


    > }


    > or


    > /**
    > * Here is some text
    > */
    > public void aMethod(){


    > }


    --
    Lars Eighner <http://larseighner.com/> September 5887, 1993
    265 days since Rick Warren prayed over Bush's third term.
    Obama: No hope, no change, more of the same. Yes, he can, but no, he won't.
    Lars Eighner, Oct 13, 2009
    #2
    1. Advertising

  3. mike <> wrote:
    >I need to find if the line before 'public void aMethod()' has a
    >
    >// or */


    for my $i (1..$#lines) {
    if (index($lines[$i], 'public void aMethod()') >=0) {
    print 'Line ', $i-1, " contains //\n"
    if (index($lines[$i-1], '//') >=0);
    print 'Line ', $i-1, " contains */\n"
    if (index($lines[$i-1], '*/') >=0);
    }
    }

    HTH

    jue
    Jürgen Exner, Oct 13, 2009
    #3
  4. mike

    C.DeRykus Guest

    On Oct 13, 7:30 am, mike <> wrote:
    > Hi,
    >
    > I need to find if the line before 'public void aMethod()' has a
    >
    > // or */
    >
    > I have tried the following:
    >
    > if($current_line =~ /(?:\\\|\*\\)/){
    >
    > }
    >
    > br,
    >
    > //mike
    >
    > //here is some text
    > public void aMethod(){
    >
    > }
    >
    > or
    >
    > /**
    > * Here is some text
    > */
    > public void aMethod(){
    >
    > }
    >
    >


    With 5.10, you could use lookbehind'x \K :

    my $qr = qr[ ( # start look-behind
    (?: # non-capturing
    // # alternative 1
    | # or
    \*/ # alternative 2
    ) # end non-capturing
    .* \n\K # rest of line
    ) # end look-behind
    public \s+ void \s+ aMethod\(\){
    ]x;

    if ( $current_line =~ /$qr/ ) {
    ...

    --
    Charles DeRykus
    C.DeRykus, Oct 13, 2009
    #4
  5. mike

    mike Guest

    On 13 Okt, 17:35, Lars Eighner <> wrote:
    > In our last episode,
    > <>,
    > the lovely and talented mike
    > broadcast on comp.lang.perl.misc:
    >
    > > Hi,
    > > I need to find if the line before 'public void aMethod()' has a
    > > // or */
    > > I have tried the following:
    > > if($current_line =~ /(?:\\\|\*\\)/){
    > > }

    >
    > Why do you think a backslash, even when escaped with a backslash, will match
    > a forward slash?


    You are absolutely correct I missed it.

    Thanks!

    //mike
    >
    > > br,
    > > //mike
    > > //here is some text
    > > public void aMethod(){
    > > }
    > > or
    > > /**
    > > * Here is some text
    > > */
    > > public void aMethod(){
    > > }

    >
    > --
    >   Lars Eighner <http://larseighner.com/>                 September 5887, 1993
    >             265 days since Rick Warren prayed over Bush's third term.
    >   Obama: No hope, no change, more of the same. Yes, he can, but no, he won't.
    mike, Oct 13, 2009
    #5
  6. mike

    mike Guest

    On 13 Okt, 20:07, "C.DeRykus" <> wrote:
    > On Oct 13, 7:30 am, mike <> wrote:
    >
    >
    >
    > > Hi,

    >
    > > I need to find if the line before 'public void aMethod()' has a

    >
    > > // or */

    >
    > > I have tried the following:

    >
    > > if($current_line =~ /(?:\\\|\*\\)/){

    >
    > > }

    >
    > > br,

    >
    > > //mike

    >
    > > //here is some text
    > > public void aMethod(){

    >
    > > }

    >
    > > or

    >
    > > /**
    > > * Here is some text
    > > */
    > > public void aMethod(){

    >
    > > }

    >
    > With 5.10, you could use lookbehind'x \K :
    >
    > my $qr = qr[ (           # start look-behind
    >                 (?:      # non-capturing
    >                   //     #   alternative 1
    >                   |      #       or
    >                   \*/    #   alternative 2
    >                 )        # end non-capturing
    >                 .* \n\K  # rest of line
    >              )           # end look-behind
    >              public \s+ void \s+ aMethod\(\){
    >             ]x;
    >
    > if ( $current_line =~ /$qr/ ) {
    >   ...
    >
    > --
    > Charles DeRykus


    Really nice with comments so this was easy to understand and learn
    something from.

    //mike
    mike, Oct 13, 2009
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Greg Hurrell
    Replies:
    4
    Views:
    145
    James Edward Gray II
    Feb 14, 2007
  2. Mikel Lindsaar
    Replies:
    0
    Views:
    445
    Mikel Lindsaar
    Mar 31, 2008
  3. Joao Silva
    Replies:
    16
    Views:
    328
    7stud --
    Aug 21, 2009
  4. Uldis  Bojars
    Replies:
    2
    Views:
    176
    Janwillem Borleffs
    Dec 17, 2006
  5. Matìj Cepl

    new RegExp().test() or just RegExp().test()

    Matìj Cepl, Nov 24, 2009, in forum: Javascript
    Replies:
    3
    Views:
    164
    Matěj Cepl
    Nov 24, 2009
Loading...

Share This Page