http://www.perlmonks.org?node_id=573688


in reply to Re: Regex problem while parsing tagged, hierarchical data
in thread Regex problem while parsing tagged, hierarchical data

perl -pi -e 'if(/<level1(\sid=\".*\")>/){$a=$1};if(/<level2>/){$_=~s/( +<level2)>/$1$a>/g}' input or perl -pi -e '$a=m/(<level([12]))(\sid=\".*\")*>/;if($a){$2==1?$b=$3:$_ +=~s/($1)/$1$b/g;}' input or perl -pi -e '$a=m/(<level(1|2))(\sid=\".*\")*>/;$a?$2==1?$b=$3:$_=~s/( +$1)/$1$b/g:next;' input or perl -pi -e '$a=m;(<level(1|2))(\sid=\".*\")*>;;$a?$2==1?$b=$3:$_=~s;( +$1);$1$b;g:1;' input
or golf by monsieur_champs
perl -pi -e 'm;(<level(1|2))(\sid=\".*\")*>;;$&?$2==1?$b=$3:$_=~s;($1) +;$1$b;g:1;' input
sorry =) -1 char
perl -pi -e 'm;(<level(1|2))(\sid=\".*\")*>;;$&?$2==1?$b=$3:$_=~s;($1) +;$1$b;g:1' input
insane !!! 33 chars
perl -pi -e '/l1(\sid=.*)>/?$a=$1:s;l2>;l2$a>;' input

=)


Mago
mago@rio.pm.org