I wrote “Making Regular Expressions Your Friends” “Making Regular Expressions Your Friends” while I was at RSI (and while it was still RSI). Regular expressions are a “little language” embedded inside IDL (and most other modern languages) with very powerful string processing capabilities. In the whitepaper, all the details of IDL’s implementation of regular expressions are explained with examples (including the undocumented stuff). Also included in the download is the STR_REPLACE routine (download).

STR_REPLACE can do search and replace actions on strings. For example,

IDL> print, str_replace('data001.dat', 'data', 'precip')

replacing ‘data’ with ‘precip’ in ‘data001.dat’. The cool part about it is you can replace one regular expression with another, even evaluating IDL expressions in the process. For example,

IDL> search = '([[:alpha:]]+)([0-9]+)'
IDL> replace = 'strupcase(''$1'') + string(fix($2), format=''(Z0)'')'
IDL> print, str_replace('precip032.dat', search, replace,  /evaluate) 

Read the whitepaper whitepaper if that’s a bit too dense to follow.

UPDATE 1/22/14: Updated links.

UPDATE 1/23/14: I recommend using MG_STREPLACE instead of the STR_REPLACE shown above.