02Home & Garden · Updated
What Does | Mean in a Regex? The Pipe Explained
Short answer
The pipe means “or” in a regular expression
In most regular expression (regex) engines, the vertical bar | is an alternation operator: it matches the expression on its left or the expression on its right. For example, cat|dog matches either word.
What the pipe means in a regex
The character |, often called a pipe or vertical bar, separates alternatives in a regular expression. The regex cat|dog can match cat or dog. It does not mean “and,” nor does it usually match a literal pipe character; it is regex syntax unless escaped or placed in a context where it is treated literally.
Alternation is useful when several strings should be accepted at the same position in a pattern. Instead of writing separate searches for gray and grey, for example, you can write gr(a|e)y. The group selects either a or e, so the pattern matches both spellings.
Regex engines and programming languages have some differences in their exact syntax and features. Still, alternation with | is supported by the commonly used regex flavors, including those found in many text editors and programming languages.
How alternation works
A regex engine reads the alternatives separated by | and tries to find a match that satisfies the pattern. In red|blue, the two alternatives are red and blue. If the engine can match either one at the current position, the alternation succeeds. The exact order in which an engine explores alternatives can affect which match it returns when more than one is possible, but it does not change the basic meaning: either branch may match.
Alternatives can be full strings, character patterns, or more complex expressions. Each branch may contain its own character classes, quantifiers, or groups. For instance, colou?r|color can match color or colour; in this particular example the second alternative is redundant because the first already matches color, but it illustrates that each side can be a pattern rather than a single character.
The pipe is not the same as a character class. In [ae], the brackets mean “match one character from this set.” In a|e, alternation means “match the expression a or the expression e.” These patterns happen to behave similarly for single letters, but alternation can represent longer choices: yes|no matches either complete word.
Why parentheses matter
Alternation can apply to more of a pattern than a reader first expects. Consider cat|dog food. It means “match cat or dog food,” not “match cat food or dog food.” The pipe separates the alternatives cat and dog food.
Use parentheses to group the choices when other pattern parts should apply to both alternatives. The pattern (cat|dog) food matches cat food or dog food. Here, the group contains the alternatives, and the space followed by food comes after the group, so it is required for either choice.
Grouping also determines how quantifiers apply. In ha|ho+, the plus sign applies only to the o in the second branch; it allows one or more o characters there. In (ha|ho)+, the plus sign applies to the entire group, allowing one or more repetitions of either ha or ho. Ordinary parentheses usually capture the matched text as a group. If capturing is unnecessary, many regex flavors offer a noncapturing group such as (?:cat|dog); check the flavor’s documentation because syntax and features can differ.
Practical examples
- Two spellings:
gr(a|e)ymatchesgrayandgrey. - Two labels:
^(yes|no)$matches a string that consists of exactlyyesorno, assuming the engine treats these anchors as start and end of the string. - Several file extensions:
\.(jpg|png|gif)$looks for a period followed by one of those extensions at the end of the string. In a programming-language string literal, backslashes may need additional escaping. - Optional word choice:
Nov(ember)?matchesNovorNovember. This uses a quantifier on a group rather than alternation, but it illustrates another way to express a choice when one form is optional. - A literal pipe:
\|commonly matches the actual|character. In a character class, such as[|], the pipe is generally literal, though regex details can vary.
Anchors such as ^ and $ are often used to require a whole-string match. Without them, a pattern such as yes|no may find either word inside a longer piece of text, depending on the search method. Anchors’ precise behavior can vary in multiline modes and across regex flavors, so consult the relevant documentation when exact boundaries matter.
Common mistakes and misconceptions
- Forgetting to group: If a suffix, prefix, or quantifier should apply to every choice, group the alternatives first. Compare
cat|dog foodwith(cat|dog) food. - Assuming “or” means only one branch can match: Alternation expresses choices, but it does not necessarily force a whole string to equal exactly one alternative. Without boundaries, a search can find a branch within a larger string.
- Using a pipe for a literal symbol: Escape it as
\|in many regex flavors, or place it in a character class where the engine treats it literally. - Confusing regex escaping with code escaping: A regex typed directly into a regex tool may use
\.for a literal period. In a programming-language string, the language may also process backslashes first, so the source code might require a doubled backslash. Raw-string syntax is available in some languages. - Expecting identical behavior everywhere: Regex flavors differ in supported syntax, flags, and details. A pattern that works in one tool may need adjustment in another.
How to choose and test alternatives
Use alternation when the choices are distinct patterns; use a character class when the choice is between individual characters. Group alternatives when shared parts belong before or after them, or when a quantifier should repeat the whole choice. These habits make a pattern easier to read and reduce accidental matches.
When a regex matters, test it against both intended matches and near-misses: examples that look similar but should not match. For a pattern intended to accept just cat or dog, test those strings, then test phrases such as cat food and hotdog to check whether boundaries are needed. Use a regex tester configured for the same flavor as the tool or language where the expression will run.
Frequently Asked Questions
Does | mean OR in a regex?
Yes. In most regex flavors, | means alternation: match the expression on its left or the expression on its right. Parentheses help control how much of the pattern each alternative includes.
How do I match a literal pipe character?
In many regex flavors, escape it as \|. Another common option is to put it in a character class, as in [|]. Check the regex flavor if the pattern is for a particular tool or language.
What is the difference between | and []?
A character class such as [ab] matches one character chosen from the listed set. Alternation such as apple|banana can choose between expressions of different lengths.
Why does my regex match only part of a word?
Alternation does not automatically require a whole-word or whole-string match. Add appropriate boundaries or start-and-end anchors for your task, while accounting for the regex engine’s rules.
Do I need parentheses around every |?
No. Parentheses are needed when you want to define the scope of alternatives, capture a group, or apply a quantifier to the choices together. A simple pattern like cat|dog can stand on its own.
Related
- What Is an LLC? Meaning, Structure, and How It Works
- Circling the Wagons Meaning and How It’s Used
- Why Is My Phone Flashing White Light?
Further reading: regular-expression documentation for your language or tool. Details can change, so check the official page for anything that matters to you. Spotted a mistake? Tell us. How we write an answer.