https://leetcode.cn/problems/detect-capital/
试着用正则表达式写了写,还挺麻烦的。
先Compile或者直接用MatchString都行。而“$“用于表示一个正则表达式的结束,必不可少。匹配的话无非就三种:全大写;全小写;只有开头字母大写。
Go
import "regexp"
func detectCapitalUse(word string) bool {
match,_ := regexp.MatchString(`(?:^[A-Z]+$|^[a-z]+$|^[A-Z][a-z]+$)$`,word)
return match
}