0

String being searched

"ran into (something-big) issues do something else bla bla, see log file: "

Pattern should find "(something-big) issues" exact substring, if it exists.

How do I allow parens in the regex pattern

This pattern i've tried fails

\b\(something-big\) issues\b

eg.

$str2 = "ran into (something-big) issues do something else bla bla, see log file: ";


if($str2 -match '\b\(something-big\) issues\b' ) {
    Write-Output "we found it";    
}
else{
    Write-Output "nope";
}   
bitshift
  • 6,026
  • 11
  • 44
  • 108

1 Answers1

3

Your regex pattern correctly returns $false for your input because of:

'\b\(something-big\) issues\b'
# ^ this guy

( is not a word character, and neither is the space that precedes it, so the index of ( is not actually a word-boundary - remove the first \b and it'll work:

$str2 -match '\(something-big\) issues\b'

If you only want to match when a non-word character is present in front of (something-big), use the negated word-character \W (notice W is upper case):

$str2 -match '\W\(something-big\) issues\b'

or use a negative lookbehind:

$str2 -match '(?<!\w)\(something-big\) issues\b'
Mathias R. Jessen
  • 157,619
  • 12
  • 148
  • 206