?
Solved

Bash Regular expression

Posted on 2010-09-16
6
Medium Priority
?
822 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 1000 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 800 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 33

Assisted Solution

by:pgnatyuk
pgnatyuk earned 200 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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Google Drive is extremely cheap offsite storage, and it's even possible to get extra storage for free for two years.  You can use the free account 15GB, and if you have an Android device..when you install Google Drive for the first time it will give…
Often times it's very very easy to extend a volume on a Linux instance in AWS, but impossible to shrink it. I wanted to contribute to the experts-exchange community a way of providing a procedure that works on an AWS instance. It can also be used on…
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses
Course of the Month17 days, 10 hours left to enroll

830 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