string Powershell 字符串不包含
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27970441/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Powershell string does not contain
提问by Grady D
I have some code that takes in a string,
我有一些接受字符串的代码,
Foreach($user in $allUsers){
if($user.DisplayName.ToLower().Contains("example.com") -or $user.DisplayName.ToLower()) {
} else {
$output3 = $externalUsers.Rows.Add($user.DisplayName)
}
}
Part of the if
right after the -or
I need to check if the string does not contain an @ sign. How can I check to see if the @ sign is missing?
我需要检查字符串是否不包含@符号if
之后的部分右侧-or
。如何检查@符号是否丢失?
回答by Mathias R. Jessen
There are a million ways to do it, I would probably go for the following due to readability:
有一百万种方法可以做到这一点,由于可读性,我可能会选择以下方法:
$user.DisplayName -inotmatch "@"
The -match
operator does a regex match on the the left-hand operand using the pattern on the right-hand side.
该-match
运营商确实在左手操作使用在右侧的模式正则表达式匹配。
Prefixing it with i
make it explicitly case-insensitive, and the not
prefix negates the expression
用前缀它i
使其明确区分我nsensitive,和not
前缀否定表达
You could also do:
你也可以这样做:
-not($user.DisplayName.ToLower().Contains("@"))
or
!$user.DisplayName.ToLower().Contains("@")
For simple wildcard text-matching (maybe you hate regex, what do I know?):
对于简单的通配符文本匹配(也许你讨厌正则表达式,我知道什么?):
$user.DisplayName -notlike "*@*"
Or alternatively look for the substring with IndexOf;
或者使用 IndexOf 查找子字符串;
$user.DisplayName.IndexOf("@") -eq (-1)