469,323 Members | 1,552 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,323 developers. It's quick & easy.

how to Exactly match a 4 digit number

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
  1. use strict;
  2. my $string = "I  have 256987, 2004 and I a 587458";
  3.  
  4. if ($string =~ /(\d{4})/)
  5. {
  6. print $1;
  7. }
Mar 17 '08 #1
4 9331
Expand|Select|Wrap|Line Numbers
  1. if ($string =~ /\s+(\d{4})\s+/)
Mar 17 '08 #2
nithinpes
410 Expert 256MB
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
  1. use strict;
  2. my $string = "I  have 256987, 2004 and I a 587458";
  3.  
  4. if ($string =~ /\b(\d{4})\b/)
  5. {
  6. print $1;
  7. }
  8.  
Mar 17 '08 #3
zcabeli
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
  1. use strict;
  2. my $string = "I  have 256987, 2004 and I a 587458";
  3.  
  4. if ($string =~ /\b(\d{4})\b/)
  5. {
  6. print $1;
  7. }
  8.  
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
  1. use strict;
  2. my $string = "I  have 256987, 2004 and I a 587458";
  3.  
  4. while ($string =~ /\b(\d{4})\b(.*)/)
  5. {
  6. print $1;
  7. $string = $2; 
  8. }
Mar 17 '08 #4
nithinpes
410 Expert 256MB
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
  1. while ($string =~ /\b(\d{4})\b/g)
  2. {
  3. print "$1\n";
  4. }
  5.  
Mar 17 '08 #5

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

33 posts views Thread by Prasad | last post: by
1 post views Thread by aidy | last post: by
7 posts views Thread by harijay | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by Gurmeet2796 | last post: by
reply views Thread by mdpf | last post: by
reply views Thread by listenups61195 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.