Literal Pattern Prefix Factoring

Factor the common UTF-16 prefix from literal alternatives and produce an escaped noncapturing pattern; no general regex algebra.

Description

Factor the common UTF-16 prefix from literal alternatives and produce an escaped noncapturing pattern; no general regex algebra.

Literal alternatives can share a common prefix. Factoring it preserves the order and exact content of their remaining suffixes.

When to use this tool

Factor the common UTF-16 prefix from literal alternatives and produce an escaped noncapturing pattern; no general regex algebra.

  • Prepare the supplied literals fields for this operation.
  • Compare the complete example with the representation used by your own input before carrying the result into another step.

How to use the tool and how it works

  1. Enter literals. Use the exact text syntax, units and index conventions explained below.
  2. Run the tool. If input validation fails, correct the supplied fields before interpreting any result.
  3. Read result.prefix, result.suffixes, result.pattern. Interpret false, null and empty lists using the distinctions below; they can be valid outputs.

Find the longest common UTF-16 prefix, escape it and every suffix as literal regex text, and join suffixes inside a noncapturing alternation. Empty input yields the never-match pattern.1

Input fieldMeaning and representationAccepted input
literalsliterals.List of strings; at most 128 entries
Output fieldInterpretation
resultObject containing the named outputs below.
result.prefixThe prefix value under the stated formula or text convention.
result.suffixesThe suffixes list in the ordering and representation described above.
result.patternThe pattern value under the stated formula or text convention.
json
{
  "input": {
    "literals": [
      "abc",
      "abd"
    ]
  },
  "output": {
    "result": {
      "prefix": "ab",
      "suffixes": [
        "c",
        "d"
      ],
      "pattern": "ab(?:c|d)"
    }
  }
}

Limitations and assumptions

  • No anchors or flags are added, and duplicate alternatives remain. Factoring does not optimize arbitrary regexes or guarantee performance.
  • All declared input fields are required and unexpected top-level fields are rejected. Numbers must be finite; lists are limited to 128 entries and ordinary text fields to 8,192 UTF-16 code units, with tighter limits where stated.
  • JSON text inputs must parse within bounded nesting and size limits. Numeric outputs outside the safe finite range are rejected; large exact integer results use decimal strings. Indexes and half-open spans are zero-based unless a rank is explicitly described as one-based.

Alternative or Complementary methods

Compile generated pattern sources with explicitly chosen flags and test them on bounded representative text. Pattern sources, replacement strings and JSON string spellings require different escaping conventions.

References

  1. Tool behavior and example: Literal Pattern Prefix Factoring — tool.io

Similar or alternative tools

  • Literal Full Token Pattern Builder

    Build a literal full-string ECMAScript pattern with a strict end assertion, avoiding dollar-anchor final-newline acceptance; assumes no multiline flag.

  • Literal Alternation Formatter

    Build a noncapturing literal alternation with regex metacharacters escaped; empty bank produces a never-matching pattern.

Don't forget to set a bookmark for tool.io!
Privacy | Imprint | Cookies