-
Notifications
You must be signed in to change notification settings - Fork 0
/
strings_regexp.go
77 lines (59 loc) · 2.77 KB
/
strings_regexp.go
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
70
71
72
73
74
75
76
77
package main
import (
"fmt"
"regexp"
"github.com/enetx/g"
)
func main() {
// Define a regular expression pattern
pattern := regexp.MustCompile(`(?m)(post-)(?:\d+)`)
// Replace the first occurrence of the pattern in the string
g.String("post-55").ReplaceRegexp(pattern, "${1}38").Print()
// Output: post-38
// Find the first match of the pattern in the string
o := g.String("x post-55 x").FindRegexp(pattern)
// Output: post-55
// switch pattern
switch {
case o.IsSome():
o.Some().Print()
default:
fmt.Println("not found")
}
// If no match is found, provide a default value
g.String("post-not-found").FindRegexp(pattern).UnwrapOr("post-333").Print()
// Output: post-333
// Find all matches of the pattern in the string
g.String("some post-55 not found post-31 post-22").FindAllRegexp(pattern).Unwrap().Print()
// Output: Slice[post-55, post-31, post-22]
// Find a specific number of matches of the pattern in the string
g.String("some post-55 not found post-31 post-22").FindAllRegexpN(pattern, 2).Some().Print()
// Output: Slice[post-55, post-31]
// Get the starting indices of the first match of the pattern in the string
g.String("post-55").IndexRegexp(pattern).Some().Print()
// Output: Slice[0, 7]
// Find the submatches of the first match of the pattern in the string
g.String("some post-55 not found post-31").FindSubmatchRegexp(pattern).Some().Print()
// Output: Slice[post-55, post-]
// Find all submatches of the pattern in the string
g.String("some post-55 not found post-31 post-22").FindAllSubmatchRegexp(pattern).Some().Print()
// Output: Slice[Slice[post-55, post-], Slice[post-31, post-], Slice[post-22, post-]]
// Find a specific number of submatches of the pattern in the string
g.String("some post-55 not found post-31 post-22").FindAllSubmatchRegexpN(pattern, 2).Some().Print()
// Output: Slice[Slice[post-55, post-], Slice[post-31, post-]]
// Split the string using the regular expression pattern
g.String("some test for split n").SplitRegexp(*regexp.MustCompile(`\s`)).Print()
// Output: Slice[some, test, for, split, n]
// Split the string using the regular expression pattern, limiting the number of splits
g.String("some test for split n").SplitRegexpN(*regexp.MustCompile(`\s`), 2).Some().Print()
// Output: Slice[some, test for split n]
// Check if the string contains a match of the regular expression
fmt.Println(g.String("some test").ContainsRegexp(`\s`).Unwrap())
// Output: true
// Check if the string contains matches for all the provided regular expressions
fmt.Println(g.String("some test 1").ContainsRegexpAll(`\s`, `\d`).Unwrap())
// Output: true
// Check if the string contains a match for any of the provided regular expressions
fmt.Println(g.String("some test").ContainsRegexpAny(`\s`, `\d`).Unwrap())
// Output: true
}