Search code examples
regexidl

Use regex in IDL to escape special character in directory name


I am trying to run a model on a cluster but IDL is throwing an error when using openw because the file locations name has [] in it (this is my conclusion after some testing). What I'd like to do is feed IDL a slightly different string that includes escape characters. I believe one way to do this is with regex but I could use some help and specifically don't know much IDL.

temp_dir='/local/scratch/1940320[2000].cluster.name/temp/area'
openw,12,temp_dir+'file.dat'

How would I send:

temp_dir2='/local/scratch/1940320\[2000\].cluster.name/temp/area'
openw,12,temp_dir2+'file.dat'

The number represents the jobid on the cluster, and I don't know this until its running. /local/scratch/$PBS_JOBID.cluster.name is held in $TMPDIR which I'm getting with getenv('TMPDIR') Thanks!


Solution

  • Well, it's a bit awkward, but this should work:

    IDL> temp_dir = '/local/scratch/1940320[2000].cluster.name/temp/area'
    IDL> temp_dir = mg_streplace(temp_dir, '(\[|\])', '\\$1', /global)
    IDL> print, temp_dir
    /local/scratch/1940320\\[2000\\].cluster.name/temp/area
    IDL> temp_dir = mg_streplace(temp_dir, '\\\\', '\', /global)
    IDL> print, temp_dir
    /local/scratch/1940320\[2000\].cluster.name/temp/area
    

    MG_STREPLACE is available on GitHub.