in reply to Re^4: Variable matching on a regex
in thread Variable matching on a regex

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