Is it possible to use a regular expression in a ..ReplaceStr function
Messages will contain fields with expressions like "REASON->Blood(1.23)" or "REASON->Bone(4.56)" or "REASON->Muscle Mass(7.89)". The word after the "->" can vary. I would like the outputs to be; "REASON(1.23)" or "REASON(4.56)" or "REASON(7.89)", basically removing "->" and the word or words that follow but leaving the parens and the numeric characters within.
Thanks,
Jonathan Harris
Something like this, perhaps?
Class User.Util.StringFunctions Extends Ens.Util.FunctionSet { ClassMethod ReReplace(pStr As %String, pPat As %String, pRepl As %String = "") As %String { Set tStrt = $LOCATE(pStr,pPat,,tEnd) - 1 // in case the pattern isn't found, return source string Return:(tStrt < 0) pStr Set tPrefix = $EXTRACT(pStr,1,tStrt) Set tSuffix = $EXTRACT(pStr,tEnd,*) Return tPrefix_pRepl_tSuffix } }
USER> set mystr = "REASON->Blood(1.23)" USER> set newstr = ##class(User.Util.StringFunctions).ReReplace(mystr,"->\w+") USER> write newstr REASON(1.23) USER> set altstr = ##class(User.Util.StringFunctions).ReReplace(mystr,"->\w+","-CODE") USER> write altstr REASON-CODE(1.23)
Hi,
if you make some assumptions
1. numbers are always enclosed in curved brackets
2. you want to always return string starting with REASON
this could be as easy as just:
set mystring="REASON->Blood(1.23)"
w "REASON"_$extract(mystring,$find(mystring,"(")-1,*)
or if you really want to use regex:
IRISHEALTH:USER>set mystring="REASON->Blood(1.23)" IRISHEALTH:USER>set regex=##class(%Regex.Matcher).%New("^([A-Z]*)->.*(\([0-9]*.[0-9]*\))",mystring) IRISHEALTH:USER>zw regex.Locate() 1 IRISHEALTH:USER>zw regex.Group(1) "REASON" IRISHEALTH:USER>zw regex.Group(2) "(1.23)"