3

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

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

设置 st = "红色"

线

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

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

有没有办法避免繁重的构造:

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

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

太感谢了!

4

2 回答 2

8

我不知道 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})
于 2011-01-06T01:14:20.820 回答
5

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

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

If you need to match regular expression, you can use the expr command; man expr or info expr for details.

于 2011-07-30T20:22:58.267 回答