Extract with Regex Node
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
- Outputs
- Editor Settings
Inputs
Title | Data Type | Description | Default Value | Notes |
---|---|---|---|---|
Input | string | The string to match the regex against. | (required) | If the value is not a string, it will be coerced into a string before matching. |
Outputs
Title | Data Type | Description | Notes |
---|---|---|---|
Output N | string | Output ports are created, one per capture group in the regular expression path. The value is the contents of the capture group. | Dynamic count based on the number of capture groups. |
Matches | string[] | All matches of the regular expression on the string, as an array. | Contains all matched strings, ignoring capture groups. |
Succeeded | boolean | Outputs true if the regex was matched on the input string. Outputs false if it was node. | |
Failed | boolean | Outputs true if the regex did not match the input string. Outputs false if it was matched. |
Editor Settings
Setting | Description | Default Value | Use Input Toggle | Input Data Type |
---|---|---|---|---|
Error on failed | If true, the Extract With Regex node will error entirely if the input string was not found. This is useful for required matches. | False | No | N/A |
Multiline Mode | If true, the multiline flag is set on the regular expression, causing ^ and $ to match the beginning and ends of lines within the string. | False | No | N/A |
Regex | The regular expression to use for matching against the input string. | ([a-zA-Z]+) (a string of alphabetic characters) | Yes | string |
Example 1: Extract a command from an LLM response
- Create a Chat Node and pass a Prompt Node set to
System
into itsprompt
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!
- Feed the output of the Chat node into an Extract With Regex node with the following regular expression:
!([a-zA-Z]+)
- Feed the output of the
Output 1
port into another Text node to get the matched command. The output of the Text node should behello
.
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.