A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://cplusplus.com/ECMAScript below:

syntax specifications

std::ECMAScript syntax

ECMAScript regular expressions pattern syntax

The following syntax is used to construct regex objects (or assign) that have selected ECMAScript as its grammar.

A regular expression pattern is formed by a sequence of characters.


Regular expression operations look sequentially for matches between the characters of the pattern and the characters in the target sequence: In principle, each character in the pattern is matched against the corresponding character in the target sequence, one by one. But the regex syntax allows for special characters and expressions in the pattern:

Special pattern characters Special pattern characters are characters (or sequences of characters) that have a special meaning when they appear in a regular expression pattern, either to represent a character that is difficult to express in a string, or to represent a category of characters. Each of these special pattern characters is matched in the target sequence against a single character (unless a quantifier specifies otherwise).

characters description matches . not newline any character except line terminators (LF, CR, LS, PS). \t tab (HT) a horizontal tab character (same as \u0009). \n newline (LF) a newline (line feed) character (same as \u000A). \v vertical tab (VT) a vertical tab character (same as \u000B). \f form feed (FF) a form feed character (same as \u000C). \r carriage return (CR) a carriage return character (same as \u000D). \cletter control code a control code character whose code unit value is the same as the remainder of dividing the code unit value of letter by 32.
For example: \ca is the same as \u0001, \cb the same as \u0002, and so on... \xhh ASCII character a character whose code unit value has an hex value equivalent to the two hex digits hh.
For example: \x4c is the same as L, or \x23 the same as #. \uhhhh unicode character a character whose code unit value has an hex value equivalent to the four hex digits hhhh. \0 null a null character (same as \u0000). \int backreference the result of the submatch whose opening parenthesis is the int-th (int shall begin by a digit other than 0). See groups below for more info. \d digit a decimal digit character (same as [[:digit:]]). \D not digit any character that is not a decimal digit character (same as [^[:digit:]]). \s whitespace a whitespace character (same as [[:space:]]). \S not whitespace any character that is not a whitespace character (same as [^[:space:]]). \w word an alphanumeric or underscore character (same as [_[:alnum:]]). \W not word any character that is not an alphanumeric or underscore character (same as [^_[:alnum:]]). \character character the character character as it is, without interpreting its special meaning within a regex expression.
Any character can be escaped except those which form any of the special character sequences above.
Needed for: ^ $ \ . * + ? ( ) [ ] { } | [class] character class the target character is part of the class (see character classes below) [^class] negated character class the target character is not part of the class (see character classes below)


Notice that, in C++, character and string literals also escape characters using the backslash character (\), and this affects the syntax for constructing regular expressions from such types. For example:
1
2
std::regex e1 ("\\d");  // regular expression: \d -> matches a digit character
std::regex e2 ("\\\\"); // regular expression: \\ -> matches a single backslash (\) character 

Quantifiers Quantifiers follow a character or a special pattern character. They can modify the amount of times that character is repeated in the match:
characters times effects * 0 or more The preceding atom is matched 0 or more times. + 1 or more The preceding atom is matched 1 or more times. ? 0 or 1 The preceding atom is optional (matched either 0 times or once). {int} int The preceding atom is matched exactly int times. {int,} int or more The preceding atom is matched int or more times. {min,max} between min and max The preceding atom is matched at least min times, but not more than max. By default, all these quantifiers are greedy (i.e., they take as many characters that meet the condition as possible). This behavior can be overridden to ungreedy (i.e., take as few characters that meet the condition as possible) by adding a question mark (?) after the quantifier.
For example:
Matching "(a+).*" against "aardvark" succeeds and yields aa as the first submatch.
While matching "(a+?).*" against "aardvark" also succeeds, but yields a as the first submatch.

Groups Groups allow to apply quantifiers to a sequence of characters (instead of a single character). There are two kinds of groups:
characters description effects (subpattern) Group Creates a backreference.
(?:subpattern) Passive group Does not create a backreference. When a group creates a backreference, the characters that represent the subpattern in the target sequence are stored as a submatch. Each submatch is numbered after the order of appearance of their opening parenthesis (the first submatch is number 1, the second is number 2, and so on...).

These submatches can be used in the regular expression itself to specify that the entire subpattern should appear again somewhere else (see \int in the special characters list). They can also be used in the replacement string or retrieved in the match_results object filled by some regex operations.



Assertions Assertions are conditions that do not consume characters in the target sequence: they do not describe a character, but a condition that must be fulfilled before or after a character.
characters description condition for match ^ Beginning of line Either it is the beginning of the target sequence, or follows a line terminator. $ End of line Either it is the end of the target sequence, or precedes a line terminator. \b Word boundary The previous character is a word character and the next is a non-word character (or vice-versa).
Note: The beginning and the end of the target sequence are considered here as non-word characters. \B Not a word boundary The previous and next characters are both word characters or both are non-word characters.
Note: The beginning and the end of the target sequence are considered here as non-word characters. (?=subpattern) Positive lookahead The characters following the assertion must match subpattern, but no characters are consumed. (?!subpattern) Negative lookahead The characters following the assertion must not match subpattern, but no characters are consumed.
Alternatives A pattern can include different alternatives:
character description effects | Separator Separates two alternative patterns or subpatterns. A regular expression can contain multiple alternative patterns simply by separating them with the separator operator (|): The regular expression will match if any of the alternatives match, and as soon as one does.

Subpatterns (in groups or assertions) can also use the separator operator to separate different alternatives.



Character classes A character class defines a category of characters. It is introduced by enclosing its descriptors in square brackets ([ and ]).
The regex object attempts to match the entire character class against a single character in the target sequence (unless a quantifier specifies otherwise).
The character class can contain any combination of:

Character classes support depend heavily on the regex traits used by the regex object: the regex object calls its traits's isctype member function with the appropriate arguments. For the standard regex_traits object using the default locale, see cctype for a classification of characters.

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4