Regex for IDs? Handle Full-Width Characters First

number AB-1234 looks simple, but the actual input may contain prefixes, trailing line breaks, or full-width characters. Writing just one regular expression and

Regex checks for IDs fail quietly when you rely on a single pattern. Here's how to check identifiers such as AB-1234 so that a substring match doesn't pass as a valid field.

Identifiers like AB-1234 look simple, but real input can include prefixes, trailing line breaks, or full-width characters. A single regular expression that passes whenever "a match succeeds" can treat part of a value as if the whole field were valid. This guide shows you how to set up stricter checks, test them against near-miss inputs, and avoid treating a format check as proof that an ID is real.

These examples use synthetic samples and were verified in an isolated environment. They do not guarantee results for every real file or product. Before you process important files, keep the original copies and check your results against the boundaries and acceptance criteria described here.

How do you validate an ID format? The short answer

Constrain both the length and the pattern, reject full-width characters, and treat format checks as a first step rather than proof that the ID is valid.

Your situationWhat to doWhy
You need to check a field like AB-1234Set the length to seven characters and the pattern to two uppercase Latin letters, a hyphen, and four ASCII digitsA pattern alone can match part of a longer string
Your input may contain full-width charactersReject them, or normalize them only if you keep the original value and a conversion logSilent conversion makes errors harder to trace
Input has a trailing line break or wrong caseReject it unless your input protocol explicitly defines how to handle itUnknown rules can change the value without anyone noticing
The format check passesRun source-location and business validation before you treat the ID as correctA regular expression cannot confirm that an ID exists or belongs to the right customer or document
You translate documents that contain IDsCheck that identifiers were not changed in the output and compare them with the original textIdentifiers are usually meant to be preserved exactly

Why one regular expression is not enough

A single pattern can look correct and still let invalid input through. Understanding the limits of pattern helps you build a stricter check.

Pattern matching finds a match, not a full match

JSON Schema's pattern typically indicates that a regular expression can find a match within a string. It does not automatically mean the whole string matches. That is why you should also constrain the length.

  • Pattern: two uppercase Latin letters, a hyphen, and four ASCII digits
  • Length: seven characters in total
  • Result: the protocol's scope is defined by both rules together

Regex behaviour varies by language

Regular expression implementations differ in how they handle anchors, Unicode character classes, and escaping. Don't assume that a construct works the same way in every JSON Schema implementation. Check the behaviour in your own environment.

Reference: JSON Schema string pattern

Test with near-miss counterexamples

Good tests use inputs that look almost right. Include values with extra characters, changed case, full-width characters, and trailing line breaks.

from jsonschema import Draft202012Validator

schema = {"type": "string", "minLength": 7, "maxLength": 7,
          "pattern": "[A-Z]{2}-[0-9]{4}"}
validator = Draft202012Validator(schema)
valid = lambda x: not list(validator.iter_errors(x))

assert valid("AB-1234")
assert not valid("prefix-AB-1234")
assert not valid("AB-1234\n")
assert not valid("AB-1234")
assert not valid("AB-1234")
assert not valid("ab-1234")

Decide how to handle full-width characters

You can handle full-width characters in one of two ways:

  1. Reject them. This is the approach shown in the rule above. It keeps the check simple and makes problems visible.
  2. Normalize, then validate. Some systems convert the value first. If you do this, keep both the original value and the conversion log.

Both approaches are clear-cut and easier to troubleshoot than splitting the work between different modules.

Know what happens to non-conforming input

Input that fails the format check should not be changed without a rule. The table below shows what each example input means for the next step.

inputThis example's resultsubsequent optional actions
AB-1234passesthen checks whether this ID exists in the business system
with a line break at the endrejectswhether to remove it is determined by the explicit input protocol
full-width alphanumericrejectsthe normalization strategy should be defined separately
case mismatchrejectsis not automatically capitalized under unknown rules

Format checks are only the first step

A regular expression cannot prove that an ID exists, nor can it confirm that the ID belongs to the current customer or document. OCR can also make mistakes. For example, it may convert O to 0, and the resulting string can still match a broad pattern.

For high-risk fields:

  • Check the value against its source location.
  • Run business validation.
  • Never display a format-check result as "extraction successful."

Keep identifiers protected during translation

When you translate documents, identifiers usually need to stay exactly as they are. When you export your work with OmniDoc (Product Entry), you can use these checks to confirm that identifiers have not been converted to full-width characters, changed in case, or lost any characters. If something has changed, return to the original text and verify it.

The rules in this guide are part of your delivery agreement. They are not an internal implementation detail of the platform.

📊 At a glance

ApproachWhat it doesWhat to keep in mind
Reject full-width charactersFails any input that contains themSimple to troubleshoot; the input must be corrected before it can pass
Normalize, then validateConverts full-width characters before the checkKeep the original value and a conversion log so you can trace changes

🎯 Which approach fits you?

If you need strict, traceable identifiers

Reject full-width characters and any value that fails the pattern or length check. Failures are easy to see and fix.

If your system must accept full-width input

Normalize first, but store the original value and a log of every conversion. Validate the normalized result against the same pattern and length rules.

If you extract identifiers from scanned documents

Run format checks, then compare each value with its source location and business records. Do not report a value as correct on the strength of a format check alone.

❓ FAQ

1. Why isn't a regular expression alone enough to validate an ID?

pattern can find a match inside a longer string, so it does not guarantee that the whole value is correct. Constraining the length as well makes the rule stricter.

2. Should I reject full-width characters or convert them?

Either approach can work. Rejecting them is the simpler option. If you convert them, keep the original value and a conversion log.

3. Does a passing format check mean the ID is valid?

No. A format check cannot confirm that an ID exists or belongs to the right customer or document. Use source-location and business validation for high-risk fields.

4. Are the examples in this guide guaranteed to work with my files?

No. The examples were verified in an isolated environment and may not work for every real file or product. Keep your original files and check results against your own acceptance criteria.

📝 Summary

A format check is only the first step in validating an identifier. Constrain both the pattern and the length, decide how to handle full-width characters, and verify important values against their source and your business records.

Try it now: upload a document and get it translated with the layout intact

Updated 2026-11-04