Further to LanX's reply and haukex's reply: Note that in addition to being used to compose more complex regexes, a  qr// object can be quantified as discussed here (with one small exception discussed below) in the same way as other regex atoms.

The quantifier exception is for the case of a counting quantifier on a regex object that looks "too much" like a hash element. The problem is rare (albeit potentially completely silent if it is present!) and easily fixed:

c:\@Work\Perl\monks>perl -wMstrict -le "my %rx = ( 2 => 'Oops...' ); my $rx = qr{ \b foo \b }xms; ;; my $n = 2; my $ry = qr{ $rx{2} X $rx{$n} Y (?:$rx){$n} }xms; print $ry; " (?msx-i: (?msx-i: \b foo \b ){2} X Oops... Y (?:(?msx-i: \b foo \b )){ +2} )
(Update: Changed this code example to make it shorter, hopefully clearer.)

(BTW: Note also that  $RE{net}{IPv4} from Regexp::Common::net is by design not delimited, so there can be a match in certain undesired or surprising cases:

c:\@Work\Perl\monks>perl -wMstrict -le "use Regexp::Common qw(net); ;; my $ipv4_A = qr{ $RE{net}{IPv4} }xms; my $ipv4_B = qr{ \b $RE{net}{IPv4} \b }xms; ;; print 'match A' if '99999.9.9.99999' =~ $ipv4_A; print 'match B' if '99999.9.9.99999' =~ $ipv4_B; " match A
Caveat Programmor. :)

Update: Here's a fun (for some definition of "fun") little problem. A decimal (i.e., base-10) IPv4 address regex could be neatly defined as follows:

my $octet = qr{ \d+ }xms; my $ipv4 = qr{ \b $octet (?: [.] $octet){3} \b }xms;
Unfortunately, this matches an IP address with octets like 256 or 99999. How would you define  $octet as a pure (i.e., no  (?{ code }) or  (?{{ code }}) constructs) regex so that only decimal octets in the range 0 .. 255 were matched? (Please, no experienced regex wranglers need reply!)


Give a man a fish:  <%-{-{-{-<


In reply to Re: Defining your own regex character class by AnomalousMonk
in thread Defining your own regex character class by igoryonya

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.