# Apply Regex

The **Apply Regex** building block allows you to perform advanced text formatting and manipulation using regular expressions.

### **Methods**

1. **Replace text**\
   Replaces text matching the given regular expression with a replacement value.
   * **Example 1**: Replace the first occurrence of "birds" with "dogs":\
     **Input Text**: "cats eat birds and birds fly"\
     **Regex**: `/birds/`\
     **Replacement**: "dogs"\
     **Result**: "cats eat dogs and birds fly"
   * **Example 2**: Replace all occurrences of "birds" with "dogs" (using `g`):\
     **Input Text**: "cats eat birds and birds fly"\
     **Regex**: `/birds/g`\
     **Result**: "cats eat dogs and dogs fly"
2. **Extract matches**\
   Extracts text that matches the given regular expression.
   * **Example 1**: Extract the first match of a number:\
     **Input Text**: "Number 123 and there is also another one 345"\
     **Regex:** `/\d+/`\
     **Result**: "123" (Note: without "g", only the first number is extracted.)
   * **Example 2**: Extract all matches of all numbers (using `g`):\
     **Input Text**: "Number 123 and there is also another one 345"\
     **Regex**: `/\d+/g`\
     **Result**: \["123", "345"]
3. **Check if pattern matches**\
   Checks if the text contains a match for the given regular expression and returns `true` or `false`.
   * **Example**: Check if "abc" exists in "abc123":\
     **Regex**: `/abc/`\
     **Result**: "true"

### **Flags**

You can add flags to modify the behavior of your regular expressions.&#x20;

Examples of typically used flags:

* **`g`**: Global search (matches all occurrences).
* **`i`**: Case-insensitive search.
* **`m`**: Multiline search.
* **Example**: `/example/gi` performs a global, case-insensitive search.

### **Use Case Example: Extract Google Sheet IDs**

Imagine you have a list of Google Sheets links and need to extract the IDs.

* Your building block set-up:

<figure><img src="/files/K2Wjjbgm7BN3lVSFkHU2" alt=""><figcaption></figcaption></figure>

* **Result**: Extracted ID is "1aBcD\_12345\_EfGhI67-JklMnOpqrstu890".

<figure><img src="/files/mpNTVPCPKqih94WcszDg" alt=""><figcaption></figcaption></figure>

### **Additional Notes**

* If the input text is empty, the TaskBot will take no action and continue running.
* If your regex is invalid, the TaskBot will generate an error report and stop run/loop.
* You can validate and test your regex using tools like [regex101.com](https://regex101.com/).

### FAQ

#### I'm not a developer, can I still use regex?

Absolutely! Regex is simply a sequence of characters that specifies a match pattern in text. Anyone can use it. There are many reference guides on the internet to learn more. Here is one example: [Regex reference guide](https://learn.microsoft.com/en-us/dotnet/standard/base-types/regular-expression-language-quick-reference).


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.zerowork.io/using-zerowork/using-building-blocks/apply-regex.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
