Regular Expression Syntax

# Course Introduction

Regular expressions use patterns you input to match and find text. This course walks you through the specific categories of characters, operators, and delimiters you can use when defining a regular expression.

# Character Classes

Character classes match a character from a specific set.

[{"Character Classes":"[character_group]","Desc":"Finds matches for every single character within character_group. By default, it is case-sensitive.","Patterns":"[ae]","Matches":"\"a\" in \"gray\"; \"a\", \"e\" in \"lane\""},{"Character Classes":"[^character_group]","Desc":"Finds mismatches for every single character within character_group. By default, it is case-sensitive.","Patterns":"[^aei]","Matches":"\"r\", \"g\", \"n\" in \"reign\""},{"Character Classes":"[first-last]","Desc":"Finds matches for every single character within the range from the first to the last.","Patterns":"[A-Z]","Matches":"\"A\", \"B\" in \"AB123\""},{"Character Classes":".","Desc":"\\\nFinds matches for every single character except .To find the literal period character (or \\\u002E), you must precede it with an escape character (\\\\.).","Patterns":"a.e","Matches":"\"ave\" in \"nave\"\"ate\" in \"water\""},{"Character Classes":"\\\\\\\p{name}","Desc":"Finds matches for every single character in a named block, specified as a Unicode generic category or name.","Patterns":"\\\\\\\p{Lu}\\\\\\\p{IsCyrillic}","Matches":"\"C\", \"L\" in \"City Lights\" \"Д\", \"Ж\" in \"ДЖem\""},{"Character Classes":"\\\\\\\P{name}","Desc":"Finds mismatches for every single character in a named block, specified as a Unicode generic category or name.","Patterns":"\\\\\\\P{Lu}\\\\\\\P{IsCyrillic}","Matches":"\"i\", \"t\", \"y\" in \"City\" \"e\", \"m\" in \"ДЖem\""},{"Character Classes":"\\\\\\\w","Desc":"Finds word characters.","Patterns":"\\\\\\\w","Matches":"\"I\", \"D\", \"A\", \"1\", and \"3\" in \"ID A1.3\""},{"Character Classes":"\\\\\\\W","Desc":"Finds non-word characters.","Patterns":"\\\\\\\W","Matches":"\" \", \".\" in \"ID A1.3\""},{"Character Classes":"\\\\\\\s","Desc":"Finds whitespace characters.","Patterns":"\\\\\\\w\\\\\\\s","Matches":"\"D \" in \"ID A1.3\""},{"Character Classes":"\\\\\\\S","Desc":"Finds non-whitespace characters.","Patterns":"\\\\\\\s\\\\\\\S","Matches":"\" _\" in \"int __ctr\""},{"Character Classes":"\\\\\\\d","Desc":"Finds decimal numbers.","Patterns":"\\\\\\\d","Matches":"\"4\" in \"4 = IV\""},{"Character Classes":"\\\\\\\D","Desc":"Matches a non-decimal character.","Patterns":"\\\\\\\D","Matches":"\" \", \"=\", \" \", \"I\", and \"V\" in \"4 = IV\""}]

# Character Escapes

Character Escapes mean that characters following the backslash character (\\) in a regular expression should be interpreted as special characters or literals, as shown in the table below.

[{"Escaped characters":"\\\t","Desc":"Finds tab character \\\u0009.","Patterns":"(\\\\\\\w+)\\\t","Matches":"\"item1\\\t\", \"item2\\\t\" in \"item1\\\titem2\\\t\""},{"Escaped characters":"\\\r","Desc":"Finds \\\u000D, the carriage return character. (\\\r is different from \\\n, the newline character).","Patterns":"\\\r\\\n(\\\\\\\w+)","Matches":"\"\\\r\\\nThese\" in \"\\\r\\\nThese are\\\ntwo lines.\""},{"Escaped characters":"\\\v","Desc":"Finds vertical tab character \\\u000B.","Patterns":"[\\\v]{2,}","Matches":"\"\\\v\\\v\\\v\" in \"\\\v\\\v\\\v\""},{"Escaped characters":"\\\n","Desc":"Finds line break character \\\u000A.","Patterns":"\\\r\\\n(\\\\\\\w+)","Matches":"\"\\\r\\\nThese\" in \"\\\r\\\nThese are\\\ntwo lines.\""},{"Escaped characters":"\\\\\\\Nnn","Desc":"Specifies the characters using an octal expression. (nnn is configured with two or three digits).","Patterns":"\\\\\\\w\\\\\\\040\\\\\\\w","Matches":"\"a b\", \"c d\" in \"a bc d\""},{"Escaped characters":"\\\\\\\xNn","Desc":"Specifies the characters using a hexadecimal expression. (nn is configured with two digits exactly.)","Patterns":"\\\\\\\w\\\\\\\x20\\\\\\\w","Matches":"\"a b\", \"c d\" in \"a bc d\""},{"Escaped characters":"\\\\\\\uNnnn","Desc":"Finds the Unicode characters using their hexadecimal expression (nnnn, which is configured with exactly four digits).","Patterns":"\\\\\\\w\\\\\\\u0020\\\\\\\w","Matches":"\"a b\", \"c d\" in \"a bc d\""},{"Escaped characters":"\\\\\\\","Desc":"Finds a character that is not recognized as an escaped character in this table or any other table in this topic, if found. For example, \\\\* is the same as \\\x2A, and \\\\. the same as \\\x2E. This allows the regular expression engine to distinguish between linguistic elements such as * or ? and character literals represented by \\\\* or \\\?.","Patterns":"\\\\\\\d+[\\\\\\\\+-x\\\\\\\\*]\\\\\\\d+","Matches":"\"2+2\" and \"3*9\" in \"(2+2) * 3*9\""}]

