gpt4 book ai didi

regex - bash 正则表达式(或测试)奇怪的行为

转载 作者:太空宇宙 更新时间:2023-11-04 09:45:40 26 4
gpt4 key购买 nike

[myuser@mycomputer]$ word="hello"
[myuser@mycomputer]$ if [[ $word =~ "^hello$" ]]; then echo "it was a hello"; else echo "must have been a goodbye"; fi
must have been a goodbye

我不明白我的错误在哪里,但我预料到相反的结果。

最佳答案

首先,您必须使用 $ 取消引用您的变量。

if [[ $word =~ "^hello$" ]]
^

然后,您不能将字符串括在双引号 " 中,以便能够使用正则表达式。

if [[ $word =~ ^hello$ ]]

关于regex - bash 正则表达式(或测试)奇怪的行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16585360/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com