Skip to content

Latest commit

 

History

History
488 lines (334 loc) · 12.4 KB

basic-regex-class.md

File metadata and controls

488 lines (334 loc) · 12.4 KB
descriptiontitlems.datef1_keywordshelpviewer_keywordsms.assetidms.custom
Learn more about: basic_regex Class
basic_regex Class
06/10/2022
regex/std::basic_regex
basic_regex class
8a18c6b4-f22a-4cfd-bc16-b4267867ebc3
devdivchpfy22

basic_regex Class

Wraps a regular expression.

Syntax

template <classElem, classRXtraits> classbasic_regex

Parameters

Elem
The type of elements to match.

RXtraits
Traits class for elements.

Remarks

The class template describes an object that holds a regular expression. Objects of this class template can be passed to the template functions regex_match, regex_search, and regex_replace. It also passes the suitable text string arguments, to search for text that matches the regular expression. There are two specializations of this class template, with the type definitions regex for elements of type char, and wregex for elements of type wchar_t.

The template argument RXtraits describes various important properties of the syntax of the regular expressions that the class template supports. A class that specifies these regular expression traits must have the same external interface as an object of type regex_traits Class.

Some functions take an operand sequence that defines a regular expression. You can specify such an operand sequence several ways:

ptr: a null-terminated sequence (such as a C string, for Elem of type char) beginning at ptr (which must not be a null pointer), where the terminating element is the value value_type() and isn't part of the operand sequence

ptr, count: a sequence of count elements beginning at ptr (which must not be a null pointer)

str: the sequence specified by the basic_string object str

first, last: a sequence of elements delimited by the iterators first and last, in the range [first, last)

right: the basic_regex object right

The above member functions also take an argument flags that specifies various options for the interpretation of the regular expression in addition to options described by the RXtraits type.

Members

MemberDefault Value
public static const flag_type icaseregex_constants::icase
public static const flag_type nosubsregex_constants::nosubs
public static const flag_type optimizeregex_constants::optimize
public static const flag_type collateregex_constants::collate
public static const flag_type ECMAScriptregex_constants::ECMAScript
public static const flag_type basicregex_constants::basic
public static const flag_type extendedregex_constants::extended
public static const flag_type awkregex_constants::awk
public static const flag_type grepregex_constants::grep
public static const flag_type egrepregex_constants::egrep
private RXtraits traits

Constructors

ConstructorDescription
basic_regexConstruct the regular expression object.

Typedefs

Type nameDescription
flag_typeThe type of syntax option flags.
locale_typeThe type of the stored locale object.
value_typeThe element type.

Member functions

Member functionDescription
assignAssigns a value to the regular expression object.
flagsReturns syntax option flags.
getlocReturns the stored locale object.
imbueAlters the stored locale object.
mark_countReturns number of subexpressions matched.
swapSwaps two regular expression objects.

Operators

OperatorDescription
operator=Assigns a value to the regular expression object.

Requirements

Header: <regex>

Namespace: std

Example

// std__regex__basic_regex.cpp// compile with: /EHsc #include<regex> #include<iostream>usingnamespacestd;intmain() { regex::value_type elem = 'x'; regex::flag_type flag = regex::grep; elem = elem; // to quiet "unused" warnings flag = flag; // constructors regex rx0; cout << "match(\"abc\", \"\") == " << boolalpha << regex_match("abc", rx0) << endl; regex rx1("abcd", regex::ECMAScript); cout << "match(\"abc\", \"abcd\") == " << boolalpha << regex_match("abc", rx1) << endl; regex rx2("abcd", 3); cout << "match(\"abc\", \"abc\") == " << boolalpha << regex_match("abc", rx2) << endl; regex rx3(rx2); cout << "match(\"abc\", \"abc\") == " << boolalpha << regex_match("abc", rx3) << endl; string str("abcd"); regex rx4(str); cout << "match(string(\"abcd\"), \"abc\") == " << boolalpha << regex_match("abc", rx4) << endl; regex rx5(str.begin(), str.end() - 1); cout << "match(string(\"abc\"), \"abc\") == " << boolalpha << regex_match("abc", rx5) << endl; cout << endl; // assignments rx0 = "abc"; rx0 = rx1; rx0 = str; rx0.assign("abcd", regex::ECMAScript); rx0.assign("abcd", 3); rx0.assign(rx1); rx0.assign(str); rx0.assign(str.begin(), str.end() - 1); rx0.swap(rx1); // mark_count cout << "\"abc\" mark_count == " << regex("abc").mark_count() << endl; cout << "\"(abc)\" mark_count == " << regex("(abc)").mark_count() << endl; // locales regex::locale_type loc = rx0.imbue(locale()); cout << "getloc == imbued == " << boolalpha << (loc == rx0.getloc()) << endl; // initializer_list regex rx6({ 'a', 'b', 'c' }, regex::ECMAScript); cout << "match(\"abc\") == " << boolalpha << regex_match("abc", rx6); cout << endl; }
match("abc", "") == false match("abc", "abcd") == false match("abc", "abc") == true match("abc", "abc") == true match(string("abcd"), "abc") == false match(string("abc"), "abc") == true "abc" mark_count == 0 "(abc)" mark_count == 1 getloc == imbued == true match("abc") == true 

