【问题标题】:How can I test whether a string contains another string in fish shell?如何测试一个字符串是否包含鱼壳中的另一个字符串?
【发布时间】:2017-10-08 15:47:46
【问题描述】:

如何测试鱼壳中是否存在子字符串?例如,在 switch 表达式中:

 set myvar "a long test string"
 switch $myvar
 case magical-operator-here "test string" 
     echo 'yep!'
 case '*'
     echo 'nope!'
 end

【问题讨论】:

    标签: fish


    【解决方案1】:

    * 是通配符,所以

    set myvar "a long test string"
    switch $myvar
    case "*test string" 
        echo 'yep!'
    case '*'
        echo 'nope!'
    end
    

    如果您想测试它是否以该字符串结束。如果它也可以出现在中间某处,请在末尾添加另一个*

    此外,从 2.3.0 开始,fish 有一个带有 match 子命令的 string 内置函数,因此您也可以使用 string match -q -- "*test string" $myvar。它还支持带有“-r”选项的 pcre 样式的正则表达式。

    【讨论】:

    • 我一直很欣赏您与鱼壳相关的 StackOverflow 答案。 ?
    • 谢谢! string match 命令就是我要找的。​​span>
    猜你喜欢
    • 2014-07-23
    • 2015-09-16
    • 1970-01-01
    • 1970-01-01
    • 2021-05-06
    • 1970-01-01
    • 2014-02-24
    • 2013-03-13
    相关资源
    最近更新 更多