How to test if a string contains one of multiple substrings?

时光怂恿深爱的人放手 提交于 2019-12-05 04:59:39

You could use the -match method and create the regex automatically using string.join:

$referenz = @('abc', 'def', 'xyz')    
$referenzRegex = [string]::Join('|', $referenz) # create the regex

Usage:

"any string containing abc" -match $referenzRegex # true
"any non matching string" -match $referenzRegex #false

Regex it: $a -match /\a|def|xyz|abc/g (https://regex101.com/r/xV6aS5/1)

  • Match exact characters anywhere in the original string: 'Ziggy stardust' -match 'iggy'

source: http://ss64.com/ps/syntax-regex.html

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!