---
name: "std::basic_regex"
section: "3cxx"
platform: "linux"
package: "libstdc++-12-doc"
version: "12.4.0-5"
release: "Debian 13 (Trixie)"
platforms: ["linux"]
url: "https://new.carta.tech/std::basic_regex/3cxx"
---

# std::basic\_regex(3cxx)

A regular expression.

## NAME

std::basic\_regex\< \_Ch\_type, \_Rx\_traits \> - A regular expression.

## SYNOPSIS

`#include <regex>`

### Public Types

**typedef** **regex\_constants::syntax\_option\_type** **flag\_type**\
 **typedef** traits\_type::locale\_type **locale\_type**\
 **typedef** traits\_type::string\_type **string\_type**\
 **typedef** **\_Rx\_traits** **traits\_type**\
 **typedef** **\_Ch\_type** **value\_type**\

### Public Member Functions

**basic\_regex** () noexcept\
 template\<**typename** **\_FwdIter** \> **basic\_regex** (**\_FwdIter** \_\_first, **\_FwdIter** \_\_last, **flag\_type** \_\_f=ECMAScript)\
 Constructs a basic regular expression from the range \[first, last) interpreted according to the flags in `f`.  **basic\_regex** (**basic\_regex** &&**\_\_rhs**) noexcept=**default**\
 Move-constructs a basic regular expression.  **basic\_regex** (const **\_Ch\_type** \*\_\_p, **flag\_type** \_\_f=ECMAScript)\
 Constructs a basic regular expression from the sequence \[\_\_p, \_\_p + char\_traits\<\_Ch\_type\>::length(\_\_p)) interpreted according to the flags in `__f`.  **basic\_regex** (const **\_Ch\_type** \*\_\_p, std::size\_t **\_\_len**, **flag\_type** \_\_f=ECMAScript)\
 Constructs a basic regular expression from the sequence \[p, p + len) interpreted according to the flags in `f`.  **basic\_regex** (const **basic\_regex** &**\_\_rhs**)=**default**\
 Copy-constructs a basic regular expression.  template\<**typename** **\_Ch\_traits** , **typename** **\_Ch\_alloc** \> **basic\_regex** (const **std::basic\_string**\< **\_Ch\_type**, **\_Ch\_traits**, **\_Ch\_alloc** \> &**\_\_s**, **flag\_type** \_\_f=ECMAScript)\
 Constructs a basic regular expression from the string `s` interpreted according to the flags in `f`.  **basic\_regex** (**initializer\_list**\< **\_Ch\_type** \> **\_\_l**, **flag\_type** \_\_f=ECMAScript)\
 Constructs a basic regular expression from an initializer list.  **~basic\_regex** ()\
 Destroys a basic regular expression.  template\<**typename** **\_InputIterator** \> **basic\_regex** & **assign** (**\_InputIterator** \_\_first, **\_InputIterator** \_\_last, **flag\_type** \_\_flags=ECMAScript)\
 Assigns a new regular expression to a regex object.  **basic\_regex** & **assign** (**basic\_regex** &&**\_\_rhs**) noexcept\
 Move-assigns one regular expression to another.  **basic\_regex** & **assign** (const **\_Ch\_type** \*\_\_p, **flag\_type** \_\_flags=ECMAScript)\
 Assigns a new regular expression to a regex object from a C-style null-terminated string containing a regular expression pattern.  **basic\_regex** & **assign** (const **\_Ch\_type** \*\_\_p, size\_t **\_\_len**, **flag\_type** \_\_flags=ECMAScript)\
 Assigns a new regular expression to a regex object from a C-style string containing a regular expression pattern.  **basic\_regex** & **assign** (const **basic\_regex** &**\_\_rhs**) noexcept\
 Assigns one regular expression to another.  template\<**typename** **\_Ch\_traits** , **typename** \_Alloc \> **basic\_regex** & **assign** (const **basic\_string**\< **\_Ch\_type**, **\_Ch\_traits**, \_Alloc \> &**\_\_s**, **flag\_type** \_\_flags=ECMAScript)\
 Assigns a new regular expression to a regex object from a string containing a regular expression pattern.  **basic\_regex** & **assign** (**initializer\_list**\< **\_Ch\_type** \> **\_\_l**, **flag\_type** \_\_flags=ECMAScript)\
 Assigns a new regular expression to a regex object.  **flag\_type** **flags** () const noexcept\
 Gets the flags used to construct the regular expression or in the last call to assign().  locale\_type **getloc** () const noexcept\
 Gets the locale currently imbued in the regular expression object.  locale\_type **imbue** (locale\_type **\_\_loc**)\
 Imbues the regular expression object with the given locale.  unsigned int **mark\_count** () const noexcept\
 Gets the number of marked subexpressions within the regular expression.  **basic\_regex** & **operator=** (**basic\_regex** &&)=**default**\
 Move-assigns one regular expression to another.  **basic\_regex** & **operator=** (const **\_Ch\_type** \*\_\_p)\
 Replaces a regular expression with a new one constructed from a C-style null-terminated string.  **basic\_regex** & **operator=** (const **basic\_regex** &)=**default**\
 Assigns one regular expression to another.  template\<**typename** **\_Ch\_traits** , **typename** \_Alloc \> **basic\_regex** & **operator=** (const **basic\_string**\< **\_Ch\_type**, **\_Ch\_traits**, \_Alloc \> &**\_\_s**)\
 Replaces a regular expression with a new one constructed from a string.  **basic\_regex** & **operator=** (**initializer\_list**\< **\_Ch\_type** \> **\_\_l**)\
 Replaces a regular expression with a new one constructed from an initializer list.  **void** **swap** (**basic\_regex** &**\_\_rhs**) noexcept\
 Swaps the contents of two regular expression objects.

