Regular Expressions/operator/pipe

From Wikibooks, open books for an open world
Jump to navigation Jump to search

A pipe symbol allows regular expression components to be logically ORed. For example, the following regular expression matches lines that start with the word "Germany" or the word "Netherlands". Note that parentheses are used to group the two expressive components.

/(^Germany)|(^Netherlands)/

Precedence[edit | edit source]

The alternation operator has a low precedence within a regular expression, so applies to the largest possible components either side:

Note that parentheses can be used to override the precedence to change the behaviour of the above expression:

The alternation operator is not recognized by some Unix tools[edit | edit source]

Some Unix tools do not recognize the pipe symbol as an alternation operator.