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.

3
  • 1
    ? and + are part of extended regex, so you do need egrep for that to work Sep 9, 2016 at 8:50
  • 1
    or grep -E in GNU grep :)
    – Zanna
    Sep 9, 2016 at 8: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 ? Sep 9, 2016 at 11:41

1 Answer 1

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
3
  • It's a pleasure, enjoy egrep ^_^ @lufork
    – Zanna
    Sep 9, 2016 at 8:58
  • 1
    zero or one e here or optional e here Sep 9, 2016 at 12:04
  • That way of putting it may be clearer to some people @AvinashRaj :)
    – Zanna
    Sep 9, 2016 at 12:13

Not the answer you're looking for? Browse other questions tagged .