Can I use a look-ahead and a look-behind at the same time?

Discussion in 'Perl Misc' started by dan.j.weber@gmail.com, Apr 9, 2008.

  1. Guest

    How would I match the text that's after "#ab cd ef#" and before "#qr
    st uv#" in the following string? I want to use a regular expression
    that has both a look-behind and a look-ahead together. Is this
    possible?

    #ab cd ef#gh ij kl#qr st uv#
    , Apr 9, 2008
    #1
    1. Advertising

  2. writes:

    > I want to use a regular expression that has both a look-behind and a
    > look-ahead together. Is this possible?


    AFAIK, yes. Just try it.

    --
    Joost Diepenmaat | blog: http://joost.zeekat.nl/ | work: http://zeekat.nl/
    Joost Diepenmaat, Apr 9, 2008
    #2
    1. Advertising

  3. wrote in
    news:1421afb9-6b66-45d8-ba6f-60aad330f718
    @p39g2000prm.googlegroups.co
    m:

    > How would I match the text that's after "#ab cd ef#" and before
    > "#qr st uv#" in the following string? I want to use a regular
    > expression that has both a look-behind and a look-ahead together.
    > Is this possible?
    >
    > #ab cd ef#gh ij kl#qr st uv#


    I may be misunderstanding the question, but I am not sure why you
    think you need look-ahead or look-behind here.

    #!/usr/bin/perl

    use strict;
    use warnings;

    my $x = q{#ab cd ef#gh ij kl#qr st uv#};

    if ( $x =~ /#ab cd ef#(.+)#qr st uv#/ ) {
    print "$1\n";
    }

    # You could also use split:
    print( (grep length, split /#/, $x)[1], "\n" );

    __END__

    --
    A. Sinan Unur <>
    (remove .invalid and reverse each component for email address)

    comp.lang.perl.misc guidelines on the WWW:
    http://www.rehabitation.com/clpmisc/
    A. Sinan Unur, Apr 9, 2008
    #3
  4. Guest

    wrote:
    > How would I match the text that's after "#ab cd ef#" and before "#qr
    > st uv#" in the following string? I want to use a regular expression
    > that has both a look-behind and a look-ahead together. Is this
    > possible?
    >
    > #ab cd ef#gh ij kl#qr st uv#


    I don't know what problems you are anticipating, so I'll just try doing it
    in a straightforward manner:

    use strict;
    "#ab cd ef#gh ij kl#qr st uv#" =~
    /(?<=#ab cd ef#)(.*?)(?=#qr st uv#)/ or die;
    print $1
    __END__
    gh ij kl

    Yep, seems to work. Which is what I expected, because the parts of Perl's
    regex language are supposed to work when used together--if they didn't
    there wouldn't be much point in having such a language. Neither look ahead
    nor look behind claim to be an experimental features, so I'd just
    storm ahead and use them with confidence.

    Xho

    --
    -------------------- http://NewsReader.Com/ --------------------
    The costs of publication of this article were defrayed in part by the
    payment of page charges. This article must therefore be hereby marked
    advertisement in accordance with 18 U.S.C. Section 1734 solely to indicate
    this fact.
    , Apr 9, 2008
    #4
  5. Guest

    On Apr 9, 3:18 pm, wrote:
    > wrote:
    > > How would I match the text that's after "#ab cd ef#" and before "#qr
    > > st uv#" in the following string? I want to use a regular expression
    > > that has both a look-behind and a look-ahead together. Is this
    > > possible?

    >
    > > #ab cd ef#gh ij kl#qr st uv#

    >
    > I don't know what problems you are anticipating, so I'll just try doing it
    > in a straightforward manner:
    >
    > use strict;
    > "#ab cd ef#gh ij kl#qr st uv#" =~
    >     /(?<=#ab cd ef#)(.*?)(?=#qr st uv#)/ or die;
    > print $1
    > __END__
    > gh ij kl
    >
    > Yep, seems to work.  Which is what I expected, because the parts of Perl's
    > regex language are supposed to work when used together--if they didn't
    > there wouldn't be much point in having such a language.  Neither look ahead
    > nor look behind claim to be an experimental features, so I'd just
    > storm ahead and use them with confidence.
    >
    > Xho
    >
    > --
    > --------------------http://NewsReader.Com/--------------------
    > The costs of publication of this article were defrayed in part by the
    > payment of page charges. This article must therefore be hereby marked
    > advertisement in accordance with 18 U.S.C. Section 1734 solely to indicate
    > this fact.


    Thanks for your responses. The example I gave was a simplification.
    The problem was that I was using (.*) instead of (.*?) and I'm not
    100% why, but it doesn't work like that. Thanks.
    , Apr 9, 2008
    #5
    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. Prabh
    Replies:
    1
    Views:
    423
    Wendy S
    Sep 11, 2003
  2. Aubrey Hutchison

    GO AHEAD -MAKE ME LOOK DUMB- Please

    Aubrey Hutchison, Dec 31, 2003, in forum: Python
    Replies:
    4
    Views:
    312
    Aubrey Hutchison
    Dec 31, 2003
  3. inhahe
    Replies:
    3
    Views:
    2,349
    Diez B. Roggisch
    Jan 28, 2005
  4. Neil Cerutti

    An iterator with look-ahead

    Neil Cerutti, Jan 10, 2007, in forum: Python
    Replies:
    5
    Views:
    661
    Paddy
    Jan 10, 2007
  5. jm

    Regexp: look ahead and match

    jm, Mar 1, 2004, in forum: Perl Misc
    Replies:
    5
    Views:
    94
    Matt Garrish
    Mar 2, 2004
Loading...

Share This Page