tcsh 脚本:if 语句中的正则表达式

发布于 2024-10-09 07:50:21 字数 117 浏览 0 评论 0原文

在 tcsh shell 脚本中编写此 perl 语句的正确方法是什么

foreach (@array) { 如果 (/^(pam|pom)/) { 做某事(); } }

what is the correct way to code this perl statment in tcsh shell script

foreach (@array) {
if (/^(pam|pom)/) {
dosomething();
}
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

回眸一遍 2024-10-16 07:50:21

这是一种方法:

#!/bin/tcsh -f

set array = ( foo pam bar pom baz xpam pamx )
alias dosomething echo

foreach elem ($array:q)
    if ($elem:q =~ {pam,pom}*) then
        dosomething $elem:q
    endif
end

请注意,=~ 运算符右侧的表达式是文件匹配模式,而不是正则表达式,因此此解决方案不能推广到所有情况。如果需要正则表达式匹配,可以使用 expr 命令:

expr STRING : REGEXP

或者等价:

expr match STRING REGEXP

Here's one way:

#!/bin/tcsh -f

set array = ( foo pam bar pom baz xpam pamx )
alias dosomething echo

foreach elem ($array:q)
    if ($elem:q =~ {pam,pom}*) then
        dosomething $elem:q
    endif
end

Note that the expression on the right side of the =~ operator is a file matching pattern, not a regular expression, so this solution doesn't generalize to all cases. If you need regular expression matching, you can use the expr command:

expr STRING : REGEXP

or, equivalently:

expr match STRING REGEXP
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文