What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

iTechGuides is reader-supported. When you buy through links on our site, we may earn an affiliate commission. As an Amazon Associate I earn from qualifying purchases. Learn more

A regular expression (regex) is a pattern that tells a tool what text to find, extract, replace, or check. The examples below use common JavaScript-style regex syntax unless a Python form is shown. Regex engines and APIs differ, so try each pattern in the application where you plan to use it.

Copy-paste regex examples

These patterns cover common beginner tasks. They are starting points, not universal validators: confirm how your editor, programming language, or other tool handles regex syntax, flags, and replacement text.

Task Pattern or operation What it does
Find a run of digits d+ Finds one or more digit characters in flavors that support d.
Match a simple identifier [A-Za-z_][A-Za-z0-9_]* Requires an ASCII letter or underscore first, followed by ASCII letters, digits, or underscores. This is a deliberately limited identifier rule.
Find a date-shaped substring bd{4}-d{2}-d{2}b Finds four digits, a hyphen, two digits, a hyphen, and two digits at word boundaries. It does not establish that the date exists on a calendar.
Match a whole date-shaped input ^d{4}-d{2}-d{2}$ In common modes, constrains the match to string or line boundaries. For whole-input validation, prefer the target API’s full-match operation when available and account for multiline behavior.
Find repeated whitespace s+ Finds one or more whitespace characters as defined by the selected flavor.
Replace repeated literal spaces Find: {2,}; replace with: Replaces each run of two or more ordinary spaces with one space. It does not collapse tabs or every kind of whitespace.
Match either spelling gr(a|e)y Matches gray or grey.
Capture a simple key and value ^([^=]+)=(.*)$ Under typical greedy matching, captures text before and after the first equals sign. Empty values, newlines, or stricter input rules may require a different pattern.
Match a literal period . Matches a period character rather than the regex wildcard.

Regex basics: characters, repetition, and boundaries

A regex combines literal characters and special syntax. A character class selects one character from a set; a quantifier says how many times the preceding character or group may occur. An ordinary search can find a matching substring anywhere in the text, so use boundaries or a full-match API when the whole input must conform.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Syntax Beginner meaning Example
cat Literal characters, often matched in sequence cat matches the substring “cat”.
. Any character except line terminators in common default modes; flags can change this c.t can match “cat” or “cot”.
[abc] One character from the listed set [aeiou] matches one listed vowel.
[a-z] One character in a range; range behavior depends on the flavor [A-Z] matches an ASCII uppercase letter in common flavors.
[^,] One character outside the listed set when ^ begins the class [^,]+ matches a nonempty run that contains no comma.
d, s, w Shorthands for digit, whitespace, and word-like characters, with flavor and mode caveats d+ finds a run of digits.
^, $ Boundary assertions often used for the start and end of input or a line ^cat$ is commonly used to match a whole line.
*, +, ? Zero or more, one or more, or zero or one repetitions of the preceding item ab+ matches “ab”, “abb”, and so on, but not just “a”.
{n}, {n,m} Exact or bounded repetition d{4} means four digits in common flavors.
(…) Groups a subpattern; a capturing group also saves matched text ([A-Z]+)-(d+) captures both parts.
(?:…) Non-capturing group in flavors that support it (?:cat|dog)s? groups the alternatives without requesting a capture.
A|B Matches either alternative cat|dog matches either word; group it when combining it with other syntax.
Escapes a metacharacter or introduces a special sequence . matches a literal period.

The JavaScript syntax reference groups concepts such as character classes, assertions, groups, and quantifiers; Python’s re module has corresponding beginner concepts. Do not assume advanced features have identical syntax or behavior in both. See MDN’s JavaScript regex cheatsheet and the Python 3.14.8 Regular Expression HOWTO.

Groups, alternation, and captured results

Parentheses can keep a pattern’s parts together and, by default, capture the text that matched. Captures are useful when extracting fields—for example, the two groups in ([A-Z]+)-(d+) separate uppercase letters from digits. If parentheses are needed only to group syntax, a non-capturing group such as (?:cat|dog) avoids asking for a capture; MDN notes that capturing groups have a performance cost.

Alternation, written with |, offers choices. Group it when it belongs inside a larger pattern: gr(a|e)y matches “gray” or “grey”. How captures are returned depends on the tool’s API. In JavaScript, for example, the behavior of match differs depending on whether the global flag is used. Consult the destination API if you need a particular result structure.

Greedy and non-greedy quantifiers

Quantifiers are generally greedy: they take as much text as they can while still allowing the entire pattern to match. In JavaScript, adding ? after a quantifier makes it non-greedy, asking for the shortest match compatible with the rest of the pattern.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

For input <b>one</b><b>two</b>, a pattern such as (<b>.*</b>) can span from the first opening tag to the last closing tag. A non-greedy form, (<b>.*?</b>), can stop at the earlier closing tag. This illustrates matching behavior; use an HTML parser rather than regex when you need to parse HTML structure.

For JavaScript’s quantifier behavior, see MDN’s guide to quantifiers.

Python and JavaScript escaping

Regex syntax is interpreted by a regex engine, but programming languages may parse the pattern string first. Python string literals and regex patterns both use backslashes, so raw strings avoid most double-escaping confusion in ordinary cases. For instance, pass r"d+" to Python’s re.search. In JavaScript, a regex literal can be written /d+/; constructing a RegExp from a string introduces the string-escaping layer as well. Check the Python re library reference for the raw-string explanation.

As A.M. Kuchling explains in the Python HOWTO, “Regular expressions (called REs, or regexes, or regex patterns) are essentially a tiny, highly specialized programming language embedded inside Python and made available through the re module.” The description is Python-specific, and it underscores why the host language and regex API matter.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

A practical way to build and debug a pattern

  1. Define the target. Write down an example the pattern should match and one it must reject.
  2. Start with literal text. Match the simplest case, then add one class, quantifier, group, or boundary at a time.
  3. Test edge cases. Try empty input, punctuation, spaces, line breaks, and boundary cases relevant to your task.
  4. Choose the right operation. Decide whether you are finding a substring, extracting captures, replacing text, or validating the entire input.
  5. Confirm the flavor and flags. Check the engine and options used by the target application, including multiline behavior.
  6. Use a better-fit tool when needed. A parser or dedicated validator is a better choice for nested structure or domain rules that a shape-matching regex cannot establish.

Know what a pattern does not prove

A regex can check that text has a particular shape without proving that it is valid in the real world. For example, d{4}-d{2}-d{2} can match a four-two-two date shape, but it does not establish that the month or day exists on the calendar. For rules involving valid dates, nested markup, or other domain-specific constraints, use a suitable parser or validator after—or instead of—a regex match.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.