# `@regex` annotation

The `@regex` annotation is used to specify a regex pattern for permitted values of attributes.

## [](#_syntax)Syntax

Value annotation syntax

```typeql
<type label> value <value type> @regex("<regular expression>");
```

Ownership annotation syntax

```typeql
<type label> owns <attribute type label> @regex("<regular expression>");
```

The `@regex` annotation can be defined for either an attribute type’s value type or an ownership.

The `@range` annotation accepts a single string argument containing a regular expression.

## [](#_usage)Usage

The `@regex` annotation adds a constraint on values of attributes of a given attribute type to be valid strings according to the regular expression.

When defined for an attribute type’s value type, the annotation creates a constraint for any attribute of this attribute type.

```typeql
#!test[schema]
define
  attribute email value string @regex("^.*@\w+\.\w+$");
```

When defined for an ownership, the annotation creates a constraint for any attribute of this attribute type owned by the owner type. In the following example, an instance of the `phone` type can contain any `string` value, but a `user` can have (`has`) only the `phone` s with the suitable values.

```typeql
#!test[schema]
define
  entity user owns phone @regex("^\d{10,15}$");
  attribute phone value string;
```

### [](#_subtyping)Subtyping

The `regex` constraint put on an attribute types' value type is inherited. This means that values of all subtypes of a constrained type are also constrained, and this constraint cannot be relaxed from the subtypes.

A subtype can introduce another `regex` constraint to combine it with the inherited one and create a subset of allowed values based on the intersection of the specified regular expressions.

### [](#_subtyping_for_ownerships)Subtyping for ownerships

The `regex` constraint put on an ownership should be respected by all the sub attribute types owned by this owner. This way, any instance of any subtype of the `phone` [owned by](#_example_owns) any instance of any subtype of the `user` should comply to the defined `regex`.

Ownerships of these subtypes can have their own `regex` constraints, and they will be respected similarly down the inheritance line.

## [](#_limitations)Limitations

Is only compatible with [`string` value type](../../values/string/index.md).

[@range](../range/index.md) [@distinct](../distinct/index.md)

[Edit on GitHub](https://github.com/typedb/typedb-docs/edit/3.x-development/typeql-reference/modules/ROOT/pages/annotations/regex.adoc) Edit this page on GitHub.