summaryrefslogtreecommitdiff
path: root/regexp/regexp.go
blob: c63bc409a90c304dee061d85cef03b07f0d52800 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// Package regexp contains functions for dealing with regular expressions
package regexp

import (
	"fmt"
	stdre "regexp"
)

// Find -
func Find(expression, input string) (string, error) {
	re, err := stdre.Compile(expression)
	if err != nil {
		return "", err
	}
	return re.FindString(input), nil
}

// FindAll -
func FindAll(expression string, n int, input string) ([]string, error) {
	re, err := stdre.Compile(expression)
	if err != nil {
		return nil, err
	}
	return re.FindAllString(input, n), nil
}

// Match -
func Match(expression, input string) (bool, error) {
	re, err := stdre.Compile(expression)
	if err != nil {
		return false, fmt.Errorf("error compiling expression: %w", err)
	}

	return re.MatchString(input), nil
}

// QuoteMeta -
func QuoteMeta(input string) string {
	return stdre.QuoteMeta(input)
}

// Replace -
func Replace(expression, replacement, input string) (string, error) {
	re, err := stdre.Compile(expression)
	if err != nil {
		return "", fmt.Errorf("error compiling expression: %w", err)
	}

	return re.ReplaceAllString(input, replacement), nil
}

// ReplaceLiteral -
func ReplaceLiteral(expression, replacement, input string) (string, error) {
	re, err := stdre.Compile(expression)
	if err != nil {
		return "", fmt.Errorf("error compiling expression: %w", err)
	}
	return re.ReplaceAllLiteralString(input, replacement), nil
}

// Split -
func Split(expression string, n int, input string) ([]string, error) {
	re, err := stdre.Compile(expression)
	if err != nil {
		return nil, fmt.Errorf("error compiling expression: %w", err)
	}

	return re.Split(input, n), nil
}