PowerShell Replace Regex

Question:

I have a select-string which is searching an IIS log for a particular string and returning the 2 lines above and one line below.

Results look like this:

Note the third line begins with > denoting that’s the line that select-string matched upon.

I am trying to do a -replace on the > to replace it with < font color="red">$1< /font> but my replace doesn’t seem to work.

Here’s my code:

Can any PowerShell regex gurus out there tell me why my regular expression isn’t matching?

Answer:

You should start thinking-object rather than text, because what you see is only formated object, not actual output of select-string.
Instead of parsing this output – use objects that you get (Get-Member will let you discover them).

I guess this should do what you need:

HTH
Bartek

Source:

PowerShell Replace Regex by licensed under CC BY-SA | With most appropriate answer!

Leave a Reply