473,396 Members | 1,893 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 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 10901
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

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

Similar topics

33
by: Prasad | last post by:
Hi, Can anyone please tell me how to store a 13 digit number in C language ? Also what is the format specifier to be used to access this variable ? Thanks in advance, Prasad P
2
by: dhutton | last post by:
How would one go about grabbing just the last 5 digits of a 16 digit number - then prefixing the last 5 numbers with 99800 so if the number is 8351101100000029 I need my SQL (Microsoft) to grab...
1
by: aidy | last post by:
Hi, I have x amount of rows in a db2 table. I want to update an account number column with a random eight digit number: This is where I have got UPDATE zzz2 SET AC = RND(/not sure what...
7
by: harijay | last post by:
Hi I am a few months new into python. I have used regexps before in perl and java but am a little confused with this problem. I want to parse a number of strings and extract only those that...
11
by: Jordan218 | last post by:
Hi. How do I write a program that sums the digits of a 4-digit number? I have a program that works, but you have to input the digits separately. Can anyone help or possibly explain what I'm doing...
7
by: abhinuke | last post by:
Been brushing up my C,C++ for my new venture in Graduate Studies for this fall.I am doing basic programs in which I am trying this one right now. A 5-digit positive integer is entered through the...
1
by: bhavanik | last post by:
haii.. How can i find the output for this query 1. List the emps whose sal is 4 digit number ending with zero --> How to count the digits in a salary number thanks bhavani
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.