strings/
mg_streplace.pro
includes main-level programRoutines
top mg_streplace
result = mg_streplace(str, pattern, replacement [, /evaluate] [, /fold_case] [, /global])
Handle string replacment with regular expressions.
Return value
string
Parameters
- str in required type=string
a string to search for expressions and replace them
- pattern in required type=string
a regular expression possibly using subexpressions; see IDL's online help for STREGEX for help on regular expressions
- replacement in required type=string
the string to replace matches of the "pattern"; can use $1, $2, etc. to refer to subexpressions in "pattern"
Keywords
- evaluate in optional type=boolean
set to evaluate the "replacement" as a IDL expression instead of just a string.
- fold_case in optional type=boolean
set to make a case insensitive match with "pattern"
- global in optional type=boolean
set to replace all expressions that match
- start out optional private type=integral default=0
index into string of where to start looking for the pattern
Examples
The following example demonstrates basic operations of MG_STREPLACE
,
simply replacing "was" with "was not" in the expression "Mike was here":
IDL> print, mg_streplace('Mike was here', 'was', 'was not')
Mike was not here
IDL> print, mg_streplace('Mike was here', '([^ ]*) ([^ ]*)', '$2 $1')
was Mike here
IDL> s = 'MikeGeorgeHenryMikeBill'
IDL> re = 'Mike([A-Z][a-z]*)'
IDL> expr = '"Mike" + strupcase($1)'
IDL> print, mg_streplace(s, re, expr, /evaluate, /global)
MikeGEORGEHenryMikeBILL
IDL> re = 'Mike([0-9]+)'
IDL> expr = 'fix($1) * 2'
IDL> help, mg_streplace('Mike5', re, expr, /evaluate)
<Expression> LONG = 10
IDL> str = '1874382735872851'
IDL> re = '^[+-]?([[:digit:]]+)([[:digit:]]{3})'
IDL> for i = 0, strlen(str) / 3 - 1 do $
IDL> str = mg_streplace(str, re, '$1,$2', /global)
IDL> print, str
1,874,382,735,872,851
Author information
- Author:
Michael Galloy
File attributes
Modification date: | Wed May 14 15:07:14 2014 |
Lines: | 76 |
Docformat: | rst rst |
Contact me if you have enhancement requests or bug fixes.