dongzhao2725 2016-11-02 12:37
浏览 101
已采纳

进行正则表达式字符串验证[重复]

This question already has an answer here:

In go there is the function MatchString which can be used to match a string with a regex, however, the function returns true if a substring that matches the regex is found.

Is there a way/similar function that returns true only when the whole of the string is matched (e.g. if I have [0-9]{2} and my string is 213, the return value should be false). ? or should this be done from the regex string itself ?

</div>
  • 写回答

1条回答 默认 最新

  • drn1008 2016-11-02 12:39
    关注

    Try this:

    ^[0-9]{2}$
    

    Explanation

    GO CODE:

    package main
    
    import (
        "regexp"
        "fmt"
    )
    
    func main() {
        var re = regexp.MustCompile(`(?m)^[0-9]{2}$`)
        var str = `213`
    
        for i, match := range re.FindAllString(str, -1) {
            fmt.Println(match, "found at index", i)
        }
    }
    

    Run the code here

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计