Re: regex for &
- From: rcoops@xxxxxxxxx ("Rob Coops")
- Date: Thu, 30 Oct 2008 11:13:48 +0100
On Thu, Oct 30, 2008 at 10:56 AM, Brent Clark <brentgclarklist@xxxxxxxxx>wrote:
Hiya
I have three sentences.
This is a nice hotel.
The view & food is good.
We are at the Victoria & Alfred Hotel.
I need a perl regex / code to not print out sentence 2 (basically fail).
This is what I so far.
print $_ if $_ !~ /\&|Victoria \&/ig;
Im struggling to get this right.
TIA.
Regards
Brent Clark
--
To unsubscribe, e-mail: beginners-unsubscribe@xxxxxxxx
For additional commands, e-mail: beginners-help@xxxxxxxx
http://learn.perl.org/
Hi Brent,
I just tried this and found that you where nrealy there.
#!/usr/local/bin/perl
use strict;
use warnings;
my @text = ('This is a nice hotel.', 'The view & food is good.', 'We are at
the Victoria & Alfred Hotel.');
foreach ( @text ) {
print $_ . "\n" if ( $_ !~ /(?<!Victoria) \&/i );
}
Prints:
This is a nice hotel.
We are at the Victoria & Alfred Hotel.
What the (?<!Victoria) bit does is nothing at first the & gets matched and
then the (?<!Victoria) bit kicks in and checks to see if the string
Victoriais not (thats the !'s doing use and = to require it) infront
of the &.
Try having a look at
perlretut<http://perldoc.perl.org/perlretut.html#Backreferences> for
more information on how this works.
Reagrds,
Rob
- Follow-Ups:
- Re: regex for &
- From: Brent Clark
- Re: regex for &
- References:
- regex for &
- From: Brent Clark
- regex for &
- Prev by Date: RE: regex for &
- Next by Date: RE: regex for &
- Previous by thread: RE: regex for &
- Next by thread: Re: regex for &
- Index(es):
Relevant Pages
|