---
title: "正则表达式-基本使用"
url: "https://goframe.org/docs/components/text-gregex-example"
markdown_url: "https://goframe.org/docs/components/text-gregex-example.md"
locale: "zh-Hans"
version: "2.10.x(Latest)"
section: "组件列表"
category: "组件列表 / 文本处理 / 正则表达式-gregex"
published: "2024-10-21"
updated: "2024-11-11"
description: "展示了如何在GoFrame框架中使用正则表达式进行基本的文本匹配操作。通过一个简单的示例代码，您可以了解如何提取和处理字符串中的信息，是开发者进行文本处理的重要参考。"
keywords: ["GoFrame", "GoFrame框架", "正则表达式", "基本使用", "编程示例", "Golang", "文本处理", "软件开发", "开源框架", "Go语言"]
---

# 正则表达式-基本使用

一个简单示例：

```go
package main

import (
    "fmt"
    "github.com/gogf/gf/v2/text/gregex"
)

func main() {
    match, _ := gregex.MatchString(`(\w+).+\-\-\s*(.+)`, `GoFrame is best! -- John`)
    fmt.Printf(`%s says "%s" is the one he loves!`, match[2], match[1])
}
```

执行后，输出结果为：

```
John says "GoFrame" is the one he loves!
```
