NewbieExamples
NewbieExamples

Reputation: 33

How can I replace specifc characters only within a substring in powershell

I am trying to process a file that is comma-delimited. Within that file, one of the fields contains commas. I have no control over the output of the file, but the field containing the commas is conveniently surrounded by curly brackets. I need to find a way in Powershell (v3) to replace the commas that exist ONLY between the curly brackets. I have tried to do a split/replace and RegEx to no avail.

Here is an example of the data:

"[email protected]","Text-exists-here()","","123456789","11/01/2015","{"ProblemSpot":"","SubstringField1":"Related Text","SubstringField2":"","SubstringFieldn":"MoreRelatedText"}","MoreData","LastField",

What I would like is an output like this:

"[email protected]","Text-exists-here()","","123456789","11/01/2015","{"ProblemSpot":""~"SubstringField1":"Related Text"~"SubstringField2":""~"SubstringFieldn":"MoreRelatedText"}","MoreData","LastField",

I've tried RegEx:

(gc $file) | -replace "\{.+?(,+?).+\}", "|" | set-content $file

And I've tried a rather long, ugly, hacked split/replace/join solution that works in Powershell v4, but not v3. If possible, I'd love a simple/clean solution to do a replace within the substring.

Upvotes: 3

Views: 614

Answers (2)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626689

In a .NET regex, you can make use of a variable-width look-behind and a lookahead:

(?<="{[^{}]*),(?=[^{}]*}")

See regex demo (replace with ~ or any other characters of your choice)

The regex matches any , that is preceded with "{ and then any number of characters other than { and }, and that is followed with any number of characters other than { and } up to }".

Another regex you can use:

,(?=[^{}]*})

This will match any comma that is followed by any number of characters other than { or } up to }.

Upvotes: 1

mjolinor
mjolinor

Reputation: 68243

One option:

$text = 
'"[email protected]","Text-exists-here()","","123456789","11/01/2015","{"ProblemSpot":"","SubstringField1":"Related Text","SubstringField2":"","SubstringFieldn":"MoreRelatedText"}","MoreData","LastField",'

$parts = $text.split('{}')

"$($parts[0]){$($parts[1].replace(',','~'))}$($parts[2])"

"[email protected]","Text-exists-here()","","123456789","11/01/2015","{"ProblemSpot":""~"SubstringField1":"Related Text"~"Substri
ngField2":""~"SubstringFieldn":"MoreRelatedText"}","MoreData","LastField",

That will split the line at the curly braces so you can isolate the section you need to do the replace on, do the replace, then re-assemble it restoring the curly braces. No regex, just simple literal text manipulations.

Upvotes: 1

Related Questions