By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,844 Members | 1,413 Online
Bytes IT Community
+ Ask a Question
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
  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
Share this Question
Share on Google+
4 Replies


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

nithinpes
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
  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

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
  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
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
  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.