Reputation: 4636
I am parsing a string into XML with powershell:
NODE IP 0.0.0.0 "APXPRD"
And I need to got:
<NODE_IP>0.0.0.0 "APXPRD"</NODE_IP>
I try to use regexp, but cant catch how to replace ' ' to '_' only if it between words in all capital letters, any advice?
I Try like that regexp:
$textis = 'NODE IP 0.0.0.0 "APXPRD"'
$textnew = $textis.replace('/^\s*[A-Z]+(?:\s+[A-Z]+)/m', '_')
but that seems not work :/
Upvotes: 0
Views: 79
Reputation: 16089
You can search for:
((?:^|\s)[A-Z]+)\s([A-Z]+(?:\s|$))
And replace it with:
$1_$2
This will look for a string with uppercase letters ([A-Z]
) which are directly after a whitespace (\s
) or the beginning of the string (^
). Then, a whitespace is necessary in between and at the end, it matches again for a whitespace (\s
) or the string end ($
).
If you are using PowerShell to replace it, you need to do it like this (take care about this: the command of case-sensitive matching is creplace
not just replace
):
$textis = 'NODE IP 0.0.0.0 TEST String "APXPRD"'
$textnew = $textis -creplace '((?:^|\s)[A-Z]+)\s([A-Z]+(?:\s|$))','$1_$2'
Upvotes: 1
Reputation: 1413
(?<=[A-Z]) (?=[A-Z])
Will get you the spaces between capital letters.
Note the space in the middle. It's using lookbehind and lookahead
Upvotes: 1