-
-
Notifications
You must be signed in to change notification settings - Fork 137
Expand file tree
/
Copy pathRegex.go
More file actions
45 lines (36 loc) · 864 Bytes
/
Regex.go
File metadata and controls
45 lines (36 loc) · 864 Bytes
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
package main
import (
"fmt"
"regexp"
)
func main() {
// Basic Regexp for matching a number
pattern := "[0-9]+"
re, err := regexp.Compile(pattern)
if err != nil {
fmt.Println("Error compiling regex", err)
}
// Test if the pattern works
str := "Some string 0 1 2 3 4 "
if re.MatchString(str) {
fmt.Println("Yes, matched a number")
} else {
fmt.Println("No, no match")
}
// Return match
result := re.FindString(str)
fmt.Println("Number matched:", result)
// Return multiple matches
results := re.FindAllString(str, -1)
fmt.Println("Number matched: ", results)
// Replace match
replaceResults := re.ReplaceAllString(str, "num")
fmt.Println("Result:", replaceResults)
// Submatches
str1 := "Hello @world@ Match"
sub_re, _ := regexp.Compile("@(.*)@")
m := sub_re.FindStringSubmatch(str1)
if len(m) > 1 {
fmt.Println(m[1])
}
}