正则表达式需要 [^/] 和另一个用户定义的正则表达式

标签 regex go

假设用户给出了 str = "[a-z]"。如何匹配 [^/] [a-z]

我想要像 "/[^/]&"+ str + "/" 这样的东西。它应该匹配 asdf 而不是 a/sdf

最佳答案

最好分开检查。

package main

import "fmt"
import "regexp"

func main() {
    str := "test"
    user_provided_pattern := "^[a-z]+\\z"

    matched := false
    matched_slash, _ := regexp.MatchString("/", str)
    if !matched_slash {
       matched_user, err := regexp.MatchString(user_provided_pattern, str)
       if err != nil {
          panic(err)
       }

       if matched_user {
          matched = true
       }
    }

    fmt.Println(matched)
}

这是我的第一个围棋程序——我以前从未见过围棋程序——所以请原谅任何奇怪的地方。

关于正则表达式需要 [^/] 和另一个用户定义的正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44334512/

相关文章:

go - 需要帮助来理解 Go 语言中的垃圾收集

go - Sync.WaitGroup,为什么在 goroutine 中更接近

arrays - 在 Golang 中将指针传递给字符串数组

regex - 零或一个匹配的 Perl 正则表达式

php - 使用正则表达式匹配所有以 4 位数字结尾的子串

javascript - 如何将 PHP 正则表达式转换为将阿拉伯字符匹配到 JS 正则表达式

mongodb - Golang + MongoDB 嵌入类型(在另一个结构中嵌入一个结构)

php - 如何将 "Port O' brian 替换为 "Port O' Brian”?

python - 为什么我的正则表达式模式允许数字?

go - 如何理解bazel覆盖coverage.dat文件?