-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpath_param.go
More file actions
35 lines (30 loc) · 780 Bytes
/
Copy pathpath_param.go
File metadata and controls
35 lines (30 loc) · 780 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
package gtools
import (
"errors"
"fmt"
"regexp"
"strconv"
)
func ExtractInt(pattern, path string, pos int) (int, error) {
re := regexp.MustCompile(pattern)
match := re.FindStringSubmatch(path)
if len(match) < pos+1 {
errStr := fmt.Sprintf("no match was found #v", match)
return 0, errors.New(errStr)
}
param, err := strconv.Atoi(match[pos])
if err != nil {
errStr := fmt.Sprintf("invalid match data type #v", match[pos])
return 0, errors.New(errStr)
}
return param, nil
}
func ExtractString(pattern, path string, pos int) (string, error) {
re := regexp.MustCompile(pattern)
match := re.FindStringSubmatch(path)
if len(match) < pos+1 {
errStr := fmt.Sprintf("no match was found #v", match)
return "", errors.New(errStr)
}
return match[pos], nil
}