SPUG: regarding Perl doubt

John W. Krahn jwkrahn at shaw.ca
Sun Jan 25 11:41:45 PST 2009

battipatisainagendra Bhavaniprasad wrote:
> Hi,


> I am new to this community.I have question regarding Pattern Matching
> in Perl.

In English, when you ask a question, you end the sentence with a 
question mark.  For example:

How do you ask a question?

> 1.when /ab?/ is given it returns true for "abbbb" but my question is "?"
> will support zero or one occurrence.

Quantifiers like ? affect the previous character or grouping, so that 
pattern matches a 'a' followed by zero or one 'b' characters. 
Quanifiers are normally greedy so *if possible* the pattern will prefer 
to match 'ab' instead of 'a'.  However, the string is scanned from left 
to right so the *first* possible match is found instead of the *best* 
possible match.

$ perl -le'"XXXaaccaabb" =~ /ab?/ and print $&'
$ perl -le'"XXXaabbaacc" =~ /ab?/ and print $&'
$ perl -le'"XXXabbaacc" =~ /ab?/ and print $&'

> 2.When i am giving /[0-9a-zA-Z] it returns true for "0AA" and "9aa" but my
> question is IT HAS TO ACCEPT ONLLY "0aa"/"9fG"....so on.

The character class [0-9a-zA-Z] matches *one* character.  The string 
"0AA" will match because "0" matches, the rest of the string is irrelevant.

> 3.What is the difference between Pattern Matching and regular expression?Are
> both same?

Patern Matching is a general term that applies to any kind of pattern 
matching while a regular expression is a specific implementation.

Those people who think they know everything are a great
annoyance to those of us who do.        -- Isaac Asimov

More information about the spug-list mailing list