simplify regexp with raw string

This commit is contained in:
chenyaqi01
2019-09-20 16:47:37 +08:00
parent 895642f9c3
commit 3175c9e226
4 changed files with 10 additions and 8 deletions

View File

@@ -573,6 +573,8 @@ func makeFullArgs(table Table, chain Chain, args ...string) []string {
return append([]string{string(chain), "-t", string(table)}, args...)
}
const iptablesVersionPattern = `v([0-9]+(\.[0-9]+)+)`
// getIPTablesVersion runs "iptables --version" and parses the returned version
func getIPTablesVersion(exec utilexec.Interface, protocol Protocol) (*utilversion.Version, error) {
// this doesn't access mutable state so we don't need to use the interface / runner
@@ -581,7 +583,7 @@ func getIPTablesVersion(exec utilexec.Interface, protocol Protocol) (*utilversio
if err != nil {
return nil, err
}
versionMatcher := regexp.MustCompile("v([0-9]+(\\.[0-9]+)+)")
versionMatcher := regexp.MustCompile(iptablesVersionPattern)
match := versionMatcher.FindStringSubmatch(string(bytes))
if match == nil {
return nil, fmt.Errorf("no iptables version found in string: %s", bytes)
@@ -641,7 +643,7 @@ func getIPTablesRestoreVersionString(exec utilexec.Interface, protocol Protocol)
if err != nil {
return "", err
}
versionMatcher := regexp.MustCompile("v([0-9]+(\\.[0-9]+)+)")
versionMatcher := regexp.MustCompile(iptablesVersionPattern)
match := versionMatcher.FindStringSubmatch(string(bytes))
if match == nil {
return "", fmt.Errorf("no iptables version found in string: %s", bytes)