Regular Expressions - Methods - 图1tip

The following is a list of common methods. Documentation updates may lag behind new code features. For more methods and examples, please refer to the code documentation: https://pkg.go.dev/github.com/gogf/gf/v2/text/gregex

When the function name contains All, it will continue to search for non-overlapping follow-ups and return a slice.

When the function name contains String, the parameters and return values are string, otherwise they are []byte.

IsMatch/IsMatchString

  • Description: The IsMatch() method can test a string to determine whether it matches the pattern of a regular expression. If one match is found, the method returns true; otherwise, it returns false.
  • Format:
  1. IsMatch(pattern string, src []byte) bool
  2. IsMatchString(pattern string, src string) bool
  • Tip: regexp has already handled and cached the Regex object at the bottom level to significantly improve execution efficiency, so there is no need to explicitly recreate it each time.
  • Example:
  1. func ExampleIsMatch() {
  2. patternStr := `\d+`
  3. g.Dump(gregex.IsMatch(patternStr, []byte("hello 2022! hello GoFrame!")))
  4. g.Dump(gregex.IsMatch(patternStr, nil))
  5. g.Dump(gregex.IsMatch(patternStr, []byte("hello GoFrame!")))
  6. // Output:
  7. // true
  8. // false
  9. // false
  10. }

Match/MatchString

  • Description: Used to match substrings. Match only returns the first matching result. Unlike native regex methods, gregex wraps FindSubmatch to directly return a slice of the first sub-pattern result.
  • Format:
  1. Match(pattern string, src []byte) ([][]byte, error)
  2. MatchString(pattern string, src string) ([]string, error)
  • Example: Matching parameters in url.
  1. func ExampleMatch() {
  2. patternStr := `(\w+)=(\w+)`
  3. matchStr := "https://goframe.org/pages/viewpage.action?pageId=1114219&searchId=8QC5D1D2E!"
  4. // This method looks for the first match index
  5. result, err := gregex.Match(patternStr, []byte(matchStr))
  6. g.Dump(result)
  7. g.Dump(err)
  8. // Output:
  9. // [
  10. // "pageId=1114219",
  11. // "pageId",
  12. // "1114219",
  13. // ]
  14. // <nil>
  15. }

MatchAll/MatchAllString

  • Description: Used for matching substrings, MatchAll returns all results. Unlike native regex methods, gregex‘s MatchAll wraps FindAllSubmatch, returning a slice of all result sets, including sub-pattern results within the result sets.
  • Format:
  1. MatchAllString(pattern string, src string) ([][]string, error)
  2. MatchAll(pattern string, src []byte) ([][][]byte, error)
  • Example: The following example matches the parameters in url.
  1. func ExampleMatchAll() {
  2. patternStr := `(\w+)=(\w+)`
  3. matchStr := "https://goframe.org/pages/viewpage.action?pageId=1114219&searchId=8QC5D1D2E!"
  4. result, err := gregex.MatchAll(patternStr, []byte(matchStr))
  5. g.Dump(result)
  6. g.Dump(err)
  7. // Output:
  8. // [
  9. // [
  10. // "pageId=1114219",
  11. // "pageId",
  12. // "1114219",
  13. // ],
  14. // [
  15. // "searchId=8QC5D1D2E",
  16. // "searchId",
  17. // "8QC5D1D2E",
  18. // ],
  19. // ]
  20. // <nil>
  21. }
  22. func ExampleMatchAllString() {
  23. patternStr := `(\w+)=(\w+)`
  24. matchStr := "https://goframe.org/pages/viewpage.action?pageId=1114219&searchId=8QC5D1D2E!"
  25. result, err := gregex.MatchAllString(patternStr, matchStr)
  26. g.Dump(result)
  27. g.Dump(err)
  28. // Output:
  29. // [
  30. // [
  31. // "pageId=1114219",
  32. // "pageId",
  33. // "1114219",
  34. // ],
  35. // [
  36. // "searchId=8QC5D1D2E",
  37. // "searchId",
  38. // "8QC5D1D2E",
  39. // ],
  40. // ]
  41. // <nil>
  42. }

Quote

  • Description: Escapes specific symbols in the given regular expression.
  • Format:
  1. Quote(s string) string
  • Example:
  1. func ExampleQuote() {
  2. result := gregex.Quote(`[1-9]\d+`)
  3. g.Dump(result)
  4. // Output:
  5. // "\[1-9\]\\d\+"
  6. }

Replace/ReplaceString

  • Description: Used to replace all matching strings and return a copy of the source string.
  • Format:
  1. Replace(pattern string, replace, src []byte) ([]byte, error)
  2. ReplaceString(pattern, replace, src string) (string, error)
  • Example:
  1. func ExampleReplace() {
  2. var (
  3. patternStr = `\d+`
  4. str = "hello GoFrame 2020!"
  5. repStr = "2021"
  6. result, err = gregex.Replace(patternStr, []byte(repStr), []byte(str))
  7. )
  8. g.Dump(err)
  9. g.Dump(result)
  10. // Output:
  11. // <nil>
  12. // "hello GoFrame 2021!"
  13. }

