github.com/gogf/gf/v2@v2.7.4/text/gregex/gregex.go (about)

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