Sign in Start free

9Alternation |

The pipe means OR: cat|dog matches either word.

The vertical bar | separates alternatives. cat|dog matches cat or dog. Alternation has the lowest precedence of all: I love cats|dogs means "I love cats" OR "dogs".

To limit the alternatives to one part of the pattern, wrap them in a group: I love (cats|dogs). If you do not need to capture the group, write (?:cats|dogs).

The engine tries the alternatives from left to right and takes the first that works, so put longer alternatives first when they share a beginning: (?:Sept|Sep).

Exercises