I need to find a regex in go that match when there is no lowercase and at least one uppercase.
For example:
"1 2 3 A" : Match
"1 2 3" : No match
"a A " : no match
"AHKHGJHB" : Match
This work but in PHP not in Go (the ?=
token isn't working in Go):
(?=.*[A-Z].*)(?=^[^a-z]*$)
In my code this line call the regex:
isUppcase, _ := reg.MatchString(`^[^a-z]*$`, string)
Actually my regex catch when there is no lowercase but I want it also to catch when there is at least one uppercase.
You may use
^\P{Ll}*\p{Lu}\P{Ll}*$
Or, a bit more efficient:
^\P{L}*\p{Lu}\P{Ll}*$
See the regex demo.
Details
^
- start of string^\P{L}*
- 0 or more chars other than letters\p{Lu}
- an uppercase letter\P{Ll}*
- 0 or more chars other than lowercase letters$
- end of string.package main
import (
"regexp"
"fmt"
)
func main() {
re := regexp.MustCompile(`^\P{L}*\p{Lu}\P{Ll}*$`)
fmt.Println(re.MatchString(`1 2 3 A`))
fmt.Println(re.MatchString(`1 2 3`))
fmt.Println(re.MatchString(`a A`))
fmt.Println(re.MatchString(`AHKHGJHB`))
fmt.Println(re.MatchString(`Δ != Γ`))
}
Output:
true
false
false
true
true