Hello awesome community,
I'm a complete dope when it comes to regex. I've put off learning it.. and now my laziness has caught up with me.
What I'm trying to do:
Check if a string matches this format:
10_06_13 ie. 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.
I'd be grateful if anyone could help point me in the right direction.
Cheers
=~ 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:
() from the regex and "" from the [[ ... ]].=~ succeeds if the string matches. If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With