github.com/hairyhenderson/templater@v3.5.0+incompatible/regexp/regexp.go (about)

     1  // Package regexp contains functions for dealing with regular expressions
     2  package regexp
     3  
     4  import stdre "regexp"
     5  
     6  // Find -
     7  func Find(expression, input string) (string, error) {
     8  	re, err := stdre.Compile(expression)
     9  	if err != nil {
    10  		return "", err
    11  	}
    12  	return re.FindString(input), nil
    13  }
    14  
    15  // FindAll -
    16  func FindAll(expression string, n int, input string) ([]string, error) {
    17  	re, err := stdre.Compile(expression)
    18  	if err != nil {
    19  		return nil, err
    20  	}
    21  	return re.FindAllString(input, n), nil
    22  }
    23  
    24  // Match -
    25  func Match(expression, input string) bool {
    26  	re := stdre.MustCompile(expression)
    27  	return re.MatchString(input)
    28  }
    29  
    30  // Replace -
    31  func Replace(expression, replacement, input string) string {
    32  	re := stdre.MustCompile(expression)
    33  	return re.ReplaceAllString(input, replacement)
    34  }
    35  
    36  // ReplaceLiteral -
    37  func ReplaceLiteral(expression, replacement, input string) (string, error) {
    38  	re, err := stdre.Compile(expression)
    39  	if err != nil {
    40  		return "", err
    41  	}
    42  	return re.ReplaceAllLiteralString(input, replacement), nil
    43  }
    44  
    45  // Split -
    46  func Split(expression string, n int, input string) ([]string, error) {
    47  	re, err := stdre.Compile(expression)
    48  	if err != nil {
    49  		return nil, err
    50  	}
    51  	return re.Split(input, n), nil
    52  }