Skip to main content
2 of 5
added 986 characters in body
Mikel
  • 58.7k
  • 16
  • 136
  • 155

It means zero of one of whatever came before it.

In your example of [ -]\?, it means there can be a space, a hyphen/minus, or neither.

In many other tools, zero or one is written as ?, but for backwards compatibility, grep uses a different syntax called "basic regular expressions", where ? just means a literal question mark.

To provide the zero or more quantifier, GNU grep added \?.

From man grep:

grep understands three different versions of regular expression syntax: “basic,” “extended” and “perl.” In GNU grep, there is no difference in available functionality between basic and extended syntaxes. In other implementations, basic regular expressions are less powerful.

...

Basic vs Extended Regular Expressions In basic regular expressions the meta-characters ?, +, {, |, (, and ) lose their special meaning; instead use the backslashed versions ?, +, {, |, (, and ).

Further info:

Mikel
  • 58.7k
  • 16
  • 136
  • 155