Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

Me again---having some trouble with regexes (again..!)

This is about grouping, and (back?)referencing. I'm reading in a configuration file, and for each line I end up with a string like this:

$table_ref="!Frequency[A][B][C] ..some other text.."

I want to get the name of the table (in this case 'Frequency'), and then each of the table variables (here represented by A, B, C) the variables will always be inside square brackets, and there are between 1 and 4 inclusive of them (but I won't know how many).

So, I tried a regex like this:

$temp_=~ /\!(\S+?) #tablename (?:\[(.*?)\]){1,4} #table variables \s+.*/x; #white space then rest of text. print Dumper ($1, $2, $3, $4, $5);

For the string above, the regex returned:

$VAR1 = 'Frequency';
$VAR2 = 'C';
$VAR3 = undef;
$VAR4 = undef;
$VAR5 = undef;

When I replaced the {1,4} with a {3} (I thought there might have been a problem with greediness) I got exactly the same output as above..

I sort of expected (or rather hoped!) that the {1,4} would create multiple matches which would go into $2, $3, $4. Should it and have I done something wrong, or is that not how it works? I read the perdoc info (and some other stuff), but couldn't gather from that how it was supposed to work in this case. Any suggestions, in either case?

Thankyou again monks, you are giving me an enormous amount of help. It makes learning Perl (even more) fun when there are knowledgable people to point out your stupid mistakes/misunderstandings :)

........
Those are my principles. If you don't like them I have others.
-- Groucho Marx
.......

In reply to Regex - backreferencing multiple matches in one group. by why_bird

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, 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, 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.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others surveying the Monastery: (4)
    As of 2018-10-16 21:17 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      When I need money for a bigger acquisition, I usually ...














      Results (89 votes). Check out past polls.

      Notices?