### Static Public Attributes

**Constants**\
 std \[28.8.1\](1)

**static** constexpr **flag\_type** **icase**\
 **static** constexpr **flag\_type** **nosubs**\
 **static** constexpr **flag\_type** **optimize**\
 **static** constexpr **flag\_type** **collate**\
 **static** constexpr **flag\_type** **ECMAScript**\
 **static** constexpr **flag\_type** **basic**\
 **static** constexpr **flag\_type** **extended**\
 **static** constexpr **flag\_type** **awk**\
 **static** constexpr **flag\_type** **grep**\
 **static** constexpr **flag\_type** **egrep**\
 **static** constexpr **flag\_type** **multiline**\

### Friends

template\<**typename** \_Bp , **typename** \_Ap , **typename** **\_Cp** , **typename** **\_Rp** \> bool **\_\_detail::\_\_regex\_algo\_impl** (\_Bp, \_Bp, **match\_results**\< \_Bp, \_Ap \> &, const **basic\_regex**\< **\_Cp**, **\_Rp** \> &, **regex\_constants::match\_flag\_type**, \_\_detail::\_RegexExecutorPolicy, bool)\
 template\<**typename** , **typename** , **typename** , bool \> **class** **\_\_detail::\_Executor**\

### Related Symbols

(Note that these are not member symbols.)  template\<**typename** **\_Ch\_type** , **typename** **\_Rx\_traits** \> **void** **swap** (**basic\_regex**\< **\_Ch\_type**, **\_Rx\_traits** \> &**\_\_lhs**, **basic\_regex**\< **\_Ch\_type**, **\_Rx\_traits** \> &**\_\_rhs**) noexcept\
 Swaps the contents of two regular expression objects.

## Detailed Description

### template\<typename \_Ch\_type, typename \_Rx\_traits = regex\_traits\<\_Ch\_type\>\>

class std::basic\_regex\< \_Ch\_type, \_Rx\_traits \>"A regular expression.

