lunedì 12 luglio 2010

Fun with RegExp [ENG]

Today it is another of those Regular Expressions Appreciation Days I sometimes like to hold. RegExp are not beautiful nor elegant, but they are very powerful.
Let's say you have a batch file which begins setting some variables:
set REGASM_HOME=c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727
set V_HOME=c:\programmi\v
set V_HOME=c:\programmi\v
set DWVF_DLL=D.V.dll
set TEMPDIR=c:\dev\_deploy
set VFAP_DLL=VPOD.dll
If you need to echo the stuff for debugging purposes (and don't want to use a SET command) you must add as many lines like this:
echo REGASM_HOME=%REGASM_HOME%
Putting that all in by hand, even with smart cut & paste, would require time and is prone to errors (and it is so not cool, is it now?). What about a good editor and some regular expressions? I copied the above lines and applied these search & replace patterns:
search for: set (\w*)(=)(.*)$
replace with: echo $1=%$1%
et voilà, les jeux sont fait!
If you want to toy with regular expressions try Regex Coach: it is a very useful tool.

Nessun commento: