Merge pull request #112241 from xiaoliwang/simplified_regexp

Optimize: simplified regexp
This commit is contained in:
Kubernetes Prow Robot 2022-09-07 02:28:36 -07:00 committed by GitHub
commit cae4c036c8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 8 additions and 8 deletions

View File

@ -70,7 +70,7 @@ func TestGetDataFromInitConfig(t *testing.T) {
t.Fatalf("failed to get secret data. fatal error: %v", err)
}
re := regexp.MustCompile(`[-._a-zA-Z0-9]+`)
re := regexp.MustCompile(`[-.\w]+`)
for name, data := range secretData {
if !re.MatchString(name) {
t.Fatalf(dedent.Dedent("failed to validate secretData\n %s isn't a valid secret key"), name)

View File

@ -28,7 +28,7 @@ import (
// GetKubeletVersion is helper function that returns version of kubelet available in $PATH
func GetKubeletVersion(execer utilsexec.Interface) (*version.Version, error) {
kubeletVersionRegex := regexp.MustCompile(`^\s*Kubernetes v((0|[1-9][0-9]*)\.(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)([-0-9a-zA-Z_\.+]*)?)\s*$`)
kubeletVersionRegex := regexp.MustCompile(`^\s*Kubernetes v((0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)([-\w.+]*)?)\s*$`)
command := execer.Command("kubelet", "--version")
out, err := command.Output()

View File

@ -25,7 +25,7 @@ var (
// (if present). All the parts before the tag we match in a single match everything (but not greedy) group.
// All matched sub-groups, except the tag one, get thrown away. Hence, in a result of FindStringSubmatch, if a tag
// matches, it's going to be the second returned element (after the full match).
tagMatcher = regexp.MustCompile(`^(?U:.*)(?::([[:word:]][[:word:].-]*))?(?:@sha256:[a-fA-F0-9]{64})?$`)
tagMatcher = regexp.MustCompile(`^(?U:.*)(?::([[:word:]][[:word:].-]*))?(?:@sha256:[a-fA-F\d]{64})?$`)
)
// TagFromImage extracts a tag from image. An empty string is returned if no tag is discovered.

View File

@ -48,7 +48,7 @@ func Reset() {
}
var (
testFunction = regexp.MustCompile(`.*\.Test[^./]+(.func[0-9]*)?$`)
testFunction = regexp.MustCompile(`.*\.Test[^./]+(.func\d*)?$`)
lock = sync.Mutex{}
fixtureDir = ""

View File

@ -41,9 +41,9 @@ const (
var (
kubeReleaseBucketURL = "https://dl.k8s.io"
kubeCIBucketURL = "https://storage.googleapis.com/k8s-release-dev"
kubeReleaseRegex = regexp.MustCompile(`^v?(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)([-0-9a-zA-Z_\.+]*)?$`)
kubeReleaseLabelRegex = regexp.MustCompile(`^((latest|stable)+(-[1-9](\.[1-9]([0-9])?)?)?)\z`)
kubeBucketPrefixes = regexp.MustCompile(`^((release|ci)/)?([-\w_\.+]+)$`)
kubeReleaseRegex = regexp.MustCompile(`^v?(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)([-\w.+]*)?$`)
kubeReleaseLabelRegex = regexp.MustCompile(`^((latest|stable)+(-[1-9](\.[1-9](\d)?)?)?)\z`)
kubeBucketPrefixes = regexp.MustCompile(`^((release|ci)/)?([-\w.+]+)$`)
)
// KubernetesReleaseVersion is helper function that can fetch
@ -135,7 +135,7 @@ func kubernetesReleaseVersion(version string, fetcher func(string, time.Duration
// Current usage is for CI images where all of symbols except '+' are valid,
// but function is for generic usage where input can't be always pre-validated.
func KubernetesVersionToImageTag(version string) string {
allowed := regexp.MustCompile(`[^-a-zA-Z0-9_\.]`)
allowed := regexp.MustCompile(`[^-\w.]`)
return allowed.ReplaceAllString(version, "_")
}