Wednesday, July 22, 2009

Replacing class checks

GenABEL's checks for passed objects class did not comply with S4 (thanks to Maxime Rotival who noticed that). So I had to replace all checks like

if (class(a)=="xxx") ...
if (class(a)!="xxx") ...

To

if (is(a,"x")) ...
if (!is(a,"x")) ...

Vim syntax:

:s/class *( *\([a-zA-Z.-]\+\) *) *== *"\([a-zA-Z.-]\+\)"/is(\1,"\2")/g
:s/class *( *\([a-zA-Z.-]\+\) *) *!= *"\([a-zA-Z-.]\+\)"/!is(\1,"\2")/g

Sed syntax:

sed -e 's/class *( *\([a-zA-Z.-]*\) *) *== *"\([a-zA-Z.-]*\)"/is(\1,"\2")/g' ...
sed -e 's/class *( *\([a-zA-Z.-]*\) *) *!= *"\([a-zA-Z.-]*\)"/!is(\1,"\2")/g' ...

(note that here you need to have "-" last!)

Perl script to replace that in all files:


@files = `ls`;
foreach (@files) {
chomp;
`sed -e 's/class *( *\\([a-zA-Z.-]*\\) *) *== *"\\([a-zA-Z.-]*\\)"/is(\\1,"\\2")/g' $_ > $_.tmp1`;
`sed -e 's/class *( *\\([a-zA-Z.-]*\\) *) *!= *"\\([a-zA-Z.-]*\\)"/!is(\\1,"\\2")/g' $_.tmp1 > $_.tmp2`;
`mv $_.tmp2 $_; rm $_.tmp1 $_.tmp2`;
}


(I know it is not really perl, but it works -- any suggestions for a nicer way to do that are welcome)

Question: can one think of regexp which will do that in a single match?

0 comments:

Post a Comment