doushanmo7024 2018-06-20 02:09
浏览 427
已采纳

Golang正则表达式匹配+-/ *

I tried this:

re_operator := regexp.MustCompile("^(+|-|*|/)")

I get this:

panic: regexp: Compile(`^(+|-|*|/)`): error parsing regexp: missing argument to repetition operator: `+`

It's literally impossible to Google an answer to this question, not to mention it varies for every language and version. I'm about to use an if else. Escape sequences are also a pain. Should I try escaping my escape sequence?

Unhelpful answer: Regular expression to match digits and basic math operators

  • 写回答

1条回答 默认 最新

  • dtd5644 2018-06-20 02:12
    关注

    Escape RegEx meta characters:

    MustCompile("^(\\+|-|\\*|/)")
    

    Or better yet, use a bracket expression:

    MustCompile("^[-+*/]")
    

    Note in bracket expressions you must put the hyphen at first or at last.

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

报告相同问题?

悬赏问题

  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试,帮帮忙吧
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python