Solved

Bash Regular expression

Posted on 2010-09-16
6
806 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
Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

 
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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
This article will show, step by step, how to integrate R code into a R Sweave document
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

786 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