简体   繁体   中英

Regex bash script

Can anyone help me understand why this doesn't work? Just trying out some simple regex in bash.

#!/bin/bash

re="-regex_"

if [[ "$re" =~ ^[-[:alpha:]_]+$ ]]; then
        echo "Regex"

else
        echo "this is not regex"        
fi

Cheers

I am assuming that you are hoping that the "-regex_" will evaluate to true in your if statement.

on the [:alpha:] tag there is nothing to say search for more than one alpha-numeric character.

try

[[ "$re" =~ ^-[[:alpha:]]+_$ ]]

If you are having an error running it, make sure you are using unix line endings (run it through dos2unix) and make sure it is marked executable. Otherwise, the script prints "Regex" for me.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM