1

I have this stored in a text file shown below:

Java 8 Update 111
Java 8 Update 111 (64-bit)
Java 8 Update 131
Java 8 Update 151
Java 8 Update 152

I am using PowerShell to do something with this information but struggling at the first hurdle. I am using Select-String to match a pattern and it isn't working.

Select-String "\\server\JavaVersions.txt" -Pattern "Java 8 Update 111 (64-bit)"

doesn't seem to work but the below brings back both.

Select-String "\\server\JavaVersions.txt" -Pattern "Java 8 Update 111"

Does anyone have any ideas why the one with the brackets in isn't working? Is it parsing issue? I really need to be able to get the (64-bit) one before the other one and act upon it separately.

Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
d4rkcell
  • 147
  • 3
  • 4
  • 11

3 Answers3

3

You have to escape special regex characters. In your case brackets.

Select-String '\\server\JavaVersions.txt' -pattern "Java 8 Update 111 \(64-bit\)"

https://www.regular-expressions.info/characters.html

Vincent K
  • 1,326
  • 12
  • 19
2

Alternatively, use -SimpleMatch to turn off regex matching:

Select-String '\\server\JavaVersions.txt' -Pattern 'Java 8 Update 111 (64-bit)' -SimpleMatch

https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/select-string?view=powershell-5.1

Mark Wragg
  • 22,105
  • 7
  • 39
  • 68
marsze
  • 15,079
  • 5
  • 45
  • 61
1

As an alternative option, if you don't need to use a regular expression for your match, you can use the -SimpleMatch parameter:

Select-String '\\server\JavaVersions.txt' -SimpleMatch "Java 8 Update 111 (64-bit)"

You then do not need to do any regex escaping.

Mark Wragg
  • 22,105
  • 7
  • 39
  • 68