in reply to Re: Perl-Replacement of comma in between the matched Patterns
in thread Perl-Replacement of comma in between the matched Patterns

I tried the below expression. It worked for few cases and failed for other cases. Dont know the reason. First expression is to replace comma between <string>{ and } and other to replace comma between <string>[{ and }]

perl -pe 's/,(?=[^\{\}]*\d\b*\{*\})/|/g;s/,(?=[^\[\]]*\[*\{*\}*\])/|/g +;'

Replies are listed 'Best First'.
Re^3: Perl-Replacement of comma in between the matched Patterns
by 1nickt (Canon) on Jul 08, 2015 at 10:54 UTC

    You still haven't explained what "failed in other cases" means.

    It's hard to read in your one-liner (have you tried adding the /x flag to your regexp which allows you to write it over multiple lines, with comments?), but in your second statement it sure seems like you are replacing anything that matches ,(?=[^\[\]]*\[*\{*\}*\]) with | ... you probably need to learn about extracting matches, too.

    Remember: Ne dederis in spiritu molere illegitimi!

      Many thanks for your inputs and advice. I dont have much expertise on the Perl Regular Expression. I need to do this using substitute operator, So I am looking for help. Let me put my requirement in a simple way.

      (1,2,4,{8,9,xy{4343,0943},pq[{434,5454}]},4,6)

      Replacement #1

      Need to replace commas in the pattern xy{4343,0943} with pipe to get xy{4343|0943}

      Replacement #2

      Need to replace commas in the pattern pq[{434,5454}] with pipe to get pq[{434|5454}]

      Please suggest