Solved

Bash Regular expression

Posted on 2010-09-16
6
800 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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

If you have a server on collocation with the super-fast CPU, that doesn't mean that you get it running at full power. Here is a preamble. When doing inventory of Linux servers, that I'm administering, I've found that some of them are running on l…
Recently, an awarded photographer, Selina De Maeyer (http://www.selinademaeyer.com/), completed a photo shoot of a beautiful event (http://www.sintjacobantwerpen.be/verslag-en-fotoreportage-van-de-sacramentsprocessie-door-antwerpen#thumbnails) in An…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
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.

708 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

15 Experts available now in Live!

Get 1:1 Help Now