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

D

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#
 
A

A. Sinan Unur

(e-mail address removed) wrote in
@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 <[email protected]>
(remove .invalid and reverse each component for email address)

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

xhoster

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.
 
D

dan.j.weber

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,731
Messages
2,569,432
Members
44,832
Latest member
GlennSmall

Latest Threads

Top