节点 / builtin / datatransformation
Regex
Match, extract and replace text with a regular expression
此内容尚不支持你的语言。
Pull a price, an id or any other fragment out of text, rewrite text, or check whether it contains a pattern.
Try it
Section titled “Try it”Price On Page Watch:包含 6 个步骤的工作流,从 When the price changes 开始,到 Remember this price 结束。
What it does
Section titled “What it does”Regex runs a regular expression on the text of each incoming item and does one of four things, set by Operation:
- Match: finds the first match.
- Match all: finds every match and sends one item per match.
- Replace: puts Replacement in place of the match.
- Test: answers
trueorfalse.
Named groups do most of the work. A group such as (?<amount>[\d.,]+) becomes a field called amount on the outgoing item, so one Regex node can split a sentence into clean fields.
Matching runs with a 2-second time limit. A pattern that would take too long is stopped with an error instead of freezing the browser. The text can be up to 2,000,000 characters and the pattern up to 2,000.
When to use it
Section titled “When to use it”- Extract a price, order number or date from text you scraped from a page.
- Split one text field into several fields with named groups.
- Clean text, for example remove every non-digit from a phone number.
- Branch on whether text contains something: run Test, then an IF on the result. For a simple “contains” check, IF alone is enough.
Inputs and settings
Section titled “Inputs and settings”| 设置项 | 类型 | 作用 |
|---|---|---|
| Operation | Choice | What to do with the pattern. Options: Match, Match all, Replace, Test. 默认值:Match |
| Pattern | Text | The regular expression, without surrounding slashes. |
| Flags | Multiple choice | How the pattern matches. Options: Ignore case (i), All matches (g), Multiline anchors (m), Dot matches newlines (s), Unicode (u). |
| Text | Value | The text to match against. |
| Source Field | Text | Field holding the text, when the field above is left empty. |
| Replacement | Text | Text put in place of each match. Use $1 or $<name> for groups. |
| Maximum Matches | Number | Most matches collected before stopping. 默认值:1000 |
| Include Input Fields | Toggle | Keep the incoming item’s fields alongside the result. 默认值:On |
| Put Result In Field | Text | Field holding the match, the replaced text, or the test result. 默认值:match |
- Where the text comes from. Text wins if it is filled, for example with
{{ $input.description }}. Otherwise the node reads the field named in Source Field (dots reach nested fields, such asproduct.title). - Replace changes only the first match unless you turn on the All matches (g) flag. Match all always finds every match, with or without the flag.
- Replacement can reuse groups:
$1for the first group,$<amount>for a named group. - Maximum Matches can be raised to 10,000 at most.
Outputs
Section titled “Outputs”-
Output
The result goes in Put Result In Field (match by default). With Include Input Fields on, the incoming fields stay on the item too.
- Test: the result field is
trueorfalse. - Replace: the result field holds the new text.
- Match: the result field holds the first match, or
null. The item also getsmatched(trueorfalse),groups(a list of the numbered groups) and one field per named group.(?<currency>[£$€])(?<amount>[\d.,]+)on"Now $19.99"givesmatch: "$19.99",currency: "$",amount: "19.99". - Match all: one item per match, each with the same fields plus
index, the match’s position in the text. If the match count hit Maximum Matches, each item also hastruncated: true. If nothing matched, one item comes out withmatched: false.
Dependencies and credentials
Section titled “Dependencies and credentials”None. This node needs no connection and has no slots for other nodes.
Example workflow
Section titled “Example workflow”- Get HTML from Link loads a product page, and HTML Extract reads the price text into a
pricefield. - Regex with Operation Match, Source Field
priceand Pattern(?<currency>[£$€])(?<amount>[\d.,]+). - An IF checks whether
{{ $input.amount }}is below your budget, as a number. - On the true branch, a Browser Notification shows
Now {{ $input.currency }}{{ $input.amount }}.
Common issues
Section titled “Common issues”- “This is not a valid regular expression: …”. The pattern has a syntax error, often an unclosed bracket or group. Write it without surrounding slashes:
\d+, not/\d+/. - “This pattern took longer than 2000ms against this text and was stopped.” Nested repeats such as
(a+)+can take forever on some text. Make the pattern more specific, or match against less text. - “No text to match against. Fill in the Text field, or name the field holding it.” Both Text and Source Field are empty. Fill one of them.
- “Field “…” does not exist on the incoming item.“ Source Field names a field the item does not have, or it is empty. Check the spelling.
matchedisfalsebut the text looks right. Matching is case sensitive by default: add the Ignore case (i) flag. For^and$to work on every line, add Multiline anchors (m).
Recipes with this node
Section titled “Recipes with this node”Related nodes
Section titled “Related nodes”- Text / Formatter: ready-made text changes without writing a pattern.
- HTML Extract: read values from a page by CSS selector.
- Edit Fields: reshape the item after extraction.
- Code: for logic a pattern cannot express.