basic_regex::assign

Assigns a value to the regular expression object.

basic_regex& assign( const basic_regex& right); basic_regex& assign( const Elem* ptr, flag_type flags = ECMAScript); basic_regex& assign( const Elem* ptr, size_type len, flag_type flags = ECMAScript); basic_regex& assign( initializer_list<_Elem> IList, flag_type flags = regex_constants::ECMAScript); template <classSTtraits, classSTalloc> basic_regex& assign( const basic_string<Elem, STtraits, STalloc>& str, flag_type flags = ECMAScript); template <classInIt> basic_regex& assign( InIt first, InIt last, flag_type flags = ECMAScript);

Parameters

STtraits
Traits class for a string source.

STalloc
Allocator class for a string source.

InIt
Input iterator type for a range source.

right
Regex source to copy.

ptr
Pointer to beginning of sequence to copy.

flags
Syntax option flags to add while copying.

len/TD>
Length of sequence to copy.

str
String to copy.

first
Beginning of sequence to copy.

last
End of sequence to copy.

IList
The initializer_list to copy.

Remarks

The member functions each replace the regular expression held by *this with the regular expression described by the operand sequence, then return *this.

basic_regex::basic_regex

Construct the regular expression object.

basic_regex(); explicitbasic_regex( const Elem* ptr, flag_type flags); explicitbasic_regex( const Elem* ptr, size_type len, flag_type flags); basic_regex( const basic_regex& right); basic_regex( initializer_list<Type> IList, flag_type flags); template <classSTtraits, classSTalloc> explicitbasic_regex( const basic_string<Elem, STtraits, STalloc>& str, flag_type flags); template <classInIt> explicitbasic_regex( InIt first, InIt last, flag_type flags);

Parameters

STtraits
Traits class for a string source.

STalloc
Allocator class for a string source.

InIt
Input iterator type for a range source.

right
Regex source to copy.

ptr
Pointer to beginning of sequence to copy.

flags
Syntax option flags to add while copying.

len/TD>
Length of sequence to copy.

str
String to copy.

first
Beginning of sequence to copy.

last
End of sequence to copy.

IList
The initializer_list to copy.

Remarks

All constructors store a default-constructed object of type RXtraits.

The first constructor constructs an empty basic_regex object. The other constructors construct a basic_regex object that holds the regular expression described by the operand sequence.

An empty basic_regex object doesn't match any character sequence when passed to regex_match, regex_search, or regex_replace.

basic_regex::flag_type

The type of syntax option flags.

typedef regex_constants::syntax_option_type flag_type;

Remarks

The type is a synonym for regex_constants::syntax_option_type.

basic_regex::flags

Returns syntax option flags.

flag_type flags() const;

Remarks

The member function returns the value of the flag_type argument passed to the most recent call to one of the basic_regex::assign member functions or, if no such call has been made, the value passed to the constructor.

basic_regex::getloc

Returns the stored locale object.

locale_type getloc() const;

Remarks

The member function returns traits.regex_traits::getloc().

basic_regex::imbue

Alters the stored locale object.

locale_type imbue(locale_type loc);

Parameters

loc
The locale object to store.

Remarks

The member function empties *this and returns traits.regex_traits::imbue(loc).

basic_regex::locale_type

The type of the stored locale object.

typedeftypename RXtraits::locale_type locale_type;

Remarks

The type is a synonym for regex_traits::locale_type.

basic_regex::mark_count

Returns number of subexpressions matched.

unsignedmark_count() const;

Remarks

The member function returns the number of capture groups in the regular expression.

basic_regex::operator=

Assigns a value to the regular expression object.

basic_regex& operator=(const basic_regex& right); basic_regex& operator=(const Elem *str); template <classSTtraits, classSTalloc> basic_regex& operator=(const basic_string<Elem, STtraits, STalloc>& str);

Parameters

STtraits
Traits class for a string source.

STalloc
Allocator class for a string source.

right
Regex source to copy.

str
String to copy.

Remarks

The operators each replace the regular expression held by *this with the regular expression described by the operand sequence, then return *this.

basic_regex::swap

Swaps two regular expression objects.

voidswap(basic_regex& right) throw();

Parameters

right
The regular expression object to swap with.

Remarks

The member function swaps the regular expressions between *this and right. It does so in constant time and throws no exceptions.

basic_regex::value_type

The element type.

typedef Elem value_type;

Remarks

The type is a synonym for the template parameter Elem.

See also

<regex>
regex_match
regex_search
regex_replace
regex
wregex
regex_traits Class

close