gregex.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. // Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
  2. //
  3. // This Source Code Form is subject to the terms of the MIT License.
  4. // If a copy of the MIT was not distributed with this file,
  5. // You can obtain one at https://github.com/gogf/gf.
  6. // Package gregex provides high performance API for regular expression functionality.
  7. package gregex
  8. import (
  9. "regexp"
  10. )
  11. // Quote quotes <s> by replacing special chars in <s>
  12. // to match the rules of regular expression pattern.
  13. // And returns the copy.
  14. //
  15. // Eg: Quote(`[foo]`) returns `\[foo\]`.
  16. func Quote(s string) string {
  17. return regexp.QuoteMeta(s)
  18. }
  19. // Validate checks whether given regular expression pattern <pattern> valid.
  20. func Validate(pattern string) error {
  21. _, err := getRegexp(pattern)
  22. return err
  23. }
  24. // IsMatch checks whether given bytes <src> matches <pattern>.
  25. func IsMatch(pattern string, src []byte) bool {
  26. if r, err := getRegexp(pattern); err == nil {
  27. return r.Match(src)
  28. }
  29. return false
  30. }
  31. // IsMatchString checks whether given string <src> matches <pattern>.
  32. func IsMatchString(pattern string, src string) bool {
  33. return IsMatch(pattern, []byte(src))
  34. }
  35. // MatchString return bytes slice that matched <pattern>.
  36. func Match(pattern string, src []byte) ([][]byte, error) {
  37. if r, err := getRegexp(pattern); err == nil {
  38. return r.FindSubmatch(src), nil
  39. } else {
  40. return nil, err
  41. }
  42. }
  43. // MatchString return strings that matched <pattern>.
  44. func MatchString(pattern string, src string) ([]string, error) {
  45. if r, err := getRegexp(pattern); err == nil {
  46. return r.FindStringSubmatch(src), nil
  47. } else {
  48. return nil, err
  49. }
  50. }
  51. // MatchAll return all bytes slices that matched <pattern>.
  52. func MatchAll(pattern string, src []byte) ([][][]byte, error) {
  53. if r, err := getRegexp(pattern); err == nil {
  54. return r.FindAllSubmatch(src, -1), nil
  55. } else {
  56. return nil, err
  57. }
  58. }
  59. // MatchAllString return all strings that matched <pattern>.
  60. func MatchAllString(pattern string, src string) ([][]string, error) {
  61. if r, err := getRegexp(pattern); err == nil {
  62. return r.FindAllStringSubmatch(src, -1), nil
  63. } else {
  64. return nil, err
  65. }
  66. }
  67. // ReplaceString replace all matched <pattern> in bytes <src> with bytes <replace>.
  68. func Replace(pattern string, replace, src []byte) ([]byte, error) {
  69. if r, err := getRegexp(pattern); err == nil {
  70. return r.ReplaceAll(src, replace), nil
  71. } else {
  72. return nil, err
  73. }
  74. }
  75. // ReplaceString replace all matched <pattern> in string <src> with string <replace>.
  76. func ReplaceString(pattern, replace, src string) (string, error) {
  77. r, e := Replace(pattern, []byte(replace), []byte(src))
  78. return string(r), e
  79. }
  80. // ReplaceFunc replace all matched <pattern> in bytes <src>
  81. // with custom replacement function <replaceFunc>.
  82. func ReplaceFunc(pattern string, src []byte, replaceFunc func(b []byte) []byte) ([]byte, error) {
  83. if r, err := getRegexp(pattern); err == nil {
  84. return r.ReplaceAllFunc(src, replaceFunc), nil
  85. } else {
  86. return nil, err
  87. }
  88. }
  89. // ReplaceFuncMatch replace all matched <pattern> in bytes <src>
  90. // with custom replacement function <replaceFunc>.
  91. // The parameter <match> type for <replaceFunc> is [][]byte,
  92. // which is the result contains all sub-patterns of <pattern> using Match function.
  93. func ReplaceFuncMatch(pattern string, src []byte, replaceFunc func(match [][]byte) []byte) ([]byte, error) {
  94. if r, err := getRegexp(pattern); err == nil {
  95. return r.ReplaceAllFunc(src, func(bytes []byte) []byte {
  96. match, _ := Match(pattern, bytes)
  97. return replaceFunc(match)
  98. }), nil
  99. } else {
  100. return nil, err
  101. }
  102. }
  103. // ReplaceStringFunc replace all matched <pattern> in string <src>
  104. // with custom replacement function <replaceFunc>.
  105. func ReplaceStringFunc(pattern string, src string, replaceFunc func(s string) string) (string, error) {
  106. bytes, err := ReplaceFunc(pattern, []byte(src), func(bytes []byte) []byte {
  107. return []byte(replaceFunc(string(bytes)))
  108. })
  109. return string(bytes), err
  110. }
  111. // ReplaceStringFuncMatch replace all matched <pattern> in string <src>
  112. // with custom replacement function <replaceFunc>.
  113. // The parameter <match> type for <replaceFunc> is []string,
  114. // which is the result contains all sub-patterns of <pattern> using MatchString function.
  115. func ReplaceStringFuncMatch(pattern string, src string, replaceFunc func(match []string) string) (string, error) {
  116. if r, err := getRegexp(pattern); err == nil {
  117. return string(r.ReplaceAllFunc([]byte(src), func(bytes []byte) []byte {
  118. match, _ := MatchString(pattern, string(bytes))
  119. return []byte(replaceFunc(match))
  120. })), nil
  121. } else {
  122. return "", err
  123. }
  124. }
  125. // Split slices <src> into substrings separated by the expression and returns a slice of
  126. // the substrings between those expression matches.
  127. func Split(pattern string, src string) []string {
  128. if r, err := getRegexp(pattern); err == nil {
  129. return r.Split(src, -1)
  130. }
  131. return nil
  132. }