regex - Regular expression character class with parenthesis with grep command -
regular expression grep command. example let have file called regular.txt contain date below:
$ cat regular.txt july jul fourth 4th 4
so trying match these text input file,using below process method:
method 1: match fourth|4th|4
$egrep '(fourth|4th|4)` regular.txt
output method 1:
fourth 4th 4
method 2: match fourth|4th|4 using optional parenthesis
$ egrep '(fourth|4(th)?)` regular.txt
output method 2:
fourth 4th 4
method 3: match entire file july, jul, fourth, 4th, 4. using command below:
$ egrep 'july? (fourth|4(th)?)` regular.txt
output method 3: nothing match here. how ?
could please me on ?
thanks,
your july? (fourth|4(th)?)
regex matches sequence of patterns, jul
followed optional y
, space, , 2 alternatives: fourth
or 4
optionally followed th
substring.
if plan match jul
or july
3rd alternative, add grouping construct:
'fourth|4(th)?|july?' ^ ^^
Comments
Post a Comment