5

I understand that

'*' : The preceding item will be matched zero or more times.
'?' : The preceding item is optional and will be matched, at most, once.
'+' : The preceding item will be matched one or more items

Can anyone give me an example of when there would be a difference while using grep? I was using egrep, but I tried to check if I could generate different outputs for these operators.

posixKing
  • 1,123
  • 2
  • 11
  • 20
  • 1
    `?` and `+` are part of extended regex, so you do need egrep for that to work – Sergiy Kolodyazhnyy Sep 09 '16 at 08:50
  • 1
    or `grep -E` in GNU grep :) – Zanna Sep 09 '16 at 08:54
  • 2
    With GNU grep, the same *functionality* is available in both basic (BRE) and extended (ERE) regular expressions - it's just a matter of escaping. So for example in BRE, `?` matches a literal `?` while `\?` is the `{0,1}` quantifier; whereas in ERE it's the other way around i.e. `?` is the quantifier while `\?` matches a literal `?` – steeldriver Sep 09 '16 at 11:41

1 Answers1

13

make an example? try it out?

$ cat greppy
grp
grep
greep

zero or more e here

$ egrep 'gre*p' greppy
grp
grep
greep

zero or one e here

$ egrep 'gre?p' greppy
grp
grep

one or more e here

$ egrep 'gre+p' greppy
grep
greep
Zanna
  • 69,223
  • 56
  • 216
  • 327