[192] | 1 | ;+ |
---|
| 2 | ; |
---|
| 3 | ; @file_comments |
---|
| 4 | ; replace one (or more) character(s)/string(s) in a string array |
---|
| 5 | ; "modern" version of the obsolete strrepl |
---|
| 6 | ; |
---|
| 7 | ; @categories |
---|
| 8 | ; String |
---|
| 9 | ; |
---|
| 10 | ; @param STR {in}{required}{type=string array or scalar} |
---|
| 11 | ; the string to be changed |
---|
| 12 | ; |
---|
| 13 | ; @param EXP1 {in}{required}{type=scalar string} |
---|
| 14 | ; a single regular expression (as implemented by the STREGEX function). |
---|
| 15 | ; |
---|
| 16 | ; @param EXP2 {in}{required}{type=scalar string} |
---|
| 17 | ; replacement character/string |
---|
| 18 | ; |
---|
| 19 | ; @keyword FOLD_CASE |
---|
[372] | 20 | ; Indicates that the regular expression matching should be done in |
---|
| 21 | ; a case-insensitive fashion. |
---|
[192] | 22 | ; |
---|
| 23 | ; @returns |
---|
| 24 | ; string array or scalar |
---|
| 25 | ; |
---|
| 26 | ; @examples |
---|
| 27 | ; |
---|
[371] | 28 | ; IDL> abc = 'abcdefghijklmnopqrstuvwxyz' |
---|
| 29 | ; IDL> print, strsed(abc, 'm', 'M') |
---|
[192] | 30 | ; abcdefghijklMnopqrstuvwxyz |
---|
[371] | 31 | ; IDL> print, strsed(abc, 'm.*t', 'M_T') |
---|
[192] | 32 | ; abcdefghijklM_Tuvwxyz |
---|
[371] | 33 | ; IDL> a = 'abcabcabc' |
---|
| 34 | ; IDL> help, strsed([abc, a, 'eee'], 'abc', 'XXX_') |
---|
[192] | 35 | ; <Expression> STRING = Array[3] |
---|
[371] | 36 | ; IDL> print, strsed([abc, a, 'eee'], 'abc', 'XXX_') |
---|
[192] | 37 | ; XXX_defghijklmnopqrstuvwxyz XXX_XXX_XXX_ eee |
---|
| 38 | ; |
---|
| 39 | ; @history |
---|
| 40 | ; Sept 2006: Sebastien Masson (smasson\@lodyc.jussieu.fr) |
---|
| 41 | ; |
---|
| 42 | ; @version |
---|
| 43 | ; $Id$ |
---|
[232] | 44 | ;- |
---|
[327] | 45 | FUNCTION strsed, str, exp1, exp2, FOLD_CASE=fold_case |
---|
[192] | 46 | ; |
---|
| 47 | compile_opt idl2, strictarrsubs |
---|
| 48 | ; |
---|
| 49 | strout = str |
---|
| 50 | line = stregex(strout, exp1) |
---|
| 51 | line = where(line NE -1, cnt) |
---|
| 52 | IF cnt GT 0 THEN BEGIN |
---|
| 53 | FOR i = 0L, cnt-1 DO $ |
---|
| 54 | strout[line[i]] = strjoin(strsplit(strout[line[i]], exp1, /extract $ |
---|
| 55 | , /regex, /preserve_null, FOLD_CASE = fold_case), exp2) |
---|
| 56 | ENDIF |
---|
| 57 | ; |
---|
| 58 | return, strout |
---|
| 59 | END |
---|