More regex functions - find, substring, replace #1598
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
PR on top of #1596, but this one can be merged first to show all the changes - it's in a separate PR to make code review easier.
This PR adds more regex functions:
find(string, pattern, output_vector)
: writes the start and end-index (0-indexed) of the first match in string of the first capturing group in pattern. Values can be read from the output vector as follows:var vec[2]; find("x", '(.*)', vec) ? vec[0] + vec[1] : null
. Returnstrue
if a match was found and values were written to the output vector, andfalse
otherwise.substring(string, start_idx, length?)
: returns the substring of string fromstart_idx
(0-indexed) with the specifiedlength
. If the length is not provided, returns the substring from start_idx to the end of the string, or null if the string is invalid or indices are invalid.replace(string, pattern, replacer)
: replaces the first match ofpattern
instring
withreplacer
, and returns the replaced string or the original string if no replacements were made.replace_all(string, pattern, replacer)
: replaces all matches (non-overlapping) ofpattern
instring
withreplacer
, and returns the replaced string or the original string if no replacements were made.This PR also renames
fullmatch
from #1596 tomatch_all
to remain consistent with thereplace
/replace_all
API.With this suite of Regex functions, one can easily clean and convert data from within Perspective - poorly formatted strings, dates that could not be parsed, monetary values that you want to convert to a float but cannot because of other string tokens, etc. can all be cleaned and properly converted from within Perspective's expression API.