455,844 Members | 1,413 Online Need help? Post your question and get tips & solutions from a community of 455,844 IT Pros & Developers. It's quick & easy.

# how to Exactly match a 4 digit number

 P: 7 Exactly match a 4 digit number My program but output is 2569 but I want it to output four digit no 2004. Expand|Select|Wrap|Line Numbers use strict; my \$string = "I  have 256987, 2004 and I a 587458";   if (\$string =~ /(\d{4})/) { print \$1; } Mar 17 '08 #1
4 Replies

 P: 7 Expand|Select|Wrap|Line Numbers if (\$string =~ /\s+(\d{4})\s+/) Mar 17 '08 #2

 Expert 100+ P: 410 By specifying a number(4) inside curly braces, the regex will match exactly 4 digits. In your case, it matched exactly 4 digits and displayed it. You had no other restrictions on that pattern, so the string could further have digits,spaces, alpha-characters following those 4 digits. But the regex will match the first occurence of continuous 4-digits and returns true. If you want to get 4 digit number, you should use either word boundary(\b) or space(\s) around the 4-digit number. Expand|Select|Wrap|Line Numbers use strict; my \$string = "I  have 256987, 2004 and I a 587458";   if (\$string =~ /\b(\d{4})\b/) { print \$1; }   Mar 17 '08 #3

 P: 51 By specifying a number(4) inside curly braces, the regex will match exactly 4 digits. In your case, it matched exactly 4 digits and displayed it. You had no other restrictions on that pattern, so the string could further have digits,spaces, alpha-characters following those 4 digits. But the regex will match the first occurence of continuous 4-digits and returns true. If you want to get 4 digit number, you should use either word boundary(\b) or space(\s) around the 4-digit number. Expand|Select|Wrap|Line Numbers use strict; my \$string = "I  have 256987, 2004 and I a 587458";   if (\$string =~ /\b(\d{4})\b/) { print \$1; }   Hi, i just wanted to expand this problem a little further, what if there are no limit of the number of 4 digit numbers in the string, and you want to print them all. one possible solution for this problem is to work on iterations, each time cutting the remaining part of the string in the place of the last found number. Expand|Select|Wrap|Line Numbers use strict; my \$string = "I  have 256987, 2004 and I a 587458";   while (\$string =~ /\b(\d{4})\b(.*)/) { print \$1; \$string = \$2;  } Mar 17 '08 #4

 Expert 100+ P: 410 Hi, i just wanted to expand this problem a little further, what if there are no limit of the number of 4 digit numbers in the string, and you want to print them all. one possible solution for this problem is to work on iterations, each time cutting the remaining part of the string in the place of the last found number. use strict; my \$string = "I have 256987, 2004 and I a 587458"; while (\$string =~ /\b(\d{4})\b(.*)/) { print \$1; \$string = \$2; } Well, in that case you need not have to reassign string to post-match pattern with each iteration. You can make use of '/g' option which will match globally and find all occurences. Expand|Select|Wrap|Line Numbers while (\$string =~ /\b(\d{4})\b/g) { print "\$1\n"; }   Mar 17 '08 #5 