简体   繁体   English

正则表达式bash脚本

[英]Regex bash script

Can anyone help me understand why this doesn't work? 谁能帮助我了解为什么这不起作用? Just trying out some simple regex in bash. 只是在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. 我假设您希望if语句中的“ -regex_”评估为true。

on the [:alpha:] tag there is nothing to say search for more than one alpha-numeric character. 在[:alpha:]标记上,没有什么可说要搜索多个字母数字字符。

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. 如果您在运行它时遇到错误,请确保您使用的是UNIX行尾(通过dos2unix运行它),并确保将其标记为可执行文件。 Otherwise, the script prints "Regex" for me. 否则,脚本会为我打印“ Regex”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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