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

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

am 10.04.2008 00:01:51 von dan.j.weber

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#

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

am 10.04.2008 00:07:35 von Joost Diepenmaat

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/

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

am 10.04.2008 00:16:40 von 1usa

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__

--
A. Sinan Unur <1usa@llenroc.ude.invalid>
(remove .invalid and reverse each component for email address)

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

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

am 10.04.2008 00:18:43 von xhoster

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.

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

am 10.04.2008 00:25:39 von dan.j.weber

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.

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

am 10.04.2008 01:32:52 von Jim Gibson

In article
,
wrote:

> On Apr 9, 3:18 pm, 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#" =~
> >     /(?<=#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.
>
> 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.

Xho's example works either with (.*?) or (.*), so your problem may lie
elsewhere. The only difference would be if your string included two
'#qr st uv#' substrings after the initial '#ab cd ef#'. In that case
(.*?) will match the shortest possible string, while (.*) will match
the longest.

--
Jim Gibson

Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com