> For the complete documentation index, see [llms.txt](https://docs.gpmautomate.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.gpmautomate.com/automate-zh/cao-zuo-zhi-nan/text-and-number/regex.md).

# 正则表达式

正则表达式（Regular Expression）就像一把放大镜，帮助您准确找到隐藏在大文本中的一段文本，或者像一张过滤网，帮助您保留所需的数据格式（如电话号码、电子邮件、OTP码），并去除周围多余的文字。

#### 配置参数：

* Text: 需要过滤数据的原始文本（或包含文本的变量）。
* Regex: 用于定义需要提取的字符串结构的过滤代码/语法。
* Output variable: 过滤成功后保存结果的变量名称。

#### 实际示例：从电子邮件内容中提取OTP码

当您使用读取电子邮件的操作时，系统将返回整个冗长的电子邮件文本内容。您的任务是从中准确提取出包含6位数字的OTP码。

假设接收到的输入文本为：

> "您的GPM Automate验证码是834912。请勿与任何人分享此码，该码在5分钟内有效。"

为了提取6位OTP数字，您可以将正则表达式操作配置如下：

* Text: 传入上述文本（或包含电子邮件内容的变量）。
* Regex: 填写为 `\d{6}` *(这是表示6个连续数字的正则语法)*。
* Output variable: 填写需要保存的变量名称（例如：`$otpCode`）。

结果：正则表达式的“过滤网”将扫描文本，忽略所有越南语部分，并准确保留字符串 `834912` 以赋值给变量 `$otpCode`。

> 💡 小贴士：正则语法看起来复杂且难以记忆？别担心，您不需要自己死记硬背。请打开AI工具（如ChatGPT、DeepSeek、Gemini等），直接用自然语言提问，例如：“给我写一段正则表达式来提取6个连续数字”或“写一个正则表达式提取位于'Mã của bạn là:'和句号之间的字符串”。AI将生成准确的代码，您只需将其复制粘贴到GPM Automate的配置框中即可。


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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, and the optional `goal` query parameter:

```
GET https://docs.gpmautomate.com/automate-zh/cao-zuo-zhi-nan/text-and-number/regex.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
