0

I'm trying to match a string in PowerShell, but I can't get True.

"one.two" -match "One Two"
False

Ok, maybe I gave too limited information of my problem.

So here is a bit more information. I have this code:

$userRoles = @('User', 'Customer Support');
$adGroups = @('ad.group.user', 'ad.group.customer.admin');
$userRoleToAdGroup = @{};
foreach ($userRole in $userRoles) {
    $adGroupsMatch = @($adGroups -match "$userRole")
    $userRoleToAdGroup.Add($userRole, $adGroupsMatch)
}
$userRoleToAdGroup

And the output value is empty for "Customer Support".

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Furieux F
  • 3
  • 2
  • 2
    From the limited info you give, my guess is that you have the strings the wrong way around. Try: `"One Two" -match "one.two"`. The string to test should appear on the left and the regex pattern on the right. – boxdog Aug 14 '20 at 08:42
  • I narrowed down my problem to very simple match. Yes, the way around it works, but it doesn't work in this case: "here.is.some.text" -match "Some Text". – Furieux F Aug 14 '20 at 08:48
  • 2
    As @boxdog tried to explain, the regex pattern to test **with** should be on the right-hand side of the `-match` operator. Your commented example uses a literal string (with a space) which is not a regex pattern at all. – Theo Aug 14 '20 at 13:29
  • The title is too general. Thus this question comes up in search engine results for queries like *"PowerShell fuzzy string match"*. A more canonical question is: *[PowerShell and the -contains operator](https://stackoverflow.com/questions/18877580/powershell-and-the-contains-operator/18877950#18877950)* (the answer covers the difference between `-Contains` (for collections), `-match` / `-imatch` (regular expression string matching), and `-like`, `-ilike` (SQL-like matching)) – Peter Mortensen Jun 09 '21 at 09:02

2 Answers2

0

Here the space between "One" and "Two" will match literally "One Two". If you want to match any character between them use:

"one.two" -match "One.Two"

Here in Regex . means any character. Or you can append

* = 0 or more instances of previous charcter
+ = 1 or more instances of previous charcter
? = 0 or 1 instances of previous charcter
\w = match any word
\d = match any digit
\s = match any whitespace
{n,m} = match n to m instances of previous charcter
Wasif
  • 14,755
  • 3
  • 14
  • 34
0

I don't exactly understand what you are trying to achieve but you should take a look at PowerShell syntax and the -match operator.

Matching a role name like Customer Support is really just matching the same exact string when using regex. One way to make this match like a wildcard would be

$String = "Customer Support"
$MatchString = "($($String -replace '\s+', '|'))"
'ad.group.customer.admin' -match $MatchString
True

This code is spliting any spaces in $String and combines them in a legit regex query "(Customer|Support)".

NOTE

This code will match any string with the word "customer" and / or "support" in it.

'ad.group.user.support' -match $MatchString
True
philmph
  • 143
  • 7