Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular expression: Preceding token is not quantifiable

I have a regular expression (-|+)?, but when I ran it on regex101.com I get the error:

+: Preceding token is not quantifiable

What does the error mean? Thank you

like image 237
VN1992 Avatar asked Oct 20 '25 03:10

VN1992


1 Answers

+ is a special character, a quantifier, that specifies the multiplicity of the element before. For example a+ means that there should be at least one a, up to any number of as. If you want to match the plus character itself, you will have to escape it:

(-|\+)?

In your case, as you are only considering two different characters, you can also use a character class and specify the two characters that way. Then you don’t need to escape it:

[-+]?
like image 183
poke Avatar answered Oct 21 '25 16:10

poke