开发者

strings / substrings matching in tcsh

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.

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

with

set st = "red"

the line

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

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) ?

Or another way to a开发者_如何学JAVAsk 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.

Thanks a million!


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

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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