3

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.

3
  • I think you can use ,(?=[^{}]*}) - unless there can be other } somewhere that may break the output. A more complex way is to use ({|(?!^)\G)([^,]*),(?=[^{}]*}) with $1$2~ replacement (this makes sure we only replace inside some pair of {...}). Commented Dec 1, 2015 at 20:54
  • And a pure .NET way: (?<={[^{}]*),(?=[^{}]*}). Commented Dec 1, 2015 at 21:05
  • That worked, stribizhev! Thank you so much!! Commented Dec 1, 2015 at 21:23

2 Answers 2

1

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 }.

Sign up to request clarification or add additional context in comments.

Comments

1

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.

1 Comment

Thank you. This is a much cleaner and more simple version of my original hacked split/replace/join solution! I will keep it on hand for future reference. However, if there is more than one field with curly brackets (some rows may have two), this doesn't look like it would work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.