Specializations of this class template represent regular expressions constructed from sequences of character type `_Ch_type`. Use the `std::regex` typedef for `std::basic_regex<char>`.

A character sequence passed to the constructor will be parsed according to the chosen grammar, and used to create a state machine representing the regular expression. The regex object can then be passed to algorithms such as `std::regex_match` to match sequences of characters.

The `syntax_option_type` flag passed to the constructor selects from one of the supported regular expression grammars. The default is `ECMAScript` and the others are `basic`, `extended`, `awk`, `grep`, and `egrep`, which are variations on POSIX regular expressions.

**Since**

C++11

## Constructor & Destructor Documentation

### template\<typename \_Ch\_type , typename \_Rx\_traits = regex\_traits\<\_Ch\_type\>\> std::basic\_regex\< \_Ch\_type, \_Rx\_traits \>::basic\_regex ()` [inline]`, ` [noexcept]`

Constructs a basic regular expression that does not match any character sequence.

### template\<typename \_Ch\_type , typename \_Rx\_traits = regex\_traits\<\_Ch\_type\>\> std::basic\_regex\< \_Ch\_type, \_Rx\_traits \>::basic\_regex (const \_Ch\_type \* \_\_p, flag\_type \_\_f = `ECMAScript`)` [inline]`, ` [explicit]`

Constructs a basic regular expression from the sequence \[\_\_p, \_\_p + char\_traits\<\_Ch\_type\>::length(\_\_p)) interpreted according to the flags in `__f`.

**Parameters**

*\_\_p* A pointer to the start of a C-style null-terminated string containing a regular expression. \
 *\_\_f* Flags indicating the syntax rules and options.

**Exceptions**

*regex\_error* if `__p` is not a valid regular expression.

### template\<typename \_Ch\_type , typename \_Rx\_traits = regex\_traits\<\_Ch\_type\>\> std::basic\_regex\< \_Ch\_type, \_Rx\_traits \>::basic\_regex (const \_Ch\_type \* \_\_p, std::size\_t \_\_len, flag\_type \_\_f = `ECMAScript`)` [inline]`

Constructs a basic regular expression from the sequence \[p, p + len) interpreted according to the flags in `f`.

**Parameters**

*\_\_p* A pointer to the start of a string containing a regular expression. \
 *\_\_len* The length of the string containing the regular expression. \
 *\_\_f* Flags indicating the syntax rules and options.

**Exceptions**

*regex\_error* if `__p` is not a valid regular expression.

### template\<typename \_Ch\_type , typename \_Rx\_traits = regex\_traits\<\_Ch\_type\>\> std::basic\_regex\< \_Ch\_type, \_Rx\_traits \>::basic\_regex (const basic\_regex\< \_Ch\_type, \_Rx\_traits \> & \_\_rhs)` [default]`

Copy-constructs a basic regular expression.

**Parameters**

*\_\_rhs* A `regex` object.

### template\<typename \_Ch\_type , typename \_Rx\_traits = regex\_traits\<\_Ch\_type\>\> std::basic\_regex\< \_Ch\_type, \_Rx\_traits \>::basic\_regex (basic\_regex\< \_Ch\_type, \_Rx\_traits \> && \_\_rhs)` [default]`, ` [noexcept]`

Move-constructs a basic regular expression.

**Parameters**

*\_\_rhs* A `regex` object.

### template\<typename \_Ch\_type , typename \_Rx\_traits = regex\_traits\<\_Ch\_type\>\> template\<typename \_Ch\_traits , typename \_Ch\_alloc \> std::basic\_regex\< \_Ch\_type, \_Rx\_traits \>::basic\_regex (const std::basic\_string\< \_Ch\_type, \_Ch\_traits, \_Ch\_alloc \> & \_\_s, flag\_type \_\_f = `ECMAScript`)` [inline]`, ` [explicit]`

Constructs a basic regular expression from the string `s` interpreted according to the flags in `f`.

**Parameters**

