Click here to get back home

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

 HomeNewsGroups | Search | About
 comp.lang.perl.misc    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
Can I use a look-ahead and a look-behind at the same time? dan.j.weber 04-09-2008
Posted by dan.j.weber on April 9, 2008, 6:01 pm
Please log in for more thread options
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#

Posted by Joost Diepenmaat on April 9, 2008, 6:07 pm
Please log in for more thread options
dan.j.weber@gmail.com 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/

Posted by A. Sinan Unur on April 9, 2008, 6:16 pm
Please log in for more thread options
dan.j.weber@gmail.com 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__

--
(remove .invalid and reverse each component for email address)

comp.lang.perl.misc guidelines on the WWW:
http://www.rehabitation.com/clpmisc/

Posted by xhoster on April 9, 2008, 6:18 pm
Please log in for more thread options
dan.j.weber@gmail.com 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.

Posted by dan.j.weber on April 9, 2008, 6:25 pm
Please log in for more thread options
On Apr 9, 3:18=A0pm, xhos...@gmail.com wrote:
> dan.j.we...@gmail.com 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#" =3D~
> =A0 =A0 /(?<=3D#ab cd ef#)(.*?)(?=3D#qr st uv#)/ or die;
> print $1
> __END__
> gh ij kl
>
> Yep, seems to work. =A0Which 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. =A0Neither look ah=
ead
> 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.

Similar ThreadsPosted
GMT time to local time, according to timezone and summer/winter time. May 15, 2005, 10:45 pm
lookahead bug (at least in 5.8.4) June 11, 2007, 12:09 pm
Lookahead peculiarity March 9, 2006, 9:18 am
help: negative lookahead and backref in regex? December 28, 2004, 4:02 pm
Regular expression lookahead question January 13, 2005, 3:57 pm
Negative lookahead regex clarification needed January 19, 2005, 12:09 pm
Calculating time of employee session from the log date/time stampusing perl May 24, 2005, 5:55 pm
Help simplify complex regexp needing positive lookahead and reluctant quantifers March 20, 2005, 11:29 am
How do I convert TIME into Cookie and last-modified-time format? October 1, 2004, 3:05 pm
Compare UNIX file time with time in a variable November 23, 2006, 2:39 pm

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap