[英]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 字符串数组匹配。
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.