Hello friends,
My experience with regular expressions is growing, but here I need some help.
I have a file with a typical line as follows:

$line = "2006-01-01,Kims,Watson,406,560(centrifuge, refrig.),569,607(dark room),210-211,101(ultracentrifuge),104-105(crystal growth rooms),660(centrifuge, refrig.)";
(It is a list of fields separated by commas: date, building,Prof(group), room or lab(function),...)
My object is to extract each field (between commas, outside any parentheses) to separate variables ($1, $2, $3…).

Making progress, slowly, I came to a promising but puzzling point with the regex:
$line =~ m/(\d{4}-\d\d-\d\d),(\w*),(\w*),(\w*,|\w*\(.*?\),?|\w*-\w*,|\w*-\w*\(.*?\),?)*/; #{8} replaces *

This gives me four of the expected 11 variables, the fourth being the last variable of the line. O.K., I understand that the match takes the last of (all|alternative|room|patterns)*, but I would like to have all the fields captured as variables.
When I use {n} in place of the ultimate '*', I get each alternative, ‘room (function)’ field in turn as n = 1 to 8.
Is there a way to catch all variables, i.e. $4 …$11 in one expression (without looping this all through values of n)?

Also, a more minor point, the use of ',?' allows for catching the end variable which alone is not followed by a comma. In the case here with 660(...)as the end variable, using '.?' after a 'room(function)'pattern(\w*(...),?) helps, but after a nondescript 'room'pattern( \w*,?), the match fails. So, in general, not knowing the end variable, how do I account for the commas to assure that the last variable is not lost?

Below is my test program.

#!/usr/local/bin/perl use warnings; use strict; my $line = "2006-01-01,Kims,common,406,560(centrifuge,refrig.),569b,60 +7(dark room),210-211,101(ultracentrifuge),104-105(crystal growth room +s),660(centrifuge,refrig.)"; $line =~ m/(\d{4}-\d\d-\d\d),(\w*),(\w*),(\w*,|\w*\(.*?\),?|\w*-\w*,|\ +w*-\w*\(.*?\),?)*/; #{8} replaces * print "1:$1\n", "2:$2\n","3:$3\n","4:$4\n","5:$5\n","6:$6\n","7:$7\n", +"8:$8\n","9:$9\n","10:$10\n","11:$11\n";

Thanks for your time and help, lev

In reply to 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.