Regular expressions, often called regex or regexp, are patterns for finding, checking, splitting, or replacing text. A beginner can use them to match simple text such as dates or words, then combine a small set of symbols to describe more flexible patterns.
What regular expressions are for
A regular expression describes a pattern rather than one fixed string. The pattern cat matches those three consecutive characters in text; it can find them inside a longer word such as “catalog.” Regex is useful when ordinary search is too exact, but a full programming language or text-processing tool would be excessive.
Common uses include searching source code, validating the shape of an input, extracting pieces of text, splitting a line into fields, and replacing repeated text. The exact features available depend on the programming language or application, so a pattern that works in one tool may need adjustment in another.
How a regex pattern works
Most regex patterns are read from left to right. Ordinary characters generally match themselves, while special characters give instructions about what kinds of text can match. For example, gr[ae]y matches either “gray” or “grey”: the square brackets allow one character from the listed choices.
.matches one character in many regex flavors, usually except a line break unless a setting changes that behavior.\dcommonly matches a digit,\wa word character, and\swhitespace. Their precise meanings can vary, especially for Unicode text.[A-Z]matches one uppercase ASCII letter;[^0-9]matches one character that is not an ASCII digit.^and$often mark the beginning and end of a line or input, depending on options.|means “or,” as intea|coffee.
A backslash often changes a character’s meaning. For example, . is usually a wildcard, while \. matches a literal period. In programming-language strings, a backslash may also need escaping for the language itself, so the written code can contain more backslashes than the regex pattern.
Repetition, groups, and boundaries
Quantifiers specify how many times a preceding character, class, or group can occur. * means zero or more, + means one or more, and ? means zero or one. A range such as {2,4} means between two and four repetitions. For instance, \d{4} matches four consecutive digits, but does not by itself ensure that those digits are a valid year.
Parentheses group parts of a pattern. In (ha)+, the plus applies to the whole group, so it can match “ha,” “haha,” or more repetitions. Capturing groups also save matched text so a program can refer to it later. Some regex flavors offer noncapturing groups, written (?:...), when grouping is needed but saving the match is not.
Boundaries help prevent matches from occurring inside larger words. In many flavors, \bcat\b looks for “cat” at word boundaries, while cat can also match the beginning of “catalog.” What counts as a word character—and therefore a boundary—can depend on the engine and its Unicode settings.
Build and test a pattern step by step
Start with the smallest pattern that can express the task. Suppose you want to find a simple date written as four digits, a hyphen, two digits, a hyphen, and two digits. A shape-checking pattern might be \d{4}-\d{2}-\d{2}. It checks the arrangement, not whether the date exists: 2025-19-45 has the same shape but is not a real calendar date.
- Write down what should match and what should not, including edge cases.
- Match the literal characters first, then add character classes and repetition.
- Test against examples that should match and examples that should fail.
- Add boundaries or anchors if the pattern must match a whole word, line, or input.
- Check the tool’s documentation for its regex flavor, flags, and replacement syntax.
For a full-input check, many tools support anchors such as ^ and $, but the meaning of those anchors can change in multiline mode. Some programming APIs also provide a dedicated “matches entire input” operation. Prefer the clearest option in the tool you are using.
Common mistakes and limitations
A frequent mistake is assuming that a pattern verifies more than it actually does. A regex can check whether a string has a familiar email-like shape, for example, but a pattern alone cannot confirm that an address exists or that a person can receive mail. Likewise, matching date-shaped text is not the same as validating calendar rules.
Quantifiers are often greedy: they try to match as much text as possible while still allowing the pattern to succeed. In <.*>, the dot and star may consume from the first opening angle bracket through the last closing one on a line. A lazy quantifier such as .*? asks for the shortest match that lets the rest of the pattern succeed, but it is not automatically the right fix; a more specific character class may express the intended rule better.
Other pitfalls include forgetting that special characters need escaping, relying on shorthand classes without checking their character-set behavior, and writing a long pattern that nobody can maintain. Complex patterns can also cause very slow matching in some backtracking engines, particularly when nested repetition and ambiguous alternatives are involved. Keep patterns focused, test troublesome inputs, and use a parser or dedicated library for structured formats such as HTML, JSON, or programming-language syntax.
Regex flavors and alternatives
Regex is not one perfectly uniform standard. Common engines include those associated with JavaScript, Python, Java, .NET, and PCRE-style tools; they differ in supported features, Unicode behavior, flags, and replacement rules. Before copying a pattern, verify the target engine’s documentation and test it in the actual application.
For an exact phrase, ordinary text search is simpler. For structured data, use a parser where available: parsers understand the data’s rules, while a regex usually sees only characters. For complicated transformations, a small program with clearly named steps may be easier to review than one dense pattern. Regex remains a practical choice for bounded text tasks when its limits are understood.
Frequently Asked Questions
What does regex stand for?
Regex is a common abbreviation for regular expression, a text pattern used to search, match, split, or replace strings.
What does the dot mean in regex?
In many engines, a dot matches one character other than a line break. A mode or flag may allow line breaks too; use an escaped dot, \., to match a literal period.
What is the difference between * and +?* allows zero or more repetitions of the preceding item. + requires one or more. Thus ab* can match “a” or “abbb,” while ab+ requires at least one “b.”
Can regex validate an email address?
A regex can check a chosen, simplified format, but email address rules and real-world delivery involve more than a basic pattern. Use an appropriate library or service when full validation is needed.
Why does my regex work in one tool but not another?
Different engines support different syntax and interpret flags, Unicode characters, boundaries, and replacements differently. Check the target tool’s flavor and test the pattern there.
Related
Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.
More cards in this drawer
- Valheim Server Not Showing in Community List Fixes
- Soundbar vs 2.1 Speaker System: Which Is Better?
- DisplayPort vs HDMI: Which Cable Should You Use?
- Survive Lava for Brainrot Tier List170 points
- MySQL Dump Import: Fix Common Errors
- Mouse Moving on Its Own? Causes & Fixes