regex.go 4.4 KB

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