Occassionally, you'll find a need to write a complicated regular expression, but you want to be able to group elements of it without capturing them to a dollar/number ($1, $2, etc.) variable. For example, imagine a simple log file in this format:
line number: action filename
A typical section of the log may have data as follows:
9248: OPEN perl.doc 9249: DELETE incriminating_evidence.txt 9250: EDIT autoexec.bat
Ignoring the over-simplicity of this example, what if you wanted to write a logfile analyzer that justs extracts records that have been deleted or edited? One way, though perhaps not the best way, to do that would be the following:
while (<>) { if ( /^(\d+):\s(?:EDIT|DELETE)\s(.*)$/ ) { $results{ $1 } = $2; } }
What the (?:xxx) does is allow me to group that alternation without capturing the value. It's useful in that it is faster than capturing the value and there's no sense in capturing data if I really don't need it (though I'd probably want to know if a file was edited or deleted).

Also, note that I do have a dot star at the end. This is appropriate in this case because it's doing exactly what I wanted it to do: slurp up the rest of the line.

Also, in case you weren't aware: a regular expression without a binding operator ('=~' or '!~') automatically matches against $_, as in the above example.

Cheers,
Ovid

Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.


In reply to (Ovid non-backreferencing parens) Re(4): Regular Expression Matching by Ovid
in thread Regular Expression Matching by daviddhall

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.