ossiejhmoore icon

OScript Regular Expression ReplaceAll

ossiejhmoore | PRO | 08/17/12 06:55:24 PM UTC | 0 ⭐ | 262 👁️ | Never ⏰ | []
text |

1.35 KB

|

None

|

0 👍

/

0 👎

function Assoc ReplaceAll(String input, String patturn, String replace)
 	Assoc retVal
	retVal.ok       = false
	retVal.ErrMsg   = Undefined
	retVal.ApiError = Undefined
	retVal.Input    = input
	retVal.Pattern  = patturn
	retVal.Replace  = replace
	retVal.Output   = Undefined
 	String output  = Undefined
	PatFind	finder
 	List result
	Integer start
	Integer stop
	String match
 	if ( (IsDefined(input)) && (Length(input)>0) )
		finder = Pattern.CompileFind( patturn )
		if (IsError(finder))
			retVal.ErrMsg   = Str.Format("%1", finder)
			retVal.ApiError = finder
		else
			output = ""
			result = Pattern.find(input, finder)
			if (IsDefined(result))
				while(IsDefined(result))
					start = result[1]
					stop   = result[2]
					match = result[3]
					if (start >1)
						output += input[:start-1]
					end
					output += replace
					input = input[stop+1:]
					result = Pattern.find(input, finder)
					if (IsUndefined(result))
						output += input
					end
				end
				retVal.ok     = true
				retVal.Output = output
			else
				retVal.ok     = true
				retVal.Output = input
			end
		end
	else
		retVal.ok     = true
		retVal.Output = input
	end
  	if (!retVal.ok)
		if (IsUndefined(retVal.ErrMsg))
			retVal.ErrMsg = "Unknown error while trying to execute ReplaceAll."
		end
	end
 	return retVal
 end

Comments