Replace all occurrences of a string/pattern¶
Goal¶
Replace all occurrences of a string (or pattern) in a text with another string.
Prerequisites¶
Some text has been imported in Orange Textable (see Cookbook: Text input) and possibly further processed (see Cookbook: Segmentation manipulation).
Procedure¶
- Create an instance of Recode on the canvas.
- Drag and drop from the output connection (righthand side) of the widget instance that emits the segmentation to be modified (e.g. Text Field) to the Recode instance’s input connection (lefthand side).
- Open the Recode instance’s interface by double-clicking on its icon on the canvas.
- In the Substitution section, insert the string that will be replaced in the Regex field.
- In the Replacement string field insert the replacement string.
- Click the Send button (or make sure the Send automatically checkbox is selected).
- A segmentation containing the modified text is then available on the Recode instance’s output connections; to display or export it, see Cookbook: Text output.
Comment¶
- In the Regex field you can use all the syntax of Python’s regular expression (cf. Python documentation).
- In our example, we choose to replace all occurrences of British -our with
American -or (for example, from colour to color); unless otherwise
specified (typically using word boundary “anchor”
\b
), replacements will also occur within words, i.e. coloured to colored.