The regular expression: (?-imsx:^\s*(\Q$A\E\s*\*?\s*\Qcos\E\s+\Q$w\E\s*\Qt\E\s+\QA\E||\Q$A\E\s*\*?\s*\Qcos\E\s*\(\s*\Q$w\E\s*\Qt\E\s*\)\s+\QA\E)\s*$) matches as follows: NODE EXPLANATION ---------------------------------------------------------------------- (?-imsx: group, but do not capture (case-sensitive) (with ^ and $ matching normally) (with . not matching \n) (matching whitespace and # normally): ---------------------------------------------------------------------- ^ the beginning of the string ---------------------------------------------------------------------- \s* whitespace (\n, \r, \t, \f, and " ") (0 or more times (matching the most amount possible)) ---------------------------------------------------------------------- ( group and capture to \1: ---------------------------------------------------------------------- \Q 'Q' ---------------------------------------------------------------------- $ before an optional \n, and the end of the string ---------------------------------------------------------------------- A 'A' ---------------------------------------------------------------------- \E 'E' ---------------------------------------------------------------------- \s* whitespace (\n, \r, \t, \f, and " ") (0 or more times (matching the most amount possible)) ---------------------------------------------------------------------- \*? '*' (optional (matching the most amount possible)) ---------------------------------------------------------------------- \s* whitespace (\n, \r, \t, \f, and " ") (0 or more times (matching the most amount possible)) ---------------------------------------------------------------------- \Q 'Q' ---------------------------------------------------------------------- cos 'cos' ---------------------------------------------------------------------- \E 'E' ---------------------------------------------------------------------- \s+ whitespace (\n, \r, \t, \f, and " ") (1 or more times (matching the most amount possible)) ---------------------------------------------------------------------- \Q 'Q' ---------------------------------------------------------------------- $ before an optional \n, and the end of the string ---------------------------------------------------------------------- w 'w' ---------------------------------------------------------------------- \E 'E' ---------------------------------------------------------------------- \s* whitespace (\n, \r, \t, \f, and " ") (0 or more times (matching the most amount possible)) ---------------------------------------------------------------------- \Q 'Q' ---------------------------------------------------------------------- t 't' ---------------------------------------------------------------------- \E 'E' ---------------------------------------------------------------------- \s+ whitespace (\n, \r, \t, \f, and " ") (1 or more times (matching the most amount possible)) ---------------------------------------------------------------------- \Q 'Q' ---------------------------------------------------------------------- A 'A' ---------------------------------------------------------------------- \E 'E' ---------------------------------------------------------------------- | OR ---------------------------------------------------------------------- | OR ---------------------------------------------------------------------- \Q 'Q' ---------------------------------------------------------------------- $ before an optional \n, and the end of the string ---------------------------------------------------------------------- A 'A' ---------------------------------------------------------------------- \E 'E' ---------------------------------------------------------------------- \s* whitespace (\n, \r, \t, \f, and " ") (0 or more times (matching the most amount possible)) ---------------------------------------------------------------------- \*? '*' (optional (matching the most amount possible)) ---------------------------------------------------------------------- \s* whitespace (\n, \r, \t, \f, and " ") (0 or more times (matching the most amount possible)) ---------------------------------------------------------------------- \Q 'Q' ---------------------------------------------------------------------- cos 'cos' ---------------------------------------------------------------------- \E 'E' ---------------------------------------------------------------------- \s* whitespace (\n, \r, \t, \f, and " ") (0 or more times (matching the most amount possible)) ---------------------------------------------------------------------- \( '(' ---------------------------------------------------------------------- \s* whitespace (\n, \r, \t, \f, and " ") (0 or more times (matching the most amount possible)) ---------------------------------------------------------------------- \Q 'Q' ---------------------------------------------------------------------- $ before an optional \n, and the end of the string ---------------------------------------------------------------------- w 'w' ---------------------------------------------------------------------- \E 'E' ---------------------------------------------------------------------- \s* whitespace (\n, \r, \t, \f, and " ") (0 or more times (matching the most amount possible)) ---------------------------------------------------------------------- \Q 'Q' ---------------------------------------------------------------------- t 't' ---------------------------------------------------------------------- \E 'E' ---------------------------------------------------------------------- \s* whitespace (\n, \r, \t, \f, and " ") (0 or more times (matching the most amount possible)) ---------------------------------------------------------------------- \) ')' ---------------------------------------------------------------------- \s+ whitespace (\n, \r, \t, \f, and " ") (1 or more times (matching the most amount possible)) ---------------------------------------------------------------------- \Q 'Q' ---------------------------------------------------------------------- A 'A' ---------------------------------------------------------------------- \E 'E' ---------------------------------------------------------------------- ) end of \1 ---------------------------------------------------------------------- \s* whitespace (\n, \r, \t, \f, and " ") (0 or more times (matching the most amount possible)) ---------------------------------------------------------------------- $ before an optional \n, and the end of the string ---------------------------------------------------------------------- ) end of grouping ----------------------------------------------------------------------