我是PowerShell新手,我在$ line中有一个字符串。
我如何测试$ line是否有单词“Bat”,这样它对于“Bat man”字符串成功,但是对于“Batman”失败了?
换句话说,我想测试一个单独的单词,而不是一个单词的字符串序列。
谢谢。
答案 0 :(得分:3)
对正则表达式使用字边界元字符。 The metacharacter \b
is an anchor like the caret and the dollar sign. It matches at a position that is called a "word boundary"
#True
"Bat" -match "\bBat\b"
#also True
"Bat man" -match "\bBat\b"
#False
"Batman" -match "\bBat\b"
#False
"another Batman" -match "\bBat\b"
#True
"another Bat man" -match "\bBat\b"