This may grow larger and may get reorganized as I learn more and more. My key reason for doing this is to make it easy for me to see the most important regex building blocks. I don’t know where this will go, but I am not trying to create a complete RegEx documentation. I have linked in additional references at the end of this page for sites that have.

Note: SQL Server supports the RE2 set of expressions. There are other expression sets that you may come across, but not everything will work on all types of expressions

Send an email to louis@drsql.org if you have any changes you think out to be made.

ValueMeaning
string of characters A filter to find patterns in a string. Some characters need to be escaped using \
^ Two meanings:
– Start of string in search filter
– as the first character in a character class to look for characters not in the class.
$End of string in a search filter
\bWord boundary in a search filter
\wA word alphanumeric character
* Repeats the previous value 0 or more times. For example \w* matches any 0 or more-word character
.Any single character
/Escape character (may also start regex commands)
\Starts regex commands
{N,M}Use to repeat a pattern between N and M times. like a{1,3] looks for a, aa, or aaa.
+In a string, this represents {1,Infinity}, so it will repeat your pattern as much as it matches, so where [0-9] will match 0123 4 times, [0-9]+ will only match it once for the whole string.
?Can be used similarly to +, but matches the preceding pattern {0,Infinity} times
|This pattern or that pattern. Example: this|that would match either ‘this’ or ‘that’
(<pattern>)Parentheses are used to group together parts of a pattern when you need to repeat them, Like (12){1,3} looks for 1-3 instances of 12 in an input
[ ]Used to denote a character class.

Used to match a pattern of 1 or more characters or ranges to a single character. For example: [agh] would match a, g or h. [a-cx-z] would match a, b, c, x, y, or z. You can mix ranges and characters like [abcd-z] would match any alpha character, just like [a-z] would.
Whitespace characters\t – tab
\n – newline, linefeed
\r – carriage return
\f – page break/form feed
Perl Character Classes\d – digits = [0-9]
\D – not digits = [^0-9]
\s – whitepsace = [\t\n\f\r ]
\S – not whitepace = [^\t\n\f\r ]
\w – word characters = [0-9a-z_]
\W – not word characters = [^0-9a-z_]
Supported Flags in SQL Serveri – insensitive
c – case sensitive
m – ^ and $ match end of line, not entire string
s – single line, dot matches newline

SQL Server RegEx Functions

REGEXP_LIKE – Comparison function that lets you find a match to a RegEx expressionSyntax:
REGEXP_LIKE (
string_expression, –input string
pattern_expression –pattern to match
–optional parameter
[, flags ] –flags, as noted earlier in this sheet
)
REGEXP_MATCHES – Table valued function that shows you all of the matches your RegEx expression has with an input value.Syntax:
REGEXP_MATCHES (
string_expression, –input string
pattern_expression –pattern to match
–optional parameter
[, flags ] –flags, as noted earlier in this sheet
)
REGEXP_REPLACE – Function that takes an input string, and replaces some text from the string wherever the RegEx expression matches. It has the ability to start at a location in the string, and replace only a certain iteration of the match as well.Syntax:
REGEXP_REPLACE (
string_expression, –input string
pattern_expression –pattern to match
–optional parameters
[, string_replacement –what to replace with
[, start –location to start matching
[, occurrence –can only replace a single value
[, flags –flags, as noted earlier in this sheet
] ] ] ]
)

Tool for testing Regular expressions:

RegEx 101

Other references, intro or deeper, more complete references:

Microsoft Learning – Work with Regular Expressions

RE2 E

Regex Cheat Sheet – RexEgg

Regular expression syntax cheat sheet – JavaScript | MDN – Developer.Mozilla

I’m Louis

I have been at this database thing for a long long time, with no plans to stop.

This is my blog site, companion to drsql.org

Recents