I certainly endorse using CPAN for complete solutions or for useful methods of attack. However, sometimes you just have to make your own wheel.

The following approach decomposes regexes into much more easily understandable and maintainable parts. While it's a lot more typing to begin with, the gain in robustness and maintainability is, I find, well worth the cost.

use warnings; use strict; my $rx_comma = qr{ \s* , \s* }xms; my $rx_date = qr{ \d{4} - \d\d - \d\d }xms; my $rx_name = qr{ [[:alpha:]] (?: '? [[:alpha:]]+)? }xms; my $rx_hyphenate = qr{ - $rx_name }xms; my $rx_surname = qr{ $rx_name $rx_hyphenate? }xms; my $rx_initial = qr{ [[:alpha:]] \. }xms; my $rx_givenname = qr{ $rx_initial | $rx_surname }xms; my $rx_prof = qr{ $rx_surname (?: $rx_comma (?: \s* $rx_givenname )+ )? }xms; # avoid polluting namespace with a bunch of common variable names. my $rx_facility = do { my $room = qr{ \d{3,4} [[:alpha:]]? }xms; my $range = qr{ $room (?: \s* - \s* $room)? }xms; my $rooms = qr{ $range (?: $rx_comma $range)* }xms; my $function = qr{ \( [^)]+ \) }xms; qr{ $rooms \s* $function }xms; # final regex }; $/ = ""; # paragrep mode while (my $entry = <DATA>) { my ($date, $prof, @facilities) = $entry =~ m{ $rx_date | $rx_prof | $rx_facility }xmsg; print <<EOS; date: '$date' prof: '$prof' facilities: '@{[ join qq{' \n '}, @facilities ]}' EOS } __DATA__ 2006-01-01,O'Reilly,Watson B., 406,560(centrifuge,refrig.), 569b,607(dark room),210-211,101(ultracentrifuge), 104-105(crystal growth rooms),660(centrifuge, refrig.) 2007-02-02, Olsen, Alfa-Betty Z. , 102a-102c, 104(media lab) , 101(writer's lounge) 2008-03-04,Peebles, P.J.E., 1000a - 9999z (physical cosmology lab.), 000-001 (computational cosmology lab)
Output:
>perl regex_parse_fields_1.pl date: '2006-01-01' prof: 'O'Reilly,Watson B.' facilities: '406,560(centrifuge,refrig.)' '569b,607(dark room)' '210-211,101(ultracentrifuge)' '104-105(crystal growth rooms)' '660(centrifuge, refrig.)' date: '2007-02-02' prof: 'Olsen, Alfa-Betty Z.' facilities: '102a-102c, 104(media lab)' '101(writer's lounge)' date: '2008-03-04' prof: 'Peebles, P.J.E.' facilities: '1000a - 9999z (physical cosmology lab.)' '000-001 (computational cosmology lab)'

In reply to Re: Regular Expression, Catching Variables by AnomalousMonk
in thread Regular Expression, Catching Variables by lev

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.