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.

  2. Drag and drop from the output (righthand side) of the widget that emits the segmentation to be modified, here Text Field (Hamlet), to the input of Recode (lefthand side).

  3. Double-click on the icon of Recode to open its interface.

  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 tick the Send automatically checkbox.

  7. A segmentation containing the modified text is then available at the output of Recode; 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. Regular expressions).

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

See also