# Anchors

Anchors or atomic zero-width assertions specify where to look for a match in the string. The anchors enable the regular expression engine to search for matches only at the specified locations, without passing through the string or using characters. For example, the `^` symbol allows it to search for the matches at the start of a line or string. `^http:` looks for "http:" only at the start of a line.

[{"Anchors":"^","Desc":"Finds the matches at the start of a string. If multiline is enabled, looks for matches at the start of each line.","Patterns":"^\\\\\\\d{3}","Matches":"\"901\" in \"901-333-\""},{"Anchors":"$","Desc":"Finds matches at the end of a string or before \\\n at the end of a string. If multiline is enabled, looks for matches at the end of the line or before \\\n at the end of a string.","Patterns":"-\\\\\\\d{3}$","Matches":"\"-333\" in \"-901-333\""},{"Anchors":"\\\\\\\A","Desc":"The matches must be only at the start of a string. (Not supporting multiple lines)","Patterns":"\\\\\\\A\\\\\\\d{3}","Matches":"\"901\" in \"901-333-\""},{"Anchors":"\\\\\\\Z","Desc":"The matches must be at the end of a string or before the \\\n at the end of a string.","Patterns":"-\\\\\\\d{3}\\\\\\\Z","Matches":"\"-333\" in \"-901-333\""},{"Anchors":"\\\\\\\z","Desc":"The matches must be only at the end of a string.","Patterns":"-\\\\\\\d{3}\\\\\\\z","Matches":"\"-333\" in \"-901-333\""},{"Anchors":"\\\\\\\G","Desc":"Finds a match at the point where the previous match stops. If there is no previous match, it finds a new match at the position in the string where the new match starts.","Patterns":"\\\\\\\G\\\\\\\\(\\\\\\\d\\\\\\\\)","Matches":"\"(1)\", \"(3)\", and\"(5)\" in \"(1)(3)(5)[7](9)\""},{"Anchors":"\\\\\\\b","Desc":"Finds matches on the boundary between a word character \\\w and a non-word character \\\W.","Patterns":"\\\\\\\b\\\\\\\w+\\\\\\\s\\\\\\\w+\\\\\\\b","Matches":"\"them theme\", \"them them\" in \"them theme them them\""},{"Anchors":"\\\\\\\B","Desc":"Specifies that matches should not be found on the boundary between the characters. It performs the opposite operation with the \\\\\\\b anchor.","Patterns":"\\\\\\\Bend\\\\\\\w*\\\\\\\b","Matches":"\"ends\", \"ender\" in \"end sends endure lender\""}]

# Grouping Constructs

Grouping constructs represent the subexpression of a regular expression and captures a substring of the origin string.

