TRegEx
Performs Find and Replace / ReplaceAll on strings using Perl Compatible Regular Expressions.
Methods
Method ReplaceAll:String(target:String, replaceWith:String, startPos:Int = 0)
Replaces all occurances of the search Pattern with replaceWith on target, from startPos.
Doesn't affect the original target contents.
Returns
The newly replaced string.
Example
' Replaces all float/numeric values with '!GONE!'
SuperStrict
Framework Text.RegEx
Import BRL.StandardIO
Local change:String = "floats are 4.533, -10.232, 1446.2003 and even 100~n" + ..
"If it wasn't enough 5 out of 10 applications said their users preferred them. That's 0.50 !~n"
Local regex:TRegEx = TRegEx.Create("[-+]?[0-9]*\.?[0-9]+")
Try
Local s:String = regex.ReplaceAll(change, "!GONE!")
Print "before = " + change
Print "after = " + s
Catch e:TRegExException
Print "Error : " + e.toString()
End
End Try
Print "Done."
Method Replace:String(target:String, replaceWith:String, startPos:Int = 0)
Replaces the first occurance of the search Pattern with replaceWith on target, from startPos.
To access a specific subquery during the replace, you can use the \n syntax in replaceWith, where \0 refers to the whole match, and \1 refers to the first subquery/group, and so on. (see test_07 for an example).
Doesn't affect the original target contents.
Returns
The newly replaced string.
Example
' Replaces 'bb' for 'd'
SuperStrict
Framework Text.RegEx
Import BRL.StandardIO
Local change:String = "yabba dabba doo"
Local regex:TRegEx = TRegEx.Create("bb")
Try
Local s:String = regex.Replace(change, "d")
Print "before = " + change
Print "after = " + s
Catch e:TRegExException
Print "Error : " + e.toString()
End
End Try
Print "Done."
Method Find:TRegExMatch(target:String = Null, startPos:Int = -1)
Performs a search on the given target from startPos, using the search Pattern.
Both parameters are optional.
If target is not set, the search will use the previous target.
You will want to set target the first time this method is called.
If you call this method with no parameters it will start the search from the end of the last search, effectively
iterating through the target string.
Returns
A TRegExMatch object or Null if no matches found.
Example
' Search for a floating point number in a string
SuperStrict
Framework Text.RegEx
Import BRL.StandardIO
Local floats:String = "floats are 4.533, -10.232, 1446.2003 and even 100"
Print "Original : " + floats + "~n"
Local regex:TRegEx = TRegEx.Create("[-+]?[0-9]*\.?[0-9]+")
Try
Local match:TRegExMatch = regex.Find(floats)
' get each match, and print it out.
While match
Print "Found : " + match.SubExp()
match = regex.Find()
Wend
Catch e:TRegExException
Print "Error : " + e.toString()
End
End Try
Print "Done."
Functions
Function Create:TRegEx(searchPattern:String, options:TRegExOptions = Null)
Creates a new TRegEx object.
searchPattern is the regular expression with which to perform the search.
options sets the global TRegExOptions. Overrides all previous options.
Function Config:Int(what:Int, where_:Int Var)
Returns which optional features are available.