replace_regex
Replace parts of text detected with a regular expression.
A regular expression (or regex, regex pattern) is a sequence of characters that forms a search pattern. This pattern is compared against texts, and any matches are substituted by a desired replacement. The replacement can be a simple (constant) text string, or a formatting pattern referencing all or parts of the matched character sequence.
Simple replacement of fixed text strings with another fixed text string can be performed easily. E.g., to
replace all occurrences of “hi” with “hello”, you’d simply use {"pattern": "hi", "replacement": "hello"}
.
However, using capturing groups in pattern
and replacement
parameters allows for much greater flexibility.
For example, if a column of texts includes twitter mentions of the form “@abc”, the regular expression
"pattern": "@(\\w*)"
will match these mentions and save the actual name without the ”@” character in a capturing
group. Using the replacement string "replacement": "{1}"
will then replace all matched mentions with only the name
part of the twitter handle, effectively removing the ”@” tags from all mentions (without removing other occurrences of
the ”@” character).
To further familiarize yourself with the regex language also see these references:
Was this page helpful?