Solved

Bash Regular expression

Posted on 2010-09-16
6
803 Views
Last Modified: 2012-05-10
The result of the following match is successful. Why?
 x=12345 ; [[ $x =~ ^[0-9]{3} ]] && echo 3 digits

I thought I wanted only 3 digits.  So to me it should have failed.  I was only expecting it to work for x=123.

Please explain.  Also if I want to match only 3 digits, what would be the regular expression?
0
Comment
Question by:farzanj
6 Comments
 
LVL 3

Expert Comment

by:kf4zmt
ID: 33696246
Are you saying it echoed "12345"?  It echoed 5 digits?
0
 
LVL 6

Accepted Solution

by:
DalHorinek earned 250 total points
ID: 33696331
You need to

 [[ $x =~ ^[0-9]{3}$ ]]

Because otherwise it matches 3 numbers on the begining and then there can be anything ...

$ says that there can't be anything else.
0
 
LVL 4

Assisted Solution

by:Tachion
Tachion earned 200 total points
ID: 33696359
The reason it also works on 12345 (and 123abc for that matter) is that you only specify a match for the first 3 characters of the string to be numbers. You don't say "nothing may come after".

The following DOES work:

x=1234 ; [[ $x =~ ^[0-9]{3}$ ]] && echo 3 digits

The $ says, "nothing more".
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 33

Assisted Solution

by:pgnatyuk
pgnatyuk earned 50 total points
ID: 33696571
x=123 ; [[ $x = [0-9][0-9][0-9] ]] && echo 3 digits

says "3 digits" for '123' and not for '1234' or '12'.

I'm late. But I think, this is the simplest variant - nothing to explain, everything's clear
0
 
LVL 31

Author Comment

by:farzanj
ID: 33696739
Thanks for your help.  Appreciated
0
 
LVL 31

Author Comment

by:farzanj
ID: 33702007
I think due change of interface, I had awarded points but it did not go through.  Please open it again so that I can award points.  Thanks.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Little introduction about CP: CP is a command on linux that use to copy files and folder from one location to another location. Example usage of CP as follow: cp /myfoder /pathto/destination/folder/ cp abc.tar.gz /pathto/destination/folder/ab…
Fine Tune your automatic Updates for Ubuntu / Debian
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now