471,854 Members | 1,459 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,854 software developers and data experts.

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 10432
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
reply views Thread by YellowAndGreen | last post: by
aboka
reply views Thread by aboka | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.