Recent chats
ASKGEEZ Q&A GUIDETechnology & devices

How can I match either of two complete words without accepting partial alternatives?

Quick answer

Put the alternatives in a group and place word boundaries around the whole group: \b(?:cat|dog)\b. The | means “or,” and the group makes the boundaries apply to both choices. In Python and JavaScript, this can find either word within a larger string. Word-boundary rules depend on the regex engine, so check them if your text or token definition is more complex. [S1][S2]

LET’S WORK THROUGH IT

The question, the details, the next step.

Question

I want a regular expression to find either cat or dog as a complete word, including in a sentence. What’s the basic pattern?

Geez

Use \b(?:cat|dog)\b. The | separates alternatives, so the group means “cat or dog.” The \b assertions mark word boundaries; putting one before and after the group applies them to either complete choice. Python’s re and JavaScript both support this pattern for ordinary English words. [S1][S2]

For example, it matches A cat naps. and A dog naps. It does not match catalog or hotdog, because neither contains cat or dog separated by the needed boundaries. These examples assume the usual regex interpretation of the pattern and ordinary English text.

Follow-up question

Why isn’t \bcat|dog\b equivalent? It still has a boundary at each end.

Geez

Because alternation has low precedence: without grouping, the pattern is read as two branches—\bcat or dog\b. Each boundary applies only to its own branch, not to both alternatives. [S1][S2]

That means \bcat|dog\b can match cat at the start of catalog, or dog at the end of hotdog. The before-and-after version is:

Before: \bcat|dog\b After: \b(?:cat|dog)\b

The non-capturing group (?:...) groups the alternatives without creating a captured group in Python. [S1] JavaScript also accepts this grouped form. [S2]

Follow-up question

How do I use that pattern in Python and JavaScript, and what should I test?

Geez

In Python, search within a string with re.search(r'\b(?:cat|dog)\b', text). In JavaScript, use /\b(?:cat|dog)\b/.test(text). These examples look for a match anywhere in the text rather than requiring the entire string to consist only of one word.

A small check set is:

  1. cat — match.
  2. A dog naps. — match.
  3. catalog — no match.
  4. hotdog — no match.
  5. A bird naps. — no match.

The boundary behavior is engine-defined. In particular, MDN describes JavaScript \b using basic Latin letters, digits, and underscore as word characters, with Unicode-aware case-folding details. Python likewise cautions that its word-boundary behavior depends on its definition of word characters. So this is a useful ordinary-English pattern, not a universal linguistic rule for every language or token system. [S1][S2]

Follow-up question

Can you give me a reusable way to build this when my two words change?

Geez

Use this template, replacing the placeholders with your alternatives: \b(?:FIRST|SECOND)\b. Keep the opening and closing parentheses around the full set of choices; otherwise a boundary—or other surrounding pattern—may apply to only one alternative. For example, for complete words red or blue, write \b(?:red|blue)\b.

Before relying on it, try one positive example for each choice and a longer word containing each choice as a substring. If you need to match punctuation-specific tokens, non-English text, or a definition of “word” different from the regex engine’s, this boundary pattern may not express your intended rule. The cited documentation explains the engine behavior; it does not establish a universal definition of a complete word. [S1][S2]

A practical checklist

  • Put every alternative inside one group: (?:FIRST|SECOND).

  • Place a word boundary before and after the complete group: \b(?:FIRST|SECOND)\b.

  • Test one sentence containing each desired word.

  • Test longer strings that contain each alternative as a substring.

  • Check whether your engine’s word-boundary rules fit your text.

A few more useful answers

Will this match a word inside a sentence?

Yes. A search or test using the pattern can find either complete word within a larger string; it does not require the whole string to equal that word.

Why use `(?:...)` instead of ordinary parentheses?

Both group alternatives, but (?:...) is a non-capturing group, so it groups the choices without capturing the group as a value. [S1]

Does `\b` define a word the same way in every language?

No. Word-boundary behavior depends on the regex engine’s word-character rules, so this pattern is not a universal linguistic word detector. [S1][S2]

Sources

References used for this guide.

  1. [S1] Python 3.15 re — Regular expression operations
    docs.python.org · Checked
  2. [S2] MDN JavaScript Guide: Character classes and disjunction
    developer.mozilla.org · Checked

YOUR QUESTION. YOUR DETAILS.

Let’s work through your situation.

Get a tailored answer or help with your next task. Starts at $1, with a quote before you pay.

Ask your own question
← Back to Geez at Work