I'm not sure that I understand all the questions. It appears to me that you've asked a couple. This question is bit different than the first one. It is of consequnce to note that \w characters are "a-zA-Z0-9_", meaning that any \d is also a \w.

Match global is great at repetitive pattern matching!

The below shows how to match a "word" followed by some numbers. Enforcing a minimum number of "numbers" after the "word" is easy. The below shows cases where there has to be at least one number or two numbers. The case of enforcing a max is more difficult and I haven't come up with the right syntax. I suppose your intent is that jkl shouldn't appear as there are 5 numbers after that "word", the below shows the first 3 numbers after jkl instead of competely omitting that line as for example xyz was omitted as there aren't any numbers after that "word".

I think there is some "look ahead" regex syntax that would solve this problem. But I'm not completely sure that is what you are asking about.

#!/usr/bin/perl -w use strict; my $input = "abc 456 897 xyz www 789 jkl 0123 456 889 3 4 fhg 123"; print "input=$input\n"; my @nums = $input =~ m/([a-zA-Z]+(?:\s+\d+){1,3})/g; print "$_\n" foreach (@nums); #prints: #input=abc 456 897 xyz www 789 jkl 0123 456 889 3 4 fhg 123 #abc 456 897 #www 789 #jkl 0123 456 889 #fhg 123 print "----\n"; @nums = $input =~ m/([a-zA-Z]+(?:\s+\d+){2,3})/g; print "$_\n" foreach (@nums); #prints: #---- #abc 456 897 #jkl 0123 456 889

In reply to Re^5: Variable matching on a regex by Marshall
in thread Variable matching on a regex by LaintalAy

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.