I am trying to use -replace statement to do the conditional replacement in PowerShell.
Below is my reproduceable example:
$content = @'
a<a
a>a
a'a
a"e;a
a&a
johnson&johnson
J&J
R&D
P&AB
M&ABC
O&ABCD
B&SS-IHC
S&B
GC&E
M&G
P&D
'@
I want to replace all the & with &, but if the string is ended with ; then don't replace.
The result would be:
a<a
a>a
a'a
a"e;a
johnson&johnson
J&J
R&D
P&AB
M&ABC
O&ABCD
B&SS-IHC
S&B
GC&E
M&G
P&D
So far I can only come up with $modifiedContent = $content -replace '(\w)&(\w*)[^;]|$' , '$1&$2'
and I am getting nowhere. How can I add a condition correctly that will do the trick?
Thanks in advance.
-replace '\b&(?!\w+;)(?=\w)', '&'¬ followed by any of the alternatives&(?!lt|gt|amp|quote|apos)regex101.com/r/AYlNsW/1