Skip to main content

Extract with Regex Node

Extract with Regex Node Screenshot

Overview

The Extract With Regex (or Extract Regex) node allows you to extract one or more strings from a string using a regular expression.

Each capture group in the regular expression will correspond to an output port on the node, so the number of outputs will be equal to the number of capture groups in the regular expression.

The Extract With Regex node can be used for many cases such as parsing a response from an LLM to extract specific values, or parsing a string from a file to extract specific values.

If you do not have any capture groups in your regex, you can still use the Matches, Succeeded, and Failed outputs of the node to determine if the regex matched the input string.

Inputs

TitleData TypeDescriptionDefault ValueNotes
InputstringThe string to match the regex against.(required)If the value is not a string, it will be coerced into a string before matching.

Example 1: Extract a command from an LLM response

  1. Create a Chat Node and pass a Prompt Node set to System into its prompt input, with something like the following:
    Your reply can initiate commands, for example `!hello` will cause "Hello world" to appear for the user. Try it out now!
  2. Feed the output of the Chat node into an Extract With Regex node with the following regular expression:
    !([a-zA-Z]+)
  3. Feed the output of the Output 1 port into another Text node to get the matched command. The output of the Text node should be hello.

Extract with Regex Example 1

Error Handling

If the input string is not provided, the node will error. If the input is not a string, it will be coerced into a string before matching.

If the regular expression is invalid, the node will error.

If the Error on failed setting is enabled, the node will error if the regular expression did not match the input string.

FAQ

Q: How do I match a string that contains a special character?

Special characters such as a newline cannot be escaped in the regular expression editor, however you can put literal newlines in the regular expression in the editor, and they will be preserved. So to match a new line, put a new line in the regular expression editor.

See Also