*\_\_s* A string containing a regular expression. \
 *\_\_f* Flags indicating the syntax rules and options.

**Exceptions**

*regex\_error* if `__s` is not a valid regular expression.

### template\<typename \_Ch\_type , typename \_Rx\_traits = regex\_traits\<\_Ch\_type\>\> template\<typename \_FwdIter \> std::basic\_regex\< \_Ch\_type, \_Rx\_traits \>::basic\_regex (\_FwdIter \_\_first, \_FwdIter \_\_last, flag\_type \_\_f = `ECMAScript`)` [inline]`

Constructs a basic regular expression from the range \[first, last) interpreted according to the flags in `f`.

**Parameters**

*\_\_first* The start of a range containing a valid regular expression. \
 *\_\_last* The end of a range containing a valid regular expression. \
 *\_\_f* The format flags of the regular expression.

**Exceptions**

*regex\_error* if \[\_\_first, \_\_last) is not a valid regular expression.

References **std::basic\_regex\< \_Ch\_type, \_Rx\_traits \>::assign()**.

### template\<typename \_Ch\_type , typename \_Rx\_traits = regex\_traits\<\_Ch\_type\>\> std::basic\_regex\< \_Ch\_type, \_Rx\_traits \>::basic\_regex (initializer\_list\< \_Ch\_type \> \_\_l, flag\_type \_\_f = `ECMAScript`)` [inline]`

Constructs a basic regular expression from an initializer list.

**Parameters**

*\_\_l* The initializer list. \
 *\_\_f* The format flags of the regular expression.

**Exceptions**

*regex\_error* if `__l` is not a valid regular expression.

### template\<typename \_Ch\_type , typename \_Rx\_traits = regex\_traits\<\_Ch\_type\>\> std::basic\_regex\< \_Ch\_type, \_Rx\_traits \>::~basic\_regex ()` [inline]`

Destroys a basic regular expression.

## Member Function Documentation

### template\<typename \_Ch\_type , typename \_Rx\_traits = regex\_traits\<\_Ch\_type\>\> template\<typename \_InputIterator \> basic\_regex & std::basic\_regex\< \_Ch\_type, \_Rx\_traits \>::assign (\_InputIterator \_\_first, \_InputIterator \_\_last, flag\_type \_\_flags = `ECMAScript`)` [inline]`

Assigns a new regular expression to a regex object.

**Parameters**

*\_\_first* The start of a range containing a valid regular expression. \
 *\_\_last* The end of a range containing a valid regular expression. \
 *\_\_flags* Syntax option flags.

**Exceptions**

*regex\_error* if p does not contain a valid regular expression pattern interpreted according to `__flags`. If regex\_error is thrown, the object remains unchanged.

References **std::basic\_regex\< \_Ch\_type, \_Rx\_traits \>::assign()**.

### template\<typename \_Ch\_type , typename \_Rx\_traits = regex\_traits\<\_Ch\_type\>\> basic\_regex & std::basic\_regex\< \_Ch\_type, \_Rx\_traits \>::assign (basic\_regex\< \_Ch\_type, \_Rx\_traits \> && \_\_rhs)` [inline]`, ` [noexcept]`

Move-assigns one regular expression to another.

**Parameters**

*\_\_rhs* Another regular expression object.

References **std::move()**.

### template\<typename \_Ch\_type , typename \_Rx\_traits = regex\_traits\<\_Ch\_type\>\> basic\_regex & std::basic\_regex\< \_Ch\_type, \_Rx\_traits \>::assign (const \_Ch\_type \* \_\_p, flag\_type \_\_flags = `ECMAScript`)` [inline]`

Assigns a new regular expression to a regex object from a C-style null-terminated string containing a regular expression pattern.

**Parameters**

*\_\_p* A pointer to a C-style null-terminated string containing a regular expression pattern. \
 *\_\_flags* Syntax option flags.

**Exceptions**

