Re: regex for &



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


Relevant Pages

  • regex for &
    ... The view & food is good. ... We are at the Victoria & Alfred Hotel. ... I need a perl regex / code to not print out sentence 2. ...
    (perl.beginners)
  • RE: regex for &
    ... 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 ...
    (perl.beginners)
  • RE: regex for &
    ... 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 ...
    (perl.beginners)
  • Re: Tipping waiters
    ... put her coat on and left sometime between ordering our food ... and delivering it. ... Frankly, if a waiter doesn't get it, he/she shouldn't be in the ... Victoria gets $8.00 an hour. ...
    (alt.sports.baseball.bos-redsox)
  • Re: Tipping waiters
    ... put her coat on and left sometime between ordering our food ... and delivering it. ... Frankly, if a waiter doesn't get it, he/she shouldn't be in the ... Victoria gets $8.00 an hour. ...
    (alt.sports.baseball.bos-redsox)