Introduction
Quantifiers help you find patterns in text by telling how many times a part can repeat. They make searching easier and smarter.
pattern* pattern+ pattern?
grep 'a*' file.txtgrep -P 'a+' file.txtgrep -E 'colou?r' file.txt#!/bin/bash # Create a sample file echo -e "color\ncolour\ncolr" > colors.txt # Find lines with zero or more 'u's after 'colo' grep 'colou*r' colors.txt # Find lines with one or more 'o's grep -P 'o+' colors.txt # Find lines where 'u' is optional grep -E 'colou?r' colors.txt
* mean when used in a regular expression?* in regex* quantifier matches zero or more occurrences of the character or group before it.+ which requires at least one match, * allows zero matches too.* means zero or more matches [OK]+ quantifier in grep to match one or more digits?+ needs extended regex+ quantifier is not supported by default in basic grep; it requires -E or -P option.grep -E '[0-9]+' enables extended regex and matches one or more digits.file.txt contains the lines:apple apples ap ape
grep -E 'ap?le' file.txt
ap?lep? means zero or one 'p' character. So it matches 'ale' or 'aple'.grep '[0-9]+' file.txt
-E (extended regex) or -P (Perl regex) option.s? quantifier.\b for word boundaries and -E to enable ? quantifier.grep -E '\bcats?\b' which matches 'cat' or 'cats' correctly. Others either lack -E or misplace the quantifier.Pass the test and this pattern moves unknown