Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

vim regex for "abcd", where "bc" is optional?

Tags:

I want to search for occurrences of ad and abcd where the bc is optional. How can I do that? i.e.,

+-------+----------+ | ad    | MATCH    | | abcd  | MATCH    | | abd   | NO match | | abbd  | NO match | | abced | NO match | | abcbcd| NO match | +-------+----------+ 
like image 681
Rob Bednark Avatar asked Mar 08 '13 22:03

Rob Bednark


2 Answers

Solutions:
option 1: ad\|abcd
option 2: a\(bc\)\=d
option 3: a\(bc\)\?d

Close but not quite:
option 4: a\(bc\)\{-\}d (zero or more; matches abcbcd which is not desired)

Descriptions:

+--------+--------------------------+ | \|     | logical OR (alternation) | | \(bc\) | treat `bc` as a group    | | \=     | zero or one occurrences  | | \?     | zero or one occurrences  | | \{-\}  | zero or more occurrences | +--------+--------------------------+ 
like image 196
Rob Bednark Avatar answered Oct 07 '22 00:10

Rob Bednark


well, I maybe stupid, but if the requirement is

search for occurrences of ad and abcd where the bc is optional.

why not simply and straightforward /ad\|abcd?

like image 35
Kent Avatar answered Oct 07 '22 01:10

Kent