Bash 将url与包含正斜杠的正则表达式匹配

Bash 将url与包含正斜杠的正则表达式匹配,bash,shell,unix,Bash,Shell,Unix,我使用下面的shell脚本来检查正则表达式 #!/bin/bash urlPattern='/demo/([^\s]+)/' #echo $urlPattern if [[ "/demo/akash/" =~ $urlPattern ]];then echo "match" else echo "not match" fi 由于bash中的正则表达式不理解\s和类似的构造,因此将其作为“不匹配”的结果提供给我。您可以使用[:space:][/code>,但是: urlPattern='/d

我使用下面的shell脚本来检查正则表达式

#!/bin/bash
urlPattern='/demo/([^\s]+)/'
#echo $urlPattern
if [[ "/demo/akash/" =~ $urlPattern ]];then
  echo "match"
else
echo "not match"
fi

由于bash中的正则表达式不理解
\s
和类似的构造,因此将其作为“不匹配”的结果提供给我。您可以使用
[:space:][/code>,但是:

urlPattern='/demo/([^[:space:]]+)/'

bash中的正则表达式不理解
\s
和类似的构造。您可以使用
[:space:][/code>,但是:

urlPattern='/demo/([^[:space:]]+)/'

您可以使用
grep
-p
来理解Perl正则表达式(就像您的一样),使用
-q
来抑制其输出,只需使用其退出值:

#!/bin/bash
urlPattern='/demo/([^\s]+)/'
if grep -qP "$urlPattern" <<< "/demo/akash/"; then
  echo "match"
else
  echo "not match"
fi
#/bin/bash
urlPattern='/demo/([^\s]+)/'

如果grep-qP“$urlPattern”,您可以使用
grep
-p
来理解Perl正则表达式(就像您的一样),并使用
-q
来抑制其输出,只使用其退出值:

#!/bin/bash
urlPattern='/demo/([^\s]+)/'
if grep -qP "$urlPattern" <<< "/demo/akash/"; then
  echo "match"
else
  echo "not match"
fi
#/bin/bash
urlPattern='/demo/([^\s]+)/'

如果grep-qP“$urlPattern”我不想更改我的正则表达式是否有其他解决方案?请切换到理解正则表达式的其他语言,例如Perl。
[:space://code>是唯一的解决方案,另请参见
对使用
[:space://code>的建议的回应是“我不想更改我的正则表达式是否有其他解决方案”。如果没有更多的背景,有几个,但没有办法知道哪一个会让你快乐。你真的应该提供更多的信息
我不想更改我的正则表达式是否有其他解决方案?请切换到理解正则表达式的其他语言,例如Perl。
[:space://code>是唯一的解决方案,另请参见
对使用
[:space://code>的建议的回应是“我不想更改我的正则表达式是否有其他解决方案”。如果没有更多的背景,有几个,但没有办法知道哪一个会让你快乐。你真的应该提供更多的信息<代码>