ReplaceFunc/ReplaceStringFunc

  • Description: Used to replace all matching strings and return a copy of the source string. Unlike the Replace method, this method allows secondary evaluation or processing within a closure instead of simple replacement.
  • Format:
  1. ReplaceFunc(pattern string, src []byte, replaceFunc func(b []byte) []byte) ([]byte, error)
  2. ReplaceStringFunc(pattern string, src string, replaceFunc func(s string) string) (string, error)
  • Example:
  1. func ExampleReplaceFunc() {
  2. var (
  3. patternStr = `(\d+)~(\d+)`
  4. str = "hello GoFrame 2018~2020!"
  5. )
  6. // In contrast to [ExampleReplace]
  7. result, err := gregex.ReplaceFunc(patternStr, []byte(str), func(match []byte) []byte {
  8. g.Dump(match)
  9. return bytes.Replace(match, []byte("2020"), []byte("2023"), -1)
  10. })
  11. g.Dump(result)
  12. g.Dump(err)
  13. // ReplaceStringFunc
  14. resultStr, _ := gregex.ReplaceStringFunc(patternStr, str, func(match string) string {
  15. g.Dump(match)
  16. return strings.Replace(match, "2020", "2023", -1)
  17. })
  18. g.Dump(resultStr)
  19. g.Dump(err)
  20. // Output:
  21. // "2018~2020"
  22. // "hello gf 2018~2023!" // ReplaceFunc result
  23. // <nil>
  24. // "2018~2020"
  25. // "hello gf 2018-2023!" // ReplaceStringFunc result
  26. // <nil>
  27. }

ReplaceFuncMatch/ReplaceStringFuncMatch

ReplaceFuncMatch returns a copy of src, where all matches of regexp are replaced by the function’s return value applied to the matched byte slices. The returned replacement replaces directly.

  • Description: Used to replace all matching strings and return a copy of the source string. The power of this method lies in secondary evaluation or processing within a closure, where the MatchString function contains all sub-pattern query results, rather than simple replacement.
  • Format:
  1. ReplaceFuncMatch(pattern string, src []byte, replaceFunc func(match [][]byte) []byte) ([]byte, error)
  2. ReplaceStringFuncMatch(pattern string, src string, replaceFunc func(match []string) string) (string, error)
  • Example:
  1. func ExampleReplaceStringFuncMatch() {
  2. var (
  3. patternStr = `([A-Z])\w+`
  4. str = "hello Golang 2018~2021!"
  5. )
  6. // In contrast to [ExampleReplaceFunc]
  7. // the result contains the `pattern' of all subpatterns that use the matching function
  8. result, err := gregex.ReplaceFuncMatch(patternStr, []byte(str), func(match [][]byte) []byte {
  9. g.Dump(match)
  10. return []byte("GoFrame")
  11. })
  12. g.Dump(result)
  13. g.Dump(err)
  14. // ReplaceStringFuncMatch
  15. resultStr, err := gregex.ReplaceStringFuncMatch(patternStr, str, func(match []string) string {
  16. g.Dump(match)
  17. match[0] = "Gf"
  18. return match[0]
  19. })
  20. g.Dump(resultStr)
  21. g.Dump(err)
  22. // Output:
  23. // [
  24. // "Golang",
  25. // "G",
  26. // ]
  27. // "hello GoFrame 2018~2021!" // ReplaceFuncMatch result
  28. // <nil>
  29. // [
  30. // "Golang",
  31. // "G",
  32. // ]
  33. // "hello Gf 2018~2021!" // ReplaceStringFuncMatch result
  34. // <nil>
  35. }

Split

  • Description: Splits the text content using the specified regular expression. Without metacharacters, equivalent to strings.SplitN.
  • Format:
  1. Split(pattern string, src string) []string
  • Example:
  1. func ExampleSplit() {
  2. patternStr := `\d+`
  3. str := "hello2020GoFrame"
  4. result := gregex.Split(patternStr, str)
  5. g.Dump(result)
  6. // Output:
  7. // [
  8. // "hello",
  9. // "GoFrame",
  10. // ]
  11. }

Validate

  • Description: Wraps the native compile method to check if the given regular expression is valid.
  • Format:
  1. Validate(pattern string) error
  • Example:
  1. func ExampleValidate() {
  2. // Valid match statement
  3. g.Dump(gregex.Validate(`\d+`))
  4. // Mismatched statement
  5. g.Dump(gregex.Validate(`[a-9]\d+`))
  6. // Output:
  7. // <nil>
  8. // {
  9. // Code: "invalid character class range",
  10. // Expr: "a-9",
  11. // }
  12. }