1

When I run:

$SearchStr = "c:\programFiles\xyz"
Select-String -pattern $SearchStr -path $env:SystemRoot\win.ini

I get an error message stating that the string is "not a valid regular expression"

By contrast, it is working fine when $SearchStr doesn't contain special characters.

Why doesn't it work with special characters such as \?

mklement0
  • 382,024
  • 64
  • 607
  • 775
Allforone
  • 84
  • 2
  • 10
  • 2
    `\p` is the start of a [regex escape sequence](http://stackoverflow.com/questions/14891129/regular-expression-pl-and-pn). Use `$SearchStr = [regex]::Escape("c:\programFiles\xyz")` or `$SearchStr = "c:\\programFiles\\xyz"` – TessellatingHeckler Apr 21 '17 at 19:59

1 Answers1

8

If you want Select-String to match literals (literal substring matching) rather than (by default) regular expressions, use -SimpleMatch:

Select-String -SimpleMatch -Pattern $SearchStr -Path $env:SystemRoot\win.ini

By contrast, if you need to incorporate a literal into a regular expression, apply [regex]::Escape() to it, as TessellatingHeckler suggests.
E.g., to only find $SearchStr at word boundaries (\b):

Select-String -Pattern ('\b' + [regex]::Escape($SearchStr) + '\b') -Path $env:SystemRoot\win.ini

Optional background information

By default, Select-String interprets the search terms passed to parameter -Pattern as regexes (regular expressions).

In regular expressions, \ has special meaning: it is used as the escape character to modify how the regular-expression engine would otherwise interpret the following character(s).

Therefore, the \ in Windows-style paths such as c:\programFiles\xyz results in interpretation of the \ chars. as the start of an escape sequence.

Using the sample path: \p, if followed by a Unicode character category such as {L}, matches any character in that category; \x, if followed by 2 hexadecimal digits, matches a single character with that code point.

To escape \ itself, so that it is treated literally, double it: \\.

The .NET framework [regex]::Escape() method can do this for you, along with escaping any other characters that have special meaning to the regex engine.

In other words: to embed a literal string in a regular expression, pass it to [regex]::Escape() first.

Community
  • 1
  • 1
mklement0
  • 382,024
  • 64
  • 607
  • 775
  • I have used [regex]::Escape() method also but getting the same error.Could anyone suggest or answer me in $SearchStr we have given a literal path and whem we are trying to append it its not working as there are special characters in that,even though treid with simplematch but no luck..Any help will be of great use for me.. – Allforone Apr 22 '17 at 13:33
  • @DeepakPanigrahi: Without additional information we cannot diagnose your problem. However, I think the question and this answer have a well-defined scope and are therefore useful to future readers _as they are now_. Therefore, please ask a _new_ question, and try to provide an [MCVE (Minimal, Complete, and Verifiable Example)](http://stackoverflow.com/help/mcve) - feel free to notify me here once you've done so. – mklement0 Apr 22 '17 at 15:38