Robbie
Robbie

Reputation: 620

Regular Expression in Bash Script

I'm trying to check if a string matches this format:

10_06_13

i.e. todays date, or a similar date with "2digits_2digits_2digits".

What I've done:

regex='([0-9][0-9][_][0-9][0-9][_][0-9][0-9])'
if [[ "$incoming_string" =~ $regex ]]
then
   # Do awesome stuff here
fi

This works to a certain extent. But when the incoming string equals 011_100_131, it still passes the regex check. How can I fix my regex to only accept the right format?

Upvotes: 34

Views: 81862

Answers (1)

rici
rici

Reputation: 241721

=~ succeeds if the string on the left contains a match for the regex on the right. If you want to know if the string matches the regex, you need to "anchor" the regex on both sides, like this:

regex='^[0-9][0-9][_][0-9][0-9][_][0-9][0-9]$'
if [[ $incoming_string =~ $regex ]]
then
  # Do awesome stuff here
fi

The ^ only succeeds at the beginning of the string, and the $ only succeeds at the end.

Notes:

  1. I removed the unnecessary () from the regex and "" from the [[ ... ]].
  2. The bash manual is poorly worded, since it says that =~ succeeds if the string matches.

Upvotes: 53

Related Questions