简体   繁体   English

Powershell字符串不包含

[英]Powershell string does not contain

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. -or之后的部分if -or我需要检查字符串是否包含@符号。 How can I check to see if the @ sign is missing? 如何检查@符号是否丢失?

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- i nsensitive, 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)

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

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