1

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
0

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! Also that demo website is fantastic.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.