regexpPattern

Creates a pattern object from a regular expression.

Category: Language Fundamentals > Data Types

Syntax

p = regexpPattern(expr)

Returns a pattern that matches the same text as the regular expression expr.

Description

This is the bridge between the two matching styles: write the hard part as a regular expression, then use it inside a larger pattern expression, or pass it to functions such as extract, contains and replace that take patterns.

Input arguments

expr
Regular expression as text.

Output arguments

p
A pattern object that can be combined with other patterns using + and |.

Example

p = regexpPattern('[0-9]+');
extract("ab12cd345", p)
matches(["abc", "a.c", "ac"], regexpPattern('a.c'))
Output
ans = 2×1 string array

    "12"
    "345"
ans = 1×3 logical array

   1   1   0

See also

pattern regexp extract matches digitsPattern