±regex
Modifies a CSV table field content using a regular expression
See also https://wikipedia.org/wiki/Regular_expression.
The source expression, the pattern and the replacement are each separated by a separator (mostly a "/"). So this is a three-part combination.
Please note that, in order to avoiding confusion, inside each part of the combination a literal slash must be escaped with a backslash, like so: \/ .
When giving multiple Regex expressions it might also be necessary to protect any embedded comma with a backslash, like so: \, . This is because multiple Regex expressions for multiple affected fields are also concatenated using commas. So any literal comma inside an expression must be escaped.
The starting part can be a literal string, some field content, or a more complex expression. It will be resolved before Regex pattern matching takes place. If you want to just modify the current field itself, without external components, you can just use [*] as the expression part or simply omit it (so that the argument starts with
The second part is a common Regex pattern that follows the usual regex conventions.
Therefore, this Regex pattern will not be resolved according to the CSVfox expression resolving rules, because it has to be handled solely by the Regex automatic.
The third part describes the replacement pattern that finally will be inserted as the the new fields content. The whole captured Regex group can be back-referenced with
Whether
Finally, this generated replacement part will also be seen as a resolvable expression, and becomes resolved.
Modifiers
If the regex modifier /i is used, the pattern matching will be case-insensitive.
If the regex modifier /m is used, the pattern matching will use multi-line mode. The
If the regex modifier /s is used, the pattern matching will use single-line mode. This changes the
If the regex modifier /e is used, the pattern matching will return only explicitly named groups, instead of all groups.
Pattern
±regex[Field]=/Pattern/Replacement
±regex[Field]=Expression/Pattern/Replacement
±regex/i[Field]=Expression/Pattern/Replacement
Usage examples
some examples here ...Correcting trailing minus signs
This moves all trailing minus signs of an amount field to the front, if the fields contain only a decimal number, a minus sign, and optionally some whitespace around:It does the following:
Expression:
The first part
Pattern:
The regex pattern between the two slashes describes :
- the beginning of the string (^),
- perhaps some whitespace (\s*),
- a group of numbers and separators (in brackets, i.e.group 1),
- a minus sign (in brackets, i.e. group 2),
- again perhaps some whitespace (\s*),
- and the string end ($).
Replacement:
When the current field content matches this description, it will be replaced by
first with group 2 (\2, containing the minus sign)
and then group 1 (\1, containing the numbers).
So now the field will consist of first the minus sign, and then the numbers.
The whitespace has gone because it was not captured in groups and inserted back into the replacement expression.
Summing up Digits of a Number
This sums up all digits of the year of birth Birth into a field SumOfBirthDigits.Expression:
The first part
Pattern:
The pattern
Replacement:
The content