Author Posts

August 10, 2018 at 12:31 pm


Can you help me please with a simple regex replace script?

I have a multiline text in a text file. I want to convert this to a single line text. Then I want to search for specific regex and replace them with specific reference inside the matched regex. For example to match 'test(.+?)test' and replace with '\1'.

But I want the search for the regex to start at the beginning of the string and as it moves along the script to match and replace accordingly, one after the other match.

How can I do that?

And how can I specify multiple regex 'match and replace' pairs?

Many thanks!

August 10, 2018 at 12:55 pm

(nice user name btw. 😉 )
What you're asking for might not be rocket science but it's neither a simple regex replace script. Probably you can imagine that we will not write sophisticated ready to use code on request here in a peer to peer forum. You will have to do the majority of the work by yourself. If you have some problems with a particular piece of code you wrote you should post this code snippet here and we will try as hard as we can to help you. For the start you could either try to find something in the Powershell-Gallery or in the MS Technet Gallery. Or you use the code you can find there as an inspiration for your own code.
If you do not know how to start at all you should read the help (including the examples) for Get-Content, Foreach-Object, about_Comparison_Operators, about_Operators, about_if and about Regular Expressions.

August 10, 2018 at 2:14 pm

Hello, thanks for your reply.

I wrote this:

(gc *.txt)  -replace "regex1(.+?)", "`$1" >> out

This will read the text file and replace a regex with a reference and output the string to a file.


How can I output the reference only?

How can I write a sequential regex match operation one after the other and output each to the out file?

August 10, 2018 at 2:51 pm

Shorthand is cool and all, but I'll write it out so you can understand the logic. Get-Content returns an array of lines, so you need to loop through the lines to check each line and do the replace operation. You can collect the output by putting a variable at the beginning of the loop and all results will roll into a single file. If you want to collect results per file, then you need to use Get-ChildItem, loop through each file and place the below as the inner loop:

$output = foreach ($line in (Get-Content -Path 'C:\something.txt')) {
    $line -replace "regex1(.+?)", "`$1"