doumei8126 2018-11-15 07:06
浏览 58

为什么正则表达式会在golang中给我编译时出现紧急错误?

I made a program for validate the names of the users but regexp will give me the compile time panic error why it so happen and how to resolve it below is the program I'm using :-

package main

import (
  "fmt"
  "regexp"
)

func main() {
  var arr [5]string
  arr[0] = "O’max"
  arr[1] = "O’sdf"
  arr[2] = "dsf’max"
  arr[3] = "dsfrewwerx"
  arr[4] = "ewrwerwerwerwr’max"
  var validName = regexp.MustCompile("^[a-zA-ZÀ-ž\x7f-\xff\u00c0-\u017e0-9-_&$.,'`()!/ ]*$")
  for i := 0; i < len(arr); i++ {
    if !validName.MatchString(arr[i]) {
        fmt.Println(arr[i])
    }
  }
}

Error

panic: regexp: Compile("^[a-zA-ZÀ-ž\u007f-\xffÀ-ž0-9-&$.,'()!/ ]*$"): error parsing regexp: invalid UTF-8:�À-ž0-9-&$.,'()!/ ]*$

Any body tell me How i will make a regrex for the validating the names of the users below is the playground link

Playground

  • 写回答

1条回答 默认 最新

  • dongmimeng5500 2018-11-15 07:23
    关注

    use

    ^[\\p{L}0-9-_&$.,'\x60()!/ ]*$
    

    As your regexp, where the "letters" including non ascii are the \p{L} class and the backtick is a \x60

    评论

报告相同问题?

悬赏问题

  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥20 matlab yalmip kkt 双层优化问题
  • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体
  • ¥88 实在没有想法,需要个思路
  • ¥15 MATLAB报错输入参数太多
  • ¥15 python中合并修改日期相同的CSV文件并按照修改日期的名字命名文件
  • ¥15 有赏,i卡绘世画不出
  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题