S0B0L
S0B0L

Reputation: 25

Powershell regex multiline find and replace

I have a question very similar to Multiline Regex in PowerShell but I cant seem to get my expression to work. much like the other question i need to parse through a file and replace the things i want. my problem is that the thing i am looking for is not a fixed string but a multiline log entry like this:

$log_number
$entry
$tag

I need to replace every entry with a selected tag with a new string. So far I have

(Get-Content $($parent_dir + $old_name + ".log")) | 
    Out-String | 
    ForEach-Object {$_ -replace "", "This is a test"} | 
    Set-Content $($parent_dir + $new_name + ".log")

but I cant seem to find a regex query that will match the format I am looking for. I tried to modify one that I found on this question: Multiline regex to match config block

'(?smi)([0-9]{1, }(\s*$)){2}.*?S0B0L'

But that doesn't seem to be working. I am completely stumped and any help would be appreciated!

Example:

39983
"set x 1"
random_user

39984
"set x 45"
S0B0L

39985
"set x 23"
random_user

Becomes:

39983
"set x 1"
random_user

This is a test

39985
"set x 23"
random_user

Upvotes: 1

Views: 1218

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626690

I am almost sure the problem is with your regex that is looking for just numbers and whitespace 2 times, then any characters including newlines, and then S0B0L.

The regex that will match a 3-line strip of text the first line of which consists of numbers, and the 3rd line of which is S0B0L:

(?i)[0-9]+[\r\n]+.*[\r\n]+S0B0L

See demo.

Upvotes: 1

Related Questions