简体   繁体   English

类似Powershell的算子不是可交换的

[英]Powershell -like operator is not commutative

Consider the following: 考虑以下:

$var = 'Now is the time'
if ($var -like 'Now*') { 'true' } else { 'false' }

Output: true 输出: true

Now swap the two operands for the -like operator: 现在将两个操作数交换为-like运算符:

if ('Now*' -like $var) { 'true' } else { 'false' }

Output: false 输出: false

The -like operator is not commutative . 类似的运算符不是可交换的 Is this as expected? 这是预期的吗?

I am on Win 7, using PS version 2. Thanks. 我在Win 7上,使用PS版本2.谢谢。

-Like behaves not commutative - this is expected. -Like行为不可交换 - 这是预期的。

Some points: 一些要点:

  1. A summary of the discussion above in the comments: Commutativity would introduce ambiguity: 'Hello.*' -like 'Hello?' 评论中上述讨论的摘要:交换会引入歧义: 'Hello.*' -like 'Hello?' - would this return true or false were -like commutative? - 这会返回truefalse是否-like交换一样? Therefor wildcard characters are only allowed on the right side. 因此,只允许在右侧使用通配符。 If they appear on the left size, they are considered literals - not wildcards. 如果它们出现在左侧大小,则它们被视为文字 - 而不是通配符。

If someone else has other valid points: feel free to edit or give a better answer. 如果其他人有其他有效点:随时编辑或给出更好的答案。

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

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