Perl Regular Expressions Quantifiers

An item in a regular expression can have an optional quantifier,
which is one of:
Quantifier Same as Meaning
* {0,} Match 0 or more times
+ {1,} Match 1 or more times
? {0,1} Match 0 or 1 time
{n}   Match exactly n times
{n,}   Match at least n times
{n,m}   Match at least n but not more than m times

Note that an additional "?" after a quantifier prevents it from being applied
again from a first match; i.e. match the minimum number of times.

A quantifier can appear after a single character, character class, or grouping.

A quantifier tries to match as much as possible, thus:
   $x = 'the cat in the hat';
   $x =~ /^(.*)(at)(.*)$/;
yields:
   $1 = 'the cat in the h'
   $2 = 'at'
   $3 = ' ' (0 matches)

In a list context ".../g" returns a list of matches as follows:
   @words = ($x A =~ /(\w+)/g);    $word[0] = 'the'
   $word[1] = 'cat'
   $word[2] = 'in'
   $word[3] = 'the'
   $word[4] = 'hat'

Quantified regular expression always try to match the longest possible sequence of characters (unless the extra '?' is present in which case the shortest possible match is taken). But first, a regex always tries to match at the earleist possible character position in the string.

Because regular expression matching tries to match the maximim, to find the first ':' in a string like:

    spp:Fe+H20=Fe)2;H:2112:100:Haryy Potter:/home/ontherange:/bin/cork
the use of /.+:/ will match up to 'ontherange', so instead use: /[^:]+:/ which will match up to the first ':'. Alternately use the '?' quantifier as in /.+:?/ which will stop at the first ':'.