For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > June 2005 > Pattern Question: One or both, but not neither









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Pattern Question: One or both, but not neither
Siegfried Heintze

2005-06-03, 8:55 pm

Can I write a pattern that matches "Tampa" or "Florida", or "Tampa Florida"?

Thanks,
Siegfried

-----Original Message-----
From: Jeff 'japhy' Pinyan [mailto:japhy@perlmonk.org]
Sent: Friday, June 03, 2005 8:44 AM
To: Jay Savage
Cc: Perl Beginners List
Subject: Re: Search Pattern for Roman Numerals?

On Jun 3, Jay Savage said:

> On 6/3/05, Jeff 'japhy' Pinyan <japhy@perlmonk.org> wrote:
>
>
> This isn't going to get them all; it says to match (between word
> boundaries) "I" or "II" or any three non-newlines. So it will catch
> "I", "II", "III", and "VII". It will also catch "I" where it's a
> pronoun (assuming this is an english text file), and any three-letter
> words/constructs.


I'm sorry, that regex wasn't meant to be taken literally. I just didn't
feel the need to reproduce the alternations *again*.

> I would trysomething like this:
>
> s/\bI(?:I+|V|X)?|VI*|XI*\b//


This will get rid of the "I" in "Ishmael". Your \b anchors aren't
effective on the *entire* pattern. You're matching

\bI(?:I+|V|X)?
or
VI*
or
XI*\b

The regex I would use would probably be

/\b(?:I{1,3}|IV|VI{0,3}|I?X)\b/

--
Jeff "japhy" Pinyan % How can we ever be the sold short or
RPI Acacia Brother #734 % the cheated, we who for every service
http://japhy.perlmonk.org/ % have long ago been overpaid?
http://www.perlmonks.org/ % -- Meister Eckhart

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Chris Devers

2005-06-03, 8:55 pm

On Fri, 3 Jun 2005, Siegfried Heintze wrote:

> Can I write a pattern that matches "Tampa" or "Florida", or "Tampa Florida"?


I'm sure someone can.

What happened when you tried it?

You did try, right?


--
Chris Devers
Wagner, David --- Senior Programmer Analyst --- WG

2005-06-03, 8:55 pm

Siegfried Heintze wrote:
> Can I write a pattern that matches "Tampa" or "Florida", or "Tampa
> Florida"?=20
>=20
> Thanks,
> Siegfried

You would have to order it so that if wanted Tampa Florida that would have=
priority over Tampa or Florida

/(Tampa Florida|Tampa|Florida)/
one method.
Wags ;)


****************************************
***************
This message contains information that is confidential
and proprietary to FedEx Freight or its affiliates.
It is intended only for the recipient named and for
the express purpose(s) described therein.
Any other use is prohibited.
****************************************
***************

Jeremy Vinding

2005-06-03, 8:55 pm

Wagner, David --- Senior Programmer Analyst --- WGO wrote:

>Siegfried Heintze wrote:
>
>
> You would have to order it so that if wanted Tampa Florida that would have priority over Tampa or Florida
>
> /(Tampa Florida|Tampa|Florida)/
>one method.
>Wags ;)
>
>

you could also do:
/(?:Tampa? Florida)|Tampa/

--jjv
Jeremy Vinding

2005-06-06, 3:56 pm

Ing. Branislav Gerzo wrote:

>Jeremy Vinding [JV], on Friday, June 03, 2005 at 16:34 (-0600) has on
>mind:
>
>
>
>JV> You caught me, but that matches "Tampa Tampa" too
>JV> what I really meant was:
>JV> /(?:(?:Tampa )?Florida)|Tampa/
>
>sorry, but it won't match "tampa tampa", just try :)
>it match left side from | or right side. not both :)
>So you save one (?:)
>
>(see that smile at the end:)
>
>
>

$ perl -le'print "yes" if "Tampa Tampa" =~ /(?:Tampa )?Florida|Tampa/'
yes
Jeremy Vinding

2005-06-06, 3:56 pm

Jeremy Vinding wrote:

> Ing. Branislav Gerzo wrote:
>
> $ perl -le'print "yes" if "Tampa Tampa" =~ /(?:Tampa )?Florida|Tampa/'
> yes
>

my bad, it seems to have to do w/ anchors, the second part of the
alternation was matching.

$perl -le'print "yes" if "Tampa Tampa" =~ /^(?:(?:Tampa )?Florida|Tampa)$/'

--jjv
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2009 codecomments.com