[{"Grouping Constructs":"(subexpression)","Desc":"Captures a matching subexpression and assigns the ordinal numbers (starting with 2).","Patterns":"(\\\\\\\w)","Matches":"\"d\", \"e\", \"e\", \"p\" of \"deep\""},{"Grouping Constructs":"(?<name>subexpression) or (?'name'subexpression)","Desc":"Captures a matching subexpression into the named group.","Patterns":"(?\\\\\\\w)\\\\\\\k<\/double><\/double>","Matches":"\"ee\" in \"deep\""},{"Grouping Constructs":"(?<name1-name2>subexpression) or (?'name1-name2'subexpression)","Desc":"Defines a balancing group. In the format of the grouping construct, name1 is the current group (optional), name2 is a previously defined group, and subexpression is any valid regular expression pattern. The definition of balancing group deletes the definition of name2 and saves the spacing between the name1 and the name2 in name1.","Patterns":"(((?'Open'\\\\\\\\()[^\\\\\\\\(\\\\\\\\)]*)+((?'Close-Open'\\\\\\\\))[^\\\\\\\\(\\\\\\\\)]*)+)*(?(Open)(?!))$","Matches":"\"((1-3)*(3-1))\" in \"3+2^((1-3)*(3-1))\""},{"Grouping Constructs":"(?:subexpression)","Desc":"Defines a non-capturing group.","Patterns":"Write(?:Line)?","Matches":"\"WriteLine\" in \"Console.WriteLine()\" \"Write\" in \"Console.Write(value)\""},{"Grouping Constructs":"(?=subexpression)","Desc":"Looks for matching patterns before the Positive Lookahead subexpression.","Patterns":"\\\\\\\b\\\\\\\w+\\\\\\\b(?=.+and.+)","Matches":"\"cats\", \"dogs\" in \"cats, dogs and some mice.\""},{"Grouping Constructs":"(?!subexpression)","Desc":"Looks for mismatched patterns before the Negative Lookahead subexpression.","Patterns":"\\\b\\\w+\\\b(?!.+and.+)","Matches":"\"and\", \"some\", \"mice\" in \"cats, dogs and some mice.\""},{"Grouping Constructs":"(?<=subexpression)","Desc":"Looks for matching patterns after the Positive Lookbehind subexpression.","Patterns":"\\\\\\\b\\\\\\\w+\\\\\\\b(?<=.+and.+)———————————\\\\\\\b\\\\\\\w+\\\\\\\b(?<=.+and.*)","Matches":"\"some\", \"mice\" in \"cats, dogs and some mice.\"————————————\"and\", \"some\", \"mice\" in \"cats, dogs and some mice.\""},{"Grouping Constructs":"(?subexpression)","Desc":"Looks for mismatched patterns after the Negative Lookbehind subexpression.","Patterns":"\\\\\\\b\\\\\\\w+\\\\\\\b(?———————————\\\\\\\b\\\\\\\w+\\\\\\\b(?","Matches":"\"cats\", \"dogs\", \"and\" in \"cats, dogs and some mice.\"————————————\"cats\", \"dogs\" in \"cats, dogs and some mice.\""},{"Grouping Constructs":"(?>subexpression)","Desc":"Atomic group.","Patterns":"(?>a|ab)c","Matches":"\"ac\" in \"ac\" Not found in \"abc\""}]

# Quantifiers

Quantifiers specify the number of instances of characters, groups, or character classes to find in matches.

[{"Quantifiers":"*","Desc":"Finds 0 or more transfer elements.","Patterns":"a.*c","Matches":"\"abcbc\" in \"abcbc\""},{"Quantifiers":"+","Desc":"Finds 1 or more transfer elements.","Patterns":"be+","Matches":"\"bee\" in \"been\", \"be\" in \"bent\""},{"Quantifiers":"?","Desc":"Finds 0 or 1 transfer element.","Patterns":"rai?","Matches":"\"rai\" in \"rain\""},{"Quantifiers":"{N}","Desc":"Finds transfer elements exactly n times.","Patterns":",\\\\\\\d{3}","Matches":"\",043\" in \"1,043.6\", \",876\", \",543\" and \",210\" in \"9,876,543,210\""},{"Quantifiers":"{N,}","Desc":"Finds transfer elements at least n times.","Patterns":"\\\\\\\d{2,}","Matches":"\"166\", \"29\", \"1930\""},{"Quantifiers":"{N,M}","Desc":"Finds transfer elements at least n times, but not more than m times.","Patterns":"\\\\\\\d{3,5}","Matches":"\"166\", \"17668\"\"19302\" in \"193024\""},{"Quantifiers":"*?","Desc":"Finds at least 0 or as few transfer elements as possible.","Patterns":"a.*?c","Matches":"\"abc\" in \"abcbc\""},{"Quantifiers":"+?","Desc":"Finds at least 1 or as few transfer elements as possible.","Patterns":"be+?","Matches":"\"be\" in \"been\", \"be\" in \"bent\""},{"Quantifiers":"??","Desc":"Finds 0 or 1 transfer element, or as few as possible.","Patterns":"rai??","Matches":"\"ra\" in \"rain\""},{"Quantifiers":"{N}?","Desc":"Finds transfer elements exactly n times.","Patterns":",\\\\\\\d{3}?","Matches":"\",043\" in \"1,043.6\", \",876\", \",543\" and \",210\" in \"9,876,543,210\""},{"Quantifiers":"{N,}?","Desc":"Finds as few transfer elements as possible, at least n times.","Patterns":"\\\\\\\d{2,}?","Matches":"\"166\", \"29\", \"1930\""},{"Quantifiers":"{N,M}?","Desc":"Finds as few transfer elements as possible, at least n to m times.","Patterns":"\\\\\\\d{3,5}?","Matches":"\"166\", \"17668\"\"193\", \"024\" in \"193024\""}]

