regex - extraction perl -


i have file 1.txt below content

f1,f2,[as1,as2],[as3,as4]

f3,f4,f5,[as5,as6]

i require write regex in perl should change delimiter ',' inbetween [' , '] | .

i tried below, did not work.

@qr = $st = /\{(.*)(\|)+\}/; 

where $st has input string.

for versions of perl older perl 5.14, can't use regex like

echo "[as1,as2,as3],[as4,as5]" | perl -lpe 's/(?:\[|\g(?!^))[^]]*?\k,/|/g' 

see online demo.

pattern details:

  • (?:\[|\g(?!^)) - either literal [ or end of previous match (\g(?!^))
  • [^]]*? - 0 or more chars other ], few possible, matched
  • \k - whole match value dropped
  • , - comma lands in match (that replaced |).

for perl version 5.14 , newer (where r modifier appeared), may match [...] substrings \[[^][]+] regex (that matches [, 1+ chars other [ , ], , ]) , perform replacements inside these matches:

echo "f1,f2,[as1,as2],[as3,as4]" | perl -lpe 's/(\[[^][]+])/$1=~s#,#|#gr/ge' # => f1,f2,[as1|as2],[as3|as4] 

see online demo.


Comments

Popular posts from this blog

Command prompt result in label. Python 2.7 -

javascript - How do I use URL parameters to change link href on page? -

amazon web services - AWS Route53 Trying To Get Site To Resolve To www -