TextSubstitution takes a Perl5 substitution expression and parses it into
the Pattern part and the Substitution part. It also supports something
like "s/a/b/gm" where m means applying the substitution on each lines.
Its method of substitute() applies the Perl5 substitution on an input
string and returns the result. It supports the substitution between
LineFeed (\n) and CarrigeReturn (\r). It also supports the substitution like
"s/a/c/g" or "s/c/b/g" where c is the hexdecimal char, such as \x02.
It also supports expressions in a different way as compared to Perl5.
In this case, the substitution expression is supposed to be something like
"s//?=xxx/e" where 'e' at the end indicates the expression substitution.
It is assumed that the text in the context will be treated as the input
string which will be parsed into whatever type of data. When you call the
method of substitute(), please make sure to check the returned string.
It should never be null nor empty. Otherwise, something is wrong. The
caller is supposed to log the details or to throw exception with details.
Here lists all supported exressions:
Supported Expressions
Expression | Operation | Type | Example |
s//+=n/e | Add | Number | s//+=3/e |
s//-=n/e | Substract | Number | s//-=3/e |
s//*=n/e | Multiply | Number | s//*=3/e |
s///=n/e | Devide | Number | s///=3/e |
s//%=n/e | Modulate | Integer | s//%=3/e |
s//~=s/e | Parse | Time Pattern | s//~=MMM d HH:mm:ss yyyy/e |
s//#=s/e | Format | Time Pattern | s//#=yyyy-MM-dd HH:mm:ss.SSS/e |
s//?=s/e | Condition | String | s//?=[0,100]15/e |
s//:=lower/e | toLowerCase | String | s//:=lower/e |
s//:=upper/e | toUperCase | String | s//:=uper/e |
s//:=encode/e | URLEncode | String | s//:=encode/e |
s//:=decode/e | URLDecode | String | s//:=decode/e |
s//:=time/e | CurrentTimeMillis | String | s//:=time/e |
s//:=rand/e | Random Number | Number | s//:=rand 1.0/e |
s//:=pow d/e | Power | Real Number | s//:=pow 0.5/e |
s//:=sqrt/e | Square Root | Number | s//:=sqrt/e |
s//:=abs/e | Absolute Value | Number | s//:=abs/e |
s//:=eval/e | Evaluation | Number or String | s//:=eval/e |
s//:=md5/e | MD5 Checksum | String | s//:=md5/e |
s//:=chop/e | Chop | String | s//:=chop/e |
s//:=replace d/e | Search Replace | String | s//:=replace !/e |
s//:=sub d/e | regex replaceFirst | String | s//:=sub !/e |
s//:=gsub d/e | regex replaceAll | String | s//:=gsub !/e |
For eval(), it applies to a template with a simple numeric expression for
numbers or a ternary expression on numbers or single quoted strings. In case
of string, the result string will have the quotes trimmed off.
For replace(), the following parameter, d, is the delimiter used to parse
the text of the template. The text of the template should contain 3 parts
delimitered by the delimiter, d. The first part is the original text to be
processed. The second part is the string to search for in the first part.
The last part is the string as the replacement. For sub() or gsub(), it
replaces either the first regex match or all the regex maches with the
replacement. So the second part is a regex string.
The constructor may throw any errors in initialization of the substitution.