520. 检测大写字母

正则表达式+内置函数

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
}
Subscribe
提醒
0 评论
Inline Feedbacks
View all comments
0
在此留下你的评论x