普通网友 2014-08-06 11:30
浏览 101

编译正则表达式时出现恐慌

I am writing a regular expression for password validation with the following code:

func IsPasswordValid(value string) bool {
    pattern := regexp.MustCompile(`^.*(?=.{7,})(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[^a-zA-Z0-9]).*$`)
    return pattern.MatchString(value)
}

When I execute the application, it panics:

Line 45: - regexp: Compile(`^.*(?=.{7,})(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[^a-zA-Z0-9]).*$`): error parsing regexp: invalid or unsupported Perl syntax: `(?=` 

This regular expression works in Javascript and Ruby but not in Go. What I am do wrong here?

  • 写回答

2条回答 默认 最新

  • donglilian0061 2014-08-06 11:47
    关注

    From the docs:

    The syntax of the regular expressions accepted is the same general syntax used by Perl, Python, and other languages. More precisely, it is the syntax accepted by RE2 and described at http://code.google.com/p/re2/wiki/Syntax, except for \C.

    The RE2 wiki clearly states:

    (?=re) before text matching re (NOT SUPPORTED)

    See also:

    EDIT: if you really need PCRE features, you can use a binding package, e.g. https://github.com/tuxychandru/golang-pkg-pcre/. Otherwise, try to rethink your regexp and what it should match.

    评论

报告相同问题?

悬赏问题

  • ¥20 java-OJ-健康体检
  • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况
  • ¥15 画两个图 python或R
  • ¥15 在线请求openmv与pixhawk 实现实时目标跟踪的具体通讯方法
  • ¥15 八路抢答器设计出现故障