Home > Back-end >  Powershell: Why does ("111" -match "111-XD") returns false and ("111-XD&quo
Powershell: Why does ("111" -match "111-XD") returns false and ("111-XD&quo

Time:11-07

It's probably a stupid question and forgive me if it has been asked somewhere else but why does ("111" -match "111-XD") returns false and ("111-XD" -match "111") returns true is it because of the special "-" character?

It there a way to ignore the position of the operands in the comparison Thanks

CodePudding user response:

The text after -match is a regular expression to match against, and the string "111" does not contain the pattern "111-XD".

Conversely, the string "111-XD" does contain the pattern "111". This shows that the -match operation is not commutative.

CodePudding user response:

How about like this? One way is true. Parentheses are not needed because of operator precedence (note that -and and -or uniquely have equal precedence).

'111' -match '111-XD' -or '111-XD' -match '111'

True
  • Related