*regex\_error* if \_\_p does not contain a valid regular expression pattern interpreted according to `__flags`. If regex\_error is thrown, \*this remains unchanged.

### template\<typename \_Ch\_type , typename \_Rx\_traits = regex\_traits\<\_Ch\_type\>\> basic\_regex & std::basic\_regex\< \_Ch\_type, \_Rx\_traits \>::assign (const \_Ch\_type \* \_\_p, size\_t \_\_len, flag\_type \_\_flags = `ECMAScript`)` [inline]`

Assigns a new regular expression to a regex object from a C-style string containing a regular expression pattern.

**Parameters**

*\_\_p* A pointer to a C-style string containing a regular expression pattern. \
 *\_\_len* The length of the regular expression pattern string. \
 *\_\_flags* Syntax option flags.

**Exceptions**

*regex\_error* if p does not contain a valid regular expression pattern interpreted according to `__flags`. If regex\_error is thrown, \*this remains unchanged.

### template\<typename \_Ch\_type , typename \_Rx\_traits = regex\_traits\<\_Ch\_type\>\> basic\_regex & std::basic\_regex\< \_Ch\_type, \_Rx\_traits \>::assign (const basic\_regex\< \_Ch\_type, \_Rx\_traits \> & \_\_rhs)` [inline]`, ` [noexcept]`

Assigns one regular expression to another.

**Parameters**

*\_\_rhs* Another regular expression object.

Referenced by **std::basic\_regex\< \_Ch\_type, \_Rx\_traits \>::basic\_regex()**, **std::basic\_regex\< \_Ch\_type, \_Rx\_traits \>::assign()**, **std::basic\_regex\< \_Ch\_type, \_Rx\_traits \>::operator=()**, **std::basic\_regex\< \_Ch\_type, \_Rx\_traits \>::operator=()**, and **std::basic\_regex\< \_Ch\_type, \_Rx\_traits \>::operator=()**.

### template\<typename \_Ch\_type , typename \_Rx\_traits = regex\_traits\<\_Ch\_type\>\> template\<typename \_Ch\_traits , typename \_Alloc \> basic\_regex & std::basic\_regex\< \_Ch\_type, \_Rx\_traits \>::assign (const basic\_string\< \_Ch\_type, \_Ch\_traits, \_Alloc \> & \_\_s, flag\_type \_\_flags = `ECMAScript`)` [inline]`

Assigns a new regular expression to a regex object from a string containing a regular expression pattern.

**Parameters**

*\_\_s* A string containing a regular expression pattern. \
 *\_\_flags* Syntax option flags.

**Exceptions**

*regex\_error* if \_\_s does not contain a valid regular expression pattern interpreted according to `__flags`. If regex\_error is thrown, \*this remains unchanged.

### template\<typename \_Ch\_type , typename \_Rx\_traits = regex\_traits\<\_Ch\_type\>\> basic\_regex & std::basic\_regex\< \_Ch\_type, \_Rx\_traits \>::assign (initializer\_list\< \_Ch\_type \> \_\_l, flag\_type \_\_flags = `ECMAScript`)` [inline]`

Assigns a new regular expression to a regex object.

**Parameters**

*\_\_l* An initializer list representing a regular expression. \
 *\_\_flags* Syntax option flags.

**Exceptions**

*regex\_error* if `__l` does not contain a valid regular expression pattern interpreted according to `__flags`. If regex\_error is thrown, the object remains unchanged.

### template\<typename \_Ch\_type , typename \_Rx\_traits = regex\_traits\<\_Ch\_type\>\> flag\_type std::basic\_regex\< \_Ch\_type, \_Rx\_traits \>::flags () const` [inline]`, ` [noexcept]`

Gets the flags used to construct the regular expression or in the last call to assign().

### template\<typename \_Ch\_type , typename \_Rx\_traits = regex\_traits\<\_Ch\_type\>\> locale\_type std::basic\_regex\< \_Ch\_type, \_Rx\_traits \>::getloc () const` [inline]`, ` [noexcept]`