# Backreference Constructs

Backreference Constructs are useful for identifying repeated characters or substrings within a string.

[{"index":"Backreference Constructs","0":"\\\\\\\k<name>"},{"index":"Desc","0":"Named backreference constructs. It finds the value of a named expression."},{"index":"Patterns","0":"(?\\\\\\\w)\\\\\\\k<\/char><\/char>"},{"index":"Matches","0":"\"ee\" in \"seek\""}]

# Alternation Constructs

Alternation Constructs modify the regular expressions to allow 'either/or' conditional matching.

[{"Alternation Constructs":"|","Desc":"Matches one element separated by a vertical bar (|).","Patterns":"th(e|is|at)","Matches":"\"the\", \"this\" in \"this is the day.\""},{"Alternation Constructs":"(?(expression)Yes|No) or(?(expression)Yes)","Desc":"Matches Yes if the regular expression pattern specified by the expression is matched. Otherwise, it matches No part. The expression is interpreted as an assertion with zero width.To avoid ambiguity with named or numbered capturing groups, if necessary, you can use explicit assertions as follows:(?( (?=expression) )Yes|No)","Patterns":"(?(A)A\\\\\\\d{2}\\\\\\\b|\\\\\\\b\\\\\\\d{3}\\\\\\\b)","Matches":"\"A10\", \"910\" in \"A10 C103 910\""},{"Alternation Constructs":"(?(name)Yes|No)or(?(name)Yes)","Desc":"Matches Yes if there is a match for name as a named or numbered capturing group. Otherwise, it matches No.","Patterns":"(?\\\\\")?(?(quoted).+?\\\\\"|\\\\\\\S+\\\\\\\s)<\/quoted>","Matches":"\"Dogs.jpg \", \"\\\\\"Yiska playing.jpg\\\\\"\" in \"Dogs.jpg \\\\\"Yiska playing.jpg\\\\\"\""}]

# Substitutions

Substitutions interact with replacement patterns. They define all or part of a substitute text for the matching text in the origin string.

[{"Characters":"${name}","Desc":"Substitutes the partial string that matches the named group.","Patterns":"\\\\\\\b(?\\\\\\\w+)(\\\\\\\s)(?\\\\\\\w+)\\\\\\\b<\/word2><\/word1>","Replacement Pattern":"${word2} ${word1}","Origin string":"\"one two\"","Result string":"\"two one\""},{"Characters":"$$","Desc":"Substitutes \"$\" literal.","Patterns":"\\\\\\\b(\\\\\\\d+)\\\\\\\s?USD","Replacement Pattern":"$$$1","Origin string":"\"103 USD\"","Result string":"\"$103\""},{"Characters":"$&","Desc":"Substitutes a copy of the entire string that matches.","Patterns":"\\\\\\\$?\\\\\\\d*\\\\\\\\.?\\\\\\\d+","Replacement Pattern":"**$&**","Origin string":"\"$1.30\"","Result string":"\"**$1.30**\""},{"Characters":"$`","Desc":"Substitutes all the text in the origin string before the matching string.","Patterns":"B+","Replacement Pattern":"$`","Origin string":"\"AABBCC\"","Result string":"\"AAAACC\""},{"Characters":"$'","Desc":"Substitutes all the text in the origin string after the matching string.","Patterns":"B+","Replacement Pattern":"$'","Origin string":"\"AABBCC\"","Result string":"\"AACCCC\""},{"Characters":"$+","Desc":"Substitutes the last group captured.","Patterns":"B+(C+)","Replacement Pattern":"$+","Origin string":"\"AABBCCDD\"","Result string":"\"AACCDD\""},{"Characters":"$_","Desc":"Substitutes the entire origin string.","Patterns":"B+","Replacement Pattern":"$_","Origin string":"\"AABBCC\"","Result string":"\"AAAABBCCCC\""}]