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

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.

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

报告相同问题?