Using Powershell, I know how to search a file for a complicated string using a regex, and replace that with some fixed value, as in the following snippet:
Get-ChildItem "*.txt" |
Foreach-Object {
$c = ($_ | Get-Content)
$c = $c -replace $regexA,'NewText'
[IO.File]::WriteAllText($_.FullName, ($c -join "`r`n"))
}
Now I'm trying to figure out how to replace a subsection of each match of a regex. Can this be done in one smooth step like above? Or do you have to extract each match of the larger regex, search and replace within it, and then somehow stick that result back into the original text?
To clarify with an example, suppose that in the following test text I want to find only the 14xx-numbered instances like "TEST=*1404" in the following text, and replace the 14xx with 16xx?
A 2180 1830 12 0 3 3 TEST=C1404
A 900 1830 12 0 3 3 TEST=R1413
A 400 1830 12 0 3 3 TEST=R1411
A 1090 1970 12 0 3 3 TEST=U1400
A 1090 1970 12 0 3 3 TEST=CSA1400
A 1090 1970 12 0 3 3 TEST=CSA1414
A 1090 1970 12 0 3 3 TEST=CSA140
A 1090 1970 12 0 3 3 TEST=CSA14001
A 1090 1970 12 0 3 3 TEST=CSA17001
I.e. I'd like the resulting text to be as follows, where you'll note that only the first 6 lines should change:
A 2180 1830 12 0 3 3 TEST=C1604
A 900 1830 12 0 3 3 TEST=R1613
A 400 1830 12 0 3 3 TEST=R1611
A 1090 1970 12 0 3 3 TEST=U1600
A 1090 1970 12 0 3 3 TEST=CSA1600
A 1090 1970 12 0 3 3 TEST=CSA1614 <- Second instance of '14' shouldn't change
A 1090 1970 12 0 3 3 TEST=CSA140 <- Shorter numbers shouldn't change
A 1090 1970 12 0 3 3 TEST=CSA14001 <- Longer numbers shouldn't change
A 1090 1970 12 0 3 3 TEST=CSA17001
The following regex seems to do the job of finding the larger strings where I need to make replacements, but I don't know what functionality in Powershell (replace
?) to use to just replace the substring of the results. Also, feel free to suggest a better regex if that would help.
$regexA = "\bTEST=\b[A-Za-z]+14\d\d\r"
I'd rather not have to hard-code an exhaustive list of the stuff that can come between the '=' and the numbers, like 'R', 'C', "CSA", etc.
I've been working on something for an hour or so where I get all the matches for the regex, search within them to replace 14 with 16, then run replace on the original text with the old and new values, e.g. replace($myText,"TEST=CSA1400","TEST=CSA1600")
, but this is not covering off the special cases very well, and it feels like I'm heading down the rabbit-hole.