regex - How to delete all occurrences (0 or many) of a character between two strings in SAS -


i trying parse .json file sas. in order deal lists in .json file, remove commas between [item1, item2, item3, .... itemn], keep commas not within [].

i think should able using prxchange regular expression...i can working 2 item list, can't figure out how alter work lists of different amounts.

newvariable=prxchange('s/(\[\w+),(\w+\])/$1 $2',-1,oldvariable);

examples:

oldvariable = "{"hospital": "nop", "drugs": ["penicillin", "ampicillin", "cephalosporin"]}"  newvariable = "{"hospital": "nop", "drugs": ["penicillin" "ampicillin" "cephalosporin"]}"   oldvariable = "{"hospital": "kop", "drugs": ["tetracycline"]}"  newvariable = "{"hospital": "kop", "drugs": ["tetracycline"]}"  

maybe there better way approach this...

sometimes easiest way handle regex break steps. in case, first array out, replace commas spaces:

data _null_; oldvariable = '{"hospital": "nop", "drugs": ["penicillin", "ampicillin", "cephalosporin"]}'; arrayexpr=prxparse( '/\[[^]]+\]/' ); call prxsubstr( arrayexpr, oldvariable, position, length ); put position length; newvariable=cat(     substr( oldvariable, 1, position - 1 ),     prxchange( 's/, / /', -1, substr( oldvariable, position, length ) ),     substr( oldvariable, position + length ) ); put newvariable; run; 

your original regex had problems well. of many regex-helper sites this 1 favorite.


Comments

Popular posts from this blog

javascript - Count length of each class -

What design pattern is this code in Javascript? -

hadoop - Restrict secondarynamenode to be installed and run on any other node in the cluster -