How to grep to include an optional word?

grep

Solution

You can use a `\?` to make something optional:

grep -nri "^\s*\(public\|private\|protected\)\s*\(static\)\?\s*\(sub\|function\)" formName.frm

In this case, the preceding group, which contains the string "static", is optional (i.e. may occur 0 or 1 times).

Problem

I use the following grep query to find the occurrences of functions in a VB source file. ``` grep -nri "^\s*\(public\|private\|protected\)\s*\(sub\|function\)" formName.frm ``` This matches - ``` Private Sub Form_Unload(Cancel As Integer) Private Sub lbSelect_Click() ... ``` However, it misses out on functions like - ``` Private Static Sub SaveCustomer() ``` because of the additional word "Static" in there. How to account for this "optional" word in the grep query?

Original source