Skip to content

Generate Values from Regular Expressions

Use the pattern attribute when a generated string must match a structural format. DATAMIMIC routes the pattern to its Rust rand_regex backend; the seeded execution path remains deterministic.

regex-patterns.xml
1
2
3
4
5
6
7
<generate name="identifiers" count="10">
    <key name="username" pattern="[A-Za-z]{5,15}"/>
    <key name="phone_number" pattern="\d{10}"/>
    <key name="password" pattern="[A-Za-z0-9]{8,12}"/>
    <key name="date" pattern="\d{4}-\d{2}-\d{2}"/>
    <key name="custom_id" pattern="[A-Z][a-z]+[0-9]"/>
</generate>

Patterns describe shape, not domain validity. For example, \d{4}-\d{2}-\d{2} can produce a string with the requested layout but does not by itself guarantee a real calendar date. Use a date generator or validation rule when semantics matter.

Supported pattern language

The supported language is the syntax accepted by the versions used by DATAMIMIC: Rust regex 1.10.6 and rand_regex 0.17.0. It is not PCRE and not the unrestricted Python re language.

  • Literals, character classes such as [A-Z], bounded quantifiers such as {2,8}, alternation with |, and groups are supported.
  • DATAMIMIC normalizes \d to the ASCII class [0-9] before handing the pattern to the Rust backend.
  • Lookarounds such as (?=...) and backreferences such as \1 are not supported by the Rust regex language.
  • Prefer bounded quantifiers. Broad unbounded expressions make output length unclear and can require unnecessarily expensive generation work.

If a pattern expresses a business rule rather than only a shape, combine it with a validation constraint or use a domain generator. A regex can format an identifier; it cannot prove that the identifier was issued by an external authority.

See the generated key reference for the exact current attribute contract.