# 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="https://3220183989-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fg9LcR8XM7TGOzOCDGsCS%2Fuploads%2FiCBTxRSGClOARMvfTtz8%2FScreenshot%202025-01-02%20at%2008.41.22.png?alt=media&#x26;token=b159872b-f646-4b65-8e9c-38893f55a461" alt=""><figcaption></figcaption></figure>

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

<figure><img src="https://3220183989-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fg9LcR8XM7TGOzOCDGsCS%2Fuploads%2FBtjoC2gV3wRBgL1ogTje%2FScreenshot%202025-01-02%20at%2008.42.58.png?alt=media&#x26;token=ed9f55c0-27ed-4349-aaca-6b298e1ee12b" 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).
