0

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

Mihai Chelaru
  • 7,614
  • 14
  • 45
  • 51

1 Answers1

5

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.

iBug
  • 35,554
  • 7
  • 89
  • 134