AI Glossary / Natural language and speech / Advanced

Character disjunction

Definition

Character disjunction is a regular-expression operation that allows one character position to match any member of a specified set. Square brackets commonly express it: [ae] matches either a or e. It is also called a character class or character set in many regex descriptions. It chooses one character, not one entire word from a list of words.

What it means

Disjunction means an alternative: this character or that character is acceptable here. For example, gr[ae]y can match gray or gray. The brackets describe the choice at one position; the surrounding g, r, and y remain fixed.
This gives text-processing rules a compact way to accept controlled variation. It does not make the system understand spelling, meaning, or the intended language. The rule recognizes only the alternatives actually described. The examples in this entry use Python's re engine with ordinary case-sensitive string matching and no additional flags.

How it works

A bracketed set is one unit in a pattern. [ABC] consumes one A, B, or C when it matches. Placing a repetition operator after that unit changes how many positions it can occupy: [ABC]{2} matches two characters, each independently selected from that set.
Ranges provide another way to specify members. [0-9] covers ASCII digits, while [A-Z] covers uppercase ASCII letters under the stated matching conditions. A leading caret inside brackets negates the set, so [^AB] matches one character other than A or B. Negation does not mean that no character is consumed.
Special syntax inside a class differs from syntax outside it. A hyphen can define a range, and a literal hyphen must be positioned or escaped appropriately. A vertical bar inside [A|B] is itself an allowed character in Python; it does not act as the outside alternation operator.

Three practical examples

  • A text search accepts a narrowly specified spelling variation such as gr[ae]y without requiring two otherwise identical patterns. Whole-word boundaries, if required, need to be specified separately.
  • A data-import rule accepts a department letter from a small approved alphabet and two ASCII digits. This checks the field's form, while a separate lookup checks whether the department and record exist.
  • A preprocessing step detects one of several punctuation marks, such as [.!?]. It can locate candidate sentence endings, but abbreviations and other context mean punctuation matching alone is not complete sentence segmentation.

Think of it this way

Imagine a form with one small box and a list of allowed symbols beside it. You may put any one listed symbol in that box. Adding more symbols to the list expands the choices; it does not make the box hold a whole sequence. A repetition rule is what adds more boxes.

Worked example

Consider an original format for a three-character batch label: the first character is A or B, and each of the next two characters is 0, 1, or 2. The pattern is [AB][0-2]{2}. Apply it with re.fullmatch so that the entire field must fit.
There are 2×3×3=18 accepted strings. For the A prefix they are A00, A01, A02, A10, A11, A12, A20, A21, and A22. Replacing A with B gives the other nine. Leading zeros are intentional because these are labels, not arithmetic quantities.
A02 and B21 pass. C02 fails because C is outside the first set. A23 fails because 3 is outside the final digit set. AA2 fails because the second position must be a digit. A2 fails because it lacks a position, and A002 fails because it has an extra position.
Now introduce a common mistake: [A|B][0-2]{2}. The first set allows A, B, or the literal vertical bar. It therefore accepts 3×3×3=27 strings, including |02. That is nine unintended additional labels. The intended equivalent using whole-pattern alternation is (?:A|B)[0-2]{2}; it still accepts exactly 18 strings.
Finally, searching for the original pattern inside XA02Y finds A02, whereas full matching XA02Y fails. Character disjunction controls the allowed characters, but the choice of matching operation controls whether a substring is enough. All counts assume the explicit alphabet and flags given above, not a broader Unicode or case-insensitive policy.

Illustrative case study

Fictional example: BatchDesk imports short routing labels. A developer writes [A|B] to mean A or B and checks only ordinary A-prefixed and B-prefixed examples. The rule appears correct until a record containing a vertical bar passes validation.
The team adds near-miss examples, enumerates the small allowed language, and replaces the mistaken class with [AB]. It also uses full matching for complete fields while retaining substring searches only for extracting labels from notes. The scenario illustrates why testing expected matches alone is insufficient; it does not claim an actual incident, a security exploit, or a measured business loss.

Common misconceptions and mistakes

[cat] matches one c, a, or t; it does not mean the word cat. Likewise, [yes|no] is not a choice between the words yes and no. Use an appropriate grouped alternation for complete alternatives.
A character class does not automatically capture the matched text as a numbered group. Capturing requires the relevant group syntax. Also, [A-z] is not a safe shorthand for only uppercase and lowercase ASCII letters: the range includes intervening characters. Write the intended ranges explicitly and test them in the chosen engine.

Business use

Character disjunction is useful for transparent, repeatable text filters and format checks with a clearly defined alphabet. Document case handling, Unicode expectations, field length, and whether matching covers the complete input. Include punctuation, extra characters, missing characters, and boundary cases in validation examples. For international text, avoid assuming that a short ASCII range represents every letter or digit users may reasonably enter.

People also ask

Is character disjunction the same as alternation?

It is a restricted choice among single characters. General alternation can choose between longer patterns.

Does [AB] match AB?

Not as a complete two-character field. It matches one of those characters.

Does [AB]+ require both letters?

No. It accepts one or more characters from the set, including AAA or B alone.

Check your understanding

Why does [A|B] accept a vertical bar?

Answer: Inside that Python character class, the bar is a literal member.

How many complete labels does [AB][0-2]{2} accept?

Answer: Eighteen.

Why does A002 fail full matching?

Answer: The pattern allows exactly three characters, while that input contains four.

Related terms

  • Regular expression
  • Capture group
  • Unicode
  • Pattern matching
  • character class
  • alternation
  • quantifier
  • input validation

Learn next

Compare bracketed character sets with grouped alternation using full-match tests. Then practice negated classes and literal punctuation, and inspect how engine flags affect the accepted language before using a pattern in a production text-processing pipeline.

More in Natural language and speech

  • Centering Theory - Centering Theory is a framework for explaining local discourse coherence through the prominence of entities and the referring expressions used for them.
  • Cepstral coefficients - Cepstral coefficients are numerical values obtained by transforming a logarithmic spectral representation of a signal.
  • Cepstral mean and variance normalization - Definition: Cepstral mean and variance normalization, usually abbreviated CMVN, standardizes speech-feature coefficients by subtracting an estimated mean...
  • Chart parsing - Chart parsing is a family of parsing methods that store and reuse hypotheses about the structure of portions of an input.
  • Chomsky-adjunction - Chomsky-adjunction, in the context-free grammar sense used here, is a recursive way of extending a constituent while retaining its category.
  • Clarification - Clarification is a conversational act that seeks to resolve uncertainty about what another participant said or meant.

Sources

Jurafsky and Martin, Speech and Language Processing, Words and Tokens, character disjunction: https://web.stanford.edu/~jurafsky/slp3/2.pdf Python documentation, re regular-expression syntax, character sets and alternation: https://docs.python.org/3/library/re.html

Previous: Channel Next: Character N-gram F-score

  • Home
  • Daily AI Updates
  • Blog
  • AIFBL Academy
  • AI Glossary
  • Products and Tools
  • Store
  • Free CFO AI Prompt Pack
  • About Shihan Sheriff
  • Privacy Policy
  • Terms of Use
  • Refund Policy
  • Cookie Policy
  • Earnings Disclaimer