3
Now, I have three strings to be matched by a single re -- 'a', 'ab','ac' I suppose one correct re should be 'a[bc]?', but it seems not correct. What is the correct one?
I try it in grep command. And it seems that there is no '?' in grep's regex. So how to do this matching in grep?
Thanks to @anubhava, I now can match all these three strings by:
grep -E 'a[bc]?' <file>
However, this expression also matches 'ad'.
In fact, I want to match all these situation: 'a','abc','ab','ac' but do not want to match 'ad' or 'ae'
1It matches
'ad'
because it matches'a'
; did you try using the word boundaries as anubhava suggested? – None – 2013-06-17T06:26:29.390@EarthWorm: My suggested command was
grep -E "\ba[bc]?\b" file
(note extra word boundaries). – anubhava – 2013-06-17T14:01:17.013