Reputation: 33
Parsing a text document using the code below matches every instance of '333', however I would like only the three examples below to be changed.
(Get-Content input.json) | ForEach-Object {
$_ -replace '333', '666'
} | Set-Content output.json
This should change:
This should be left unchanged:
What is the PowerShell regex solution for this?
Upvotes: 3
Views: 52
Reputation: 626893
You may use
(?i)(?<=\b(?:se)?)333\b
See the regex demo
Details
(?i)
- case insensitive modifier(?<=\b(?:se)?)
- before 333
there must be a word boundary and anoptional substrin se
333
- a literal substring\b
- a trailing word boundary.Powershell test:
PS> $s = "se333 SE333 333 1212333 3331212 333asda asd333"
PS> $s -replace '(?i)(?<=\b(?:se)?)333\b', '666'
se666 SE666 666 1212333 3331212 333asda asd333
Upvotes: 4