Gets the locale currently imbued in the regular expression object.

### template\<typename \_Ch\_type , typename \_Rx\_traits = regex\_traits\<\_Ch\_type\>\> locale\_type std::basic\_regex\< \_Ch\_type, \_Rx\_traits \>::imbue (locale\_type \_\_loc)` [inline]`

Imbues the regular expression object with the given locale.

**Parameters**

*\_\_loc* A locale.

References **std::swap()**.

### template\<typename \_Ch\_type , typename \_Rx\_traits = regex\_traits\<\_Ch\_type\>\> unsigned int std::basic\_regex\< \_Ch\_type, \_Rx\_traits \>::mark\_count () const` [inline]`, ` [noexcept]`

Gets the number of marked subexpressions within the regular expression.

### template\<typename \_Ch\_type , typename \_Rx\_traits = regex\_traits\<\_Ch\_type\>\> basic\_regex & std::basic\_regex\< \_Ch\_type, \_Rx\_traits \>::operator= (basic\_regex\< \_Ch\_type, \_Rx\_traits \> &&)` [default]`

Move-assigns one regular expression to another.

### template\<typename \_Ch\_type , typename \_Rx\_traits = regex\_traits\<\_Ch\_type\>\> basic\_regex & std::basic\_regex\< \_Ch\_type, \_Rx\_traits \>::operator= (const \_Ch\_type \* \_\_p)` [inline]`

Replaces a regular expression with a new one constructed from a C-style null-terminated string.

**Parameters**

*\_\_p* A pointer to the start of a null-terminated C-style string containing a regular expression.

References **std::basic\_regex\< \_Ch\_type, \_Rx\_traits \>::assign()**.

### template\<typename \_Ch\_type , typename \_Rx\_traits = regex\_traits\<\_Ch\_type\>\> basic\_regex & std::basic\_regex\< \_Ch\_type, \_Rx\_traits \>::operator= (const basic\_regex\< \_Ch\_type, \_Rx\_traits \> &)` [default]`

Assigns one regular expression to another.

### template\<typename \_Ch\_type , typename \_Rx\_traits = regex\_traits\<\_Ch\_type\>\> template\<typename \_Ch\_traits , typename \_Alloc \> basic\_regex & std::basic\_regex\< \_Ch\_type, \_Rx\_traits \>::operator= (const basic\_string\< \_Ch\_type, \_Ch\_traits, \_Alloc \> & \_\_s)` [inline]`

Replaces a regular expression with a new one constructed from a string.

**Parameters**

*\_\_s* A pointer to a string containing a regular expression.

References **std::basic\_regex\< \_Ch\_type, \_Rx\_traits \>::assign()**.

### template\<typename \_Ch\_type , typename \_Rx\_traits = regex\_traits\<\_Ch\_type\>\> basic\_regex & std::basic\_regex\< \_Ch\_type, \_Rx\_traits \>::operator= (initializer\_list\< \_Ch\_type \> \_\_l)` [inline]`

Replaces a regular expression with a new one constructed from an initializer list.

**Parameters**

*\_\_l* The initializer list.

**Exceptions**

*regex\_error* if `__l` is not a valid regular expression.

References **std::basic\_regex\< \_Ch\_type, \_Rx\_traits \>::assign()**.

### template\<typename \_Ch\_type , typename \_Rx\_traits = regex\_traits\<\_Ch\_type\>\> void std::basic\_regex\< \_Ch\_type, \_Rx\_traits \>::swap (basic\_regex\< \_Ch\_type, \_Rx\_traits \> & \_\_rhs)` [inline]`, ` [noexcept]`

Swaps the contents of two regular expression objects.

**Parameters**

*\_\_rhs* Another regular expression object.

References **std::swap()**.

## Author

Generated automatically by Doxygen for libstdc++ from the source code.
