string tcsh 中匹配的字符串/子字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4610778/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 00:56:58  来源:igfitidea点击:

strings / substrings matching in tcsh

stringcomparisonsubstringtcsh

提问by MadMacs

I'm struggling trying to use a concise comparison statement to avoid a bunch of "if a = b or a = c or a = d or a = e", etc.

我正在努力尝试使用简洁的比较语句来避免一堆“if a = b or a = c or a = d or a = e”等。

Instead, I'm trying to use regex and pattern matching like you would do in perl.

相反,我正在尝试使用正则表达式和模式匹配,就像您在 perl 中所做的那样。

with

set st = "red"

设置 st = "红色"

the line

线

if ($st =~ yellow|blue|red|green)

如果($st =~黄色|蓝色|红色|绿色)

just doesn't work (if: Expression Syntax.) I tried with quotes, parenthesis, but I never get the expected result, if no error.

只是不起作用(如果:表达式语法。)我尝试使用引号,括号,但如果没有错误,我永远不会得到预期的结果。

is there a way to avoid the heavy construct:

有没有办法避免重构造:

if ($st == yellow) || ($st == blue) || ($st == red) || ($st == green) ?

如果 ($st == 黄色) || ($st == 蓝色) || ($st == 红色) || ($st == 绿色) ?

Or another way to ask the same question: does tcsh allow for something like "if string a contains string b"? I couldn't find any notion of substring in tcsh reference.

或者用另一种方式问同样的问题:tcsh 是否允许诸如“如果字符串 a 包含字符串 b”之类的内容?我在 tcsh 参考中找不到任何子字符串的概念。

Thanks a million!

太感谢了!

回答by Gerrat

I don't know tsch, but looking at the info this page (under Special Characters): http://www.tcsh.org/tcsh.html/Filename_substitution.htmlhttp://www.cs.duke.edu/csl/docs/csh.htmlit appears that you need to surround your colours with braces:

我不知道 tsch,但查看此页面的信息(在特殊字符下):http://www.tcsh.org/tcsh.html/Filename_substitution.html http://www.cs.duke.edu/csl /docs/csh.html看来你需要用大括号包围你的颜色:

if ($st =~ {yellow,blue,red,green})

回答by Keith Thompson

The thing on the right hand side of the ~=operator is a "glob-pattern", nota regular expression. (For example, in a regexp .matches any character, and .*matches zero or more arbitrary characters; the glob-pattern equivalents are ?and *.)

~=运算符右侧的内容是“全局模式”,而不是正则表达式。(例如,在正则表达式中.匹配任何字符,并.*匹配零个或多个任意字符;glob-pattern 等价物是?*。)

{...,...,...}is part of the syntax of glob-patterns. man tcshfor a full description.

{...,...,...}是 glob-patterns 语法的一部分。 man tcsh完整的描述。

If you need to match regular expression, you can use the exprcommand; man expror info exprfor details.

如果需要匹配正则表达式,可以使用该expr命令;man exprinfo expr了解详情。