I wrote ā€œ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')
precip001.dat

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)
PRECIP20.dat

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.