A typical section of the log may have data as follows:line number: action filename
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:9248: OPEN perl.doc 9249: DELETE incriminating_evidence.txt 9250: EDIT autoexec.bat
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).while (<>) { if ( /^(\d+):\s(?:EDIT|DELETE)\s(.*)$/ ) { $results{ $1 } = $2; } }
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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |