2

What is wrong with this? I can't seem to figure it out

regsub {+} $input { }

I get this error: couldn't compile regular expression pattern: quantifier operand invalid

potrzebie
  • 1,768
  • 1
  • 12
  • 25

2 Answers2

4

The + character is regular-expression metasyntax: it means match the preceding sub-RE one or more times. (For example, a+ matches one or more a characters.) Because of this, if you want to use a raw + you have to either escape it with a backslash (\+) or in a character set ([+]), or put the RE engine into one of its restricted modes; starting the RE with ***= makes the rest of the RE be a literal to match. Specializing to your case, ***=+ matches a plain +, and ***=++ matches two plusses in a row, etc.

Donal Fellows
  • 133,037
  • 18
  • 149
  • 215
1

Escape the + char:

regsub {\+} $input { }
pynexj
  • 19,215
  • 5
  • 38
  • 56