Forum Moderators: coopster & phranque

Message Too Old, No Replies

How to find out if XYZ occurs once and only once.

         

ThomasAJ

7:35 am on Sep 8, 2002 (gmt 0)

10+ Year Member



$string = "Hello XYZ are you XYZ"

To find out if XYZ occurs once and only once, I thought it would work using:

if ($string =~ /(XYZ){1}/)

I thought the condition should be FALSE as {1} is a quantifier that is described in my documentation as "exactly 1 times".

But XYZ occurs 2 times.

However the condition is TRUE in my testing. I just don't get it.

Also, is m!regexp! same as /regexp/ but the former used for legibility?

Thanks

Tom

littleman

8:35 am on Sep 8, 2002 (gmt 0)



Using the {number} will only show true if it is matched up, it isn't good for counting. There might me a more elegant way, but you could do something like this:
$good_number = 1;
$search_word = "beer";
$t = "This beer is not where it is at for a cat, but is for a dog";
my $i = 0;
my @words = split(/\W/,$t);
foreach (@words) {
if ( $_ =~/^$search_word$/i ) { $i++ }
}

if ($i == $good_number){ print "good\!\n"; }
else {

print "bad, $search_word is in there $i, which is not right\n"
}

gsx

9:33 am on Sep 8, 2002 (gmt 0)

10+ Year Member



Perl:

$times = 0;
if ($string =~ /(XYZ)/) { $times++; } # $string contains XYZ once or more#
if ($string =~ /(XYZ).*?(XYZ)/) { $times++; } # $string contains XYZ twice or more#

Then, $times contains, 0 for none, 1 for once and 2 for twice (or more).

ThomasAJ

9:35 am on Sep 8, 2002 (gmt 0)

10+ Year Member



littleman

Thanks, I'll test your suggestion out.

However I don't understand what you mean by "Using the {number} will only show true if it is matched up, it isn't good for counting.".

So what would the difference be between m!(XYZ){1}! and m!(XYZ){2}!

I looked at more 'quantifiers', specifically {i,j} which is described as "at lease i times and at most j times".

So I tried m!(XYZ){1,1}! which to me means return TRUE if XYZ is found at least 1 times and at most 1 times. But that did not work.

Obviously I am misunderstanding something fundamental here.

gsx

Your suggestion came up after I posted this reply, so I'm editing my reply. Thanks also.

++++++++++++++++++

Also, is m!regexp! same as /regexp/ but the former used for legibility?

Tom

andreasfriedrich

12:39 pm on Sep 8, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Also, is m!regexp! same as /regexp/ but the former used for legibility?

Itīs the same. Using the m style letīs you use any delimiter. That way you can choose one thatīs not in your expression to avoid having to escape it.

/\/path\/to\/file/ is a lot harder to read than m{/path/to/file}.

So what would the difference be between m!(XYZ){1}! and m!(XYZ){2}!

The former will match "XYZ", "XYZXYZ", "XYZXYZXYZ", etc while the latter will start matching with the "XYZXYZ".

To find the number of occurances of pattern in string use something like


my $c = 0;
while (m!pattern!g) {
$c++;
}

To test those things for yourself you might want to use perl from the command line like this:

perl -e '$_="XYZXYZ"; print scalar(/(XYZ){1}/);'