Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > a simple RegExp question

Reply
Thread Tools

a simple RegExp question

 
 
S. Levent Yilmaz
Guest
Posts: n/a
 
      05-07-2004
Hello,

It sounded like a very simple problem but I couldn't come up with an
elegant solution. The problem is:

Find all lines which contain a given word (say 'kitty') not preceded
with another given word (say 'puppy') . For instance:

I have two pets:
my kitty is very cute
but puppy is cuter than kitty

What regular expression stands only for the 2nd line? Note that the
other way around is very easy, that is the lines with 'puppy' followed
by 'kitty' is only "puppy.*kitty"
Is this possible at all with a single RegExp?

thank you so very much!
-Levent.
 
Reply With Quote
 
 
 
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      05-07-2004
S. Levent Yilmaz wrote:
> Find all lines which contain a given word (say 'kitty') not
> preceded with another given word (say 'puppy') . For instance:
>
> I have two pets:
> my kitty is very cute
> but puppy is cuter than kitty
>
> What regular expression stands only for the 2nd line? Note that the
> other way around is very easy, that is the lines with 'puppy'
> followed by 'kitty' is only "puppy.*kitty"
> Is this possible at all with a single RegExp?


Don't know, but why not just do:

/\bkitty\b/ and !/\bpuppy\b.*\bkitty\b/

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

 
Reply With Quote
 
 
 
 
Richard Morse
Guest
Posts: n/a
 
      05-07-2004
In article <c7gs2a$n9e$>,
"S. Levent Yilmaz" <> wrote:

> Hello,
>
> It sounded like a very simple problem but I couldn't come up with an
> elegant solution. The problem is:
>
> Find all lines which contain a given word (say 'kitty') not preceded
> with another given word (say 'puppy') . For instance:
>
> I have two pets:
> my kitty is very cute
> but puppy is cuter than kitty
>
> What regular expression stands only for the 2nd line? Note that the
> other way around is very easy, that is the lines with 'puppy' followed
> by 'kitty' is only "puppy.*kitty"
> Is this possible at all with a single RegExp?
>
> thank you so very much!
> -Levent.


You're looking for negative look-behind assertion. (I think).

perldoc perlre

look for "look-behind".

Short form:

my $line =~ m/(?<!puppy).*kitty/;

HTH,
Ricky

--
Pukku
 
Reply With Quote
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      05-07-2004
Richard Morse wrote:
> You're looking for negative look-behind assertion. (I think).


Think? How about testing the code before posting?

> Short form:
>
> my $line =~ m/(?<!puppy).*kitty/;


That matches any line containing "kitty".

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

 
Reply With Quote
 
Matt Garrish
Guest
Posts: n/a
 
      05-07-2004

"Gunnar Hjalmarsson" <> wrote in message
news:...
> S. Levent Yilmaz wrote:
> > Find all lines which contain a given word (say 'kitty') not
> > preceded with another given word (say 'puppy') . For instance:
> >
> > I have two pets:
> > my kitty is very cute
> > but puppy is cuter than kitty
> >
> > What regular expression stands only for the 2nd line? Note that the
> > other way around is very easy, that is the lines with 'puppy'
> > followed by 'kitty' is only "puppy.*kitty"
> > Is this possible at all with a single RegExp?

>
> Don't know, but why not just do:
>
> /\bkitty\b/ and !/\bpuppy\b.*\bkitty\b/
>


Or if the OP had bothered to read a bit of perlre:

/\bkitty\b/ and $` !~ /\bpuppy\b/

Matt


 
Reply With Quote
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      05-07-2004
Purl Gurl wrote:
> S. Levent Yilmaz wrote:
>> Find all lines which contain a given word (say 'kitty') not
>> preceded with another given word (say 'puppy') .

>
> You have failed to indicate if "kitty" followed by "puppy" is
> permissible or to be ignored.


How do you define "preceded"?

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
new RegExp().test() or just RegExp().test() Matěj Cepl Javascript 3 11-24-2009 02:41 PM
[regexp] How to convert string "/regexp/i" to /regexp/i - ? Joao Silva Ruby 16 08-21-2009 05:52 PM
Ruby 1.9 - ArgumentError: incompatible encoding regexp match(US-ASCII regexp with ISO-2022-JP string) Mikel Lindsaar Ruby 0 03-31-2008 10:27 AM
Programmatically turning a Regexp into an anchored Regexp Greg Hurrell Ruby 4 02-14-2007 06:56 PM
RegExp.exec() returns null when there is a match - a JavaScript RegExp bug? Uldis Bojars Javascript 2 12-17-2006 09:50 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57