简体   繁体   English

如何将 UNIX 中的这个正则表达式与 expr 匹配?

[英]How to match this regexp in UNIX with expr?

Can some body pl tell me why this if condition doesn't output Matched and how to change the reg exp pattern with expr to display the output as Matched.有人可以告诉我为什么如果条件不匹配 output 以及如何使用 expr 更改 reg exp 模式以将 output 显示为匹配。 The thing is that instead of BH in the var variable there can be any country code like US or CA.问题是 var 变量中的 BH 可以是任何国家代码,例如 US 或 CA。 All the other characters in the variable remains the same.变量中的所有其他字符保持不变。

var1="BH.EBS.EBS.BH.RCMS.RCMS.FBACCR"
if [ `expr $var1 : "*.EBS.EBS.*.RCMS.RCMS.FBACCR"` -gt 0 ]; then
echo "Matched"
else
echo "Not matched"
fi

Thanks Gautam谢谢高塔姆

Something like this would do it:这样的事情会做到这一点:

.\.EBS\.EBS\...\.RCMS\.RCMS\.FBACCR

Just keep in mind that the dots in your original string are a special type of character in a regexp, this means they won't be interpreted as a dot.请记住,原始字符串中的点是正则表达式中的一种特殊类型的字符,这意味着它们不会被解释为点。 It says instead that is could be any char.相反,它说它可以是任何字符。 Then you have to scape them, hence the backslash.然后你必须对它们进行转义,因此是反斜杠。

Finally where you need to match BH, you can use dots.. if the country codes are guaranteed to be 2 chars long.最后,在您需要匹配 BH 的地方,您可以使用点。如果国家代码保证为 2 个字符长。 If not, you can use instead:如果没有,您可以改用:

.*\.EBS\.EBS\..*\.RCMS\.RCMS\.FBACCR

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM