简体   繁体   English

为什么类似于 Powershell 的运算符与我的字符串不匹配?

[英]Why does the Powershell -like operator not match my string?

Here I have 2 variables: $repo and $growth_repo_name_list .这里我有 2 个变量: $repo$growth_repo_name_list Here is the output of each:这是每个的output:

> $repo
growth_medicare_consumertargetinganalyzer

> $growth_repo_name_list
2021.2
growth_ifp_consumersummary_v2
growth_ifp_consumertargetinganalyzer
growth_medicare_all_usconsumerattributes
growth_medicare_consumersummary_v2
growth_medicare_consumertargetinganalyzer
growth_medicare_growthopportunityindex
growth_medicare_ifp_all_usconsumerattributes
growth_medicare_ntmtargetinganalyzer
marketintel_medicare_historicalenrollmentanalyzer
marketintel_medicare_planbenefits
memberintel_ifp_aepoepreporting_v2
memberintel_ifp_memberreporting_v2
memberintel_medicaid_memberreporting_v2
memberintel_medicare_aepoepreporting_v2
memberintel_medicare_memberreporting_v2

$repo is a string, and $growth_repo_name_list is an Array of strings: $repo是一个字符串, $growth_repo_name_list是一个字符串数组:

> $repo.GetType()

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     String                                   System.Object

> $growth_repo_name_list.GetType()

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     Object[]                                 System.Array

$repo has no hidden characters as shown by the output of the following code: $repo没有隐藏字符,如下代码的 output 所示:

> [System.Text.Encoding]::UTF8.GetBytes($repo)
103
114
111
119
116
104
95
109
101
100
105
99
97
114
101
95
99
111
110
115
117
109
101
114
116
97
114
103
101
116
105
110
103
97
110
97
108
121
122
101
114

> [System.Text.Encoding]::UTF8.GetBytes($growth_repo_name_list[5])
103
114
111
119
116
104
95
109
101
100
105
99
97
114
101
95
99
111
110
115
117
109
101
114
116
97
114
103
101
116
105
110
103
97
110
97
108
121
122
101
114

Even when I cast the Array into a flat string via double quotes it still returns False :即使我通过双引号将 Array 转换为平面字符串,它仍然返回False

> "$growth_repo_name_list"
2021.2 growth_ifp_consumersummary_v2 growth_ifp_consumertargetinganalyzer growth_medicare_all_usconsumerattributes growth_medicare_consumersummary_v2 growth_medicare_consumertargetinganalyzer growth_medicare_growthopportunityindex growth_medicare_ifp_all_usconsumerattributes growth_medicare_ntmtargetinganalyzer marketintel_medicare_historicalenrollmentanalyzer marketintel_medicare_planbenefits memberintel_ifp_aepoepreporting_v2 memberintel_ifp_memberreporting_v2 memberintel_medicaid_memberreporting_v2 memberintel_medicare_aepoepreporting_v2 memberintel_medicare_memberreporting_v2

> $ph = "$growth_repo_name_list"

> $repo -like $ph
False

> $ph.GetType()

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     String                                   System.Object

What am I doing wrong here?我在这里做错了什么? The result of the comparison should be True since the string $repo contains is in the list $growth_repo_name_list .比较的结果应该是True因为字符串$repo包含在列表$growth_repo_name_list中。 Thanks for the help in advance.我在这里先向您的帮助表示感谢。

All comparisons will yield $True , you might be comparing them wrong?所有的比较都会产生$True ,你可能把它们比较错了吗?

Extract from about_Comparison_Operators :about_Comparison_Operators中提取:

When the input is a collection, the operator returns the elements of the collection that match the right-hand value of the expression.当输入是一个集合时,运算符返回集合中与表达式右侧值匹配的元素。 If there are no matches in the collection, comparison operators return an empty array如果集合中没有匹配项,则比较运算符返回一个空数组

$repo = 'growth_medicare_consumertargetinganalyzer'

$growth_repo_name_list = @(
    '2021.2'
    'growth_ifp_consumersummary_v2'
    'growth_ifp_consumertargetinganalyzer'
    'growth_medicare_all_usconsumerattributes'
    'growth_medicare_consumersummary_v2'
    'growth_medicare_consumertargetinganalyzer'
    'growth_medicare_growthopportunityindex'
    'growth_medicare_ifp_all_usconsumerattributes'
    'growth_medicare_ntmtargetinganalyzer'
    'marketintel_medicare_historicalenrollmentanalyzer'
    'marketintel_medicare_planbenefits'
    'memberintel_ifp_aepoepreporting_v2'
    'memberintel_ifp_memberreporting_v2'
    'memberintel_medicaid_memberreporting_v2'
    'memberintel_medicare_aepoepreporting_v2'
    'memberintel_medicare_memberreporting_v2'
)

[bool]($growth_repo_name_list -like $repo)  # => True
$repo -in $growth_repo_name_list            # => True
$growth_repo_name_list -contains $repo      # => True
[bool]($growth_repo_name_list -match $repo) # => True
$growth_repo_name_list.Contains($repo)      # => True

tl;dr tl;博士

# Is the value of $repo equal to at least one element of array $growth_repo_name_list?
$repo -in $growth_repo_name_list

Note:笔记:

  • The per-element equality comparison that -in performs is case- insensitive by default, as PowerShell generally is.默认情况下, -in执行的每个元素相等比较不区分大小写,就像 PowerShell 通常一样。

  • However, you can prefix the operator name with c to make it case- sensitive ( -cin in the example above. This applies to all string-processing operator, including the ones discussed below.但是,您可以在运算符名称前加上c以使其区分大小写(在上面的示例中为-cin 。这适用于所有字符串处理运算符,包括下面讨论的运算符。


As for what you tried :至于你尝试了什么:

$repo -like $ph ( $ph being "$growth_repo_name_list" ) $repo -like $ph ( $ph"$growth_repo_name_list" )
The result of the comparison should be $true since the string $repo is in the list $growth_repo_name_list比较的结果应该是$true因为字符串$repo在列表$growth_repo_name_list

No: -like , the wildcard-matching operator , neither performs (direct) substring matching, nor does it support matching against an array of RHS strings.否: -like 通配符匹配运算符既不执行(直接) substring匹配,也不支持与 RHS 字符串数组匹配。

  • Instead, it compares the LHS - a single input string or an array of input string(s) - to a single wildcard expression on the RHS.相反,它将 LHS(单个输入字符串或输入字符串数组)与 RHS 上的单个通配符表达式进行比较。
  • If you accidentally use an array as the RHS, the array is stringified , by concatenating the (stringified) array elements with a single space between them to form a single string - this will rarely be helpful.如果您不小心将数组用作 RHS,则数组是stringified ,通过将(字符串化的)数组元素与它们之间的单个空格连接起来以形成单个字符串 - 这很少有帮助。

Therefore, the immediate fix is to reverse the operands :因此,直接的解决方法是反转操作数

# *Array* of string inputs. 
# Return those array elements that match the wildcard expression stored in $repo
$growth_repo_name_list -like $repo

This compares each element of array $growth_repo_name_list to the wildcard expression in $repo , and returns the sub-array of matching elements .这会将数组$growth_repo_name_list每个元素与 $ $repo中的通配符表达式进行比较,并返回匹配元素的子数组

Using the return array in a Boolean context (eg if ($growth_repo_name_list -like $repo)... ) translates to $false if the sub-array is empty , and $true otherwise.Boolean上下文中使用返回数组(例如if ($growth_repo_name_list -like $repo)... )如果子数组为则转换为$false ,否则转换为$true

However, $repo is just a string literal in your case (it contains no wildcard metacharacters such as * and ? ), so there is no reason to use -like - just use -eq :但是, $repo在您的情况下只是一个字符串文字(它不包含通配符元字符,例如*? ),因此没有理由使用-like - 只需使用-eq

# *Array* of string inputs. 
# Return those array elements that are case-insensitively equal to the
# string stored in $repo.
$growth_repo_name_list -eq $repo

Conversely, if you wanted to match the value of $repo as a substring of the array elements:相反,如果您想将$repo的值匹配为数组元素的substring

# *Array* of string inputs. 
# Return those array elements that case-insensitively contain the
# string stored in $repo as a substring (or in full).
$growth_repo_name_list -lik "*$repo*"

Taking a step back: It seems that you simply want to test whether the value of $repo is contained in the array stored in $growth_repo_name_list , ie if the value is, in full, equal to (at least) one of the elements of the array.退后一步:您似乎只是想测试$repo的值是否包含在$growth_repo_name_list中存储的数组中,即该值是否完全等于(至少)大批。

For that, PowerShell offers two functionally equivalent operators, -in and -contains , which differ only by operand order :为此,PowerShell 提供了两个功能等效的运算符-in-contains ,它们仅在操作数顺序上有所不同:

# Is the value of $repo equal to at least one element of array $growth_repo_name_list?
$repo -in $growth_repo_name_list

# Ditto, operands reversed.
$growth_repo_name_list -contains $repo

These operators directly return a Boolean value ( $true or $false ).这些运算符直接返回 Boolean 值( $true$false )。

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

相关问题 在我的代码中“不匹配'operator >>'”是什么意思? - What does “no match for 'operator >>'” mean in my code? Powershell:-like 运算符的神秘行为 - Powershell: enigmatic behavior of -like operator 为什么我的数组像这样 - why does my array behave like this 为什么我的数组参数在 Powershell 中被强制转换为 position 0 处的字符串? - Why is my array parameter getting forced into a string at position 0 in Powershell? 为什么我的PowerShell脚本提示您允许将项目添加到数组的权限? - Why does my PowerShell script prompt for permission to add items to arrays? 为什么它不匹配运算符操作数类型是 std:: 基本 stream 和第 45 行中的 void - Why does it give me no match for operator the operand types are std:: basic stream and void in line 45 为什么我的字符串不能很好地分裂成数组? - Why does my string not split nicely into an array? 为什么JOptionPane会干扰我的字符串? - Why does JOptionPane disturb my string? 为什么单个指针就像一个数组,并且不需要解引用运算符 (*) 来访问指向的元素? - Why does a single pointer act like an array and the dereference operator (*) isn´t needed to access the pointed element? 字符串数组不匹配 - String arrays does not match
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM