1

I have a script where two variables are compared, it can happen that a variable contains open brackets without closing, like in the example. Then a System.ArgumentException occur: "...not enough closing brackets.."

$test1="Testtext"
$test2="Testtext (3x(2x0,25"
if(!($test1 -match $test2)){ "test"}

how can i deal with it?

  • 1
    The `-match` operator is expecting the right-hand operand (e.g. `$test2`) to be a regular expression. In your case you are supplying an invalid RegEx. Try comparing with`-eq` instead: `if(!($test1 -eq $test2)){ "test"}` – boxdog Mar 03 '21 at 09:39

1 Answers1

1

-match performs regular expression matching - use Regex.Escape() to automatically escape any escapable sequence in a verbatim pattern string:

$text = 'Text with (parens)'
$pattern = '(par'

if($text -match [regex]::Escape($pattern)){
  "It worked!"
}
Mathias R. Jessen
  • 157,619
  • 12
  • 148
  • 206