Regular expression questions

H

Hung Truong

Hi,

I'm new at this regular expression. How can I do a match for strings that
1) contain three (or more) pairs of double letters
2) have an even number of 'a's

Thanks,
Hung
 
B

Ben Morrow

Quoth (e-mail address removed) (Hung Truong):
Hi,

I'm new at this regular expression. How can I do a match for strings that
1) contain three (or more) pairs of double letters

/(?: (.)\1 .* ){3}/x

If you actually mean 'letter' (i.e., a string like '{{' shouldn't match)
then you want

/(?: ([[:alpha:]]) \1 .* ){3}/x
2) have an even number of 'a's

/^ [^a]* (?: a [^a]* a [^a]* )* $/x

If you want to do both at once you're best off combining them with Perl
logic, rather than trying to write one regex.

Ben
 
B

Bob Walton

Hung Truong wrote:

....

I'm new at this regular expression. How can I do a match for strings that
1) contain three (or more) pairs of double letters
2) have an even number of 'a's

....

Hung

Homework time, huh? You can find what you're looking for in:

perldoc perlre

in the section titled "Regular Expressions". Just type that line at
your operating system's command prompt and read up.
 
J

Jay Tilton

(e-mail address removed) (Hung Truong) wrote:

: I'm new at this regular expression. How can I do a match for strings that
: 1) contain three (or more) pairs of double letters

Depending on whether 'xxx' should be considered to have one set of doubled
letters or two, you could say:

print "has three or more pairs of letters" if 3<=(()=/(.)\1/g);

or:

print "has three or more pairs of letters" if 3<=(()=/(.)(?=\1)/g);

: 2) have an even number of 'a's

print "has an even count of the letter 'a'" unless (()=/a/g)%2;
 
A

Ala Qumsieh

Purl said:
if ($input =~ s/([$letter])/\1/g % 2 == 0)

First of all, you don't need a character class for a single letter.
Second of all, there is no need for needless substitutions when you can
use tr/// to count the occurrence of characters.

The only gotcha is that tr/// does not interpolate its searchlist so
you'll need to eval it as a double-quoted string instead.

--Ala
 
M

Matt Garrish

Purl Gurl said:
After personal debate

It's bad enough when a person starts hearing voices, but arguing with them
is a sure sign you should check yourself into the local psychiatric ward.
Explains a lot about you, though...

Matt
 
A

Ala Qumsieh

Purl said:
After personal debate, I have decided a character [ ] class
could be removed for the originating author's unclear parameters,
which leave a reader guessing at intent.

Your point is taken, graciously.

Thank you.
However, transliteration would be more of a challenge
to use with a need to write transliteration code which
interpolates a variable.

You only have to wrap it up inside an eval:

$count = eval qq("$str" =~ tr/$letter//);
Nonetheless, the orginating author, who has yet to respond
to articles, should retain my deprecated \1 usage simply
to annoy.

True. But this question smells a lot like homework. I would rather point
the OP to the relevant docs than supply a solution.

--Ala
 
A

Anno Siegel

Abigail said:
Hung Truong ([email protected]) wrote on MMMCMIII September MCMXCIII in
<URL:
[...]

:) 2) have an even number of 'a's


I wouldn't use a regex for that.

print "Even number of 'a's\n" unless y/a/a/c;

Hmm?

Scalar y/a/a/c counts the number of non-a's, so that identifies strings
of a's only.

Anno
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top