2 回答

TA貢獻1830條經驗 獲得超9個贊
您可以使用
re := regexp.MustCompile(`(?:\[\d{2}])+(.*)`)
match := re.FindStringSubmatch(s)
if len(match) > 1 {
return match[1] != ""
}
return false
該(?:\[\d{2}])+(.*)模式匹配 1+ 次出現[,2 個數字,]然后將除換行符之外的任何 0 個或更多字符捕獲到組 1 中。然后,如果找到匹配項 ( if len(match) > 1),true則如果組 1 值不為空,則應返回 ( match[1] != ""),否則false返回。
請參閱Go 演示:
package main
import (
"fmt"
"regexp"
)
func main() {
strs := []string{
"[11][22][33]",
"___[11][22][33]",
"[11][22][33]____",
"[11][22]____[33]",
}
for _, str := range strs {
fmt.Printf("%q - %t\n", str, match(str))
}
}
var re = regexp.MustCompile(`(?:\[\d{2}])+(.*)`)
func match(s string) bool {
match := re.FindStringSubmatch(s)
if len(match) > 1 {
return match[1] != ""
}
return false
}
輸出:
"[11][22][33]" - false
"___[11][22][33]" - false
"[11][22][33]____" - true
"[11][22]____[33]" - true
- 2 回答
- 0 關注
- 166 瀏覽
添加回答
舉報