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).

Ingredients

Widget Recode
Icon recode_icon
Quantity 1

Procedure

Replace all occurrences of a string with the Recode widget

Figure 1: Replace all occurrences of a string with an instance of Recode.

  1. Create an instance of Recode on the canvas.
  2. 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).
  3. Open the Recode instance’s interface by double-clicking on its icon on the canvas.
  4. In the Substitution section, insert the string that will be replaced in the Regex field.
  5. In the Replacement string field insert the replacement string.
  6. Click the Send button (or make sure the Send automatically checkbox is selected).
  7. 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.