Regular Expressions 101

Save & Share

Flavor

  • PCRE2 (PHP >=7.3)
  • PCRE (PHP <7.3)
  • ECMAScript (JavaScript)
  • Python
  • Golang
  • Java 8
  • .NET 7.0 (C#)
  • Rust
  • Regex Flavor Guide

Function

  • Match
  • Substitution
  • List
  • Unit Tests

Tools

Sponsors
There are currently no sponsors. Become a sponsor today!
An explanation of your regex will be automatically generated as you type.
Detailed match information will be displayed here automatically.
  • All Tokens
  • Common Tokens
  • General Tokens
  • Anchors
  • Meta Sequences
  • Quantifiers
  • Group Constructs
  • Character Classes
  • Flags/Modifiers
  • Substitution
  • A single character of: a, b or c
    [abc]
  • A character except: a, b or c
    [^abc]
  • A character in the range: a-z
    [a-z]
  • A character not in the range: a-z
    [^a-z]
  • A character in the range: a-z or A-Z
    [a-zA-Z]
  • Any single character
    .
  • Alternate - match either a or b
    a|b
  • Any whitespace character
    \s
  • Any non-whitespace character
    \S
  • Any digit
    \d
  • Any non-digit
    \D
  • Any word character
    \w
  • Any non-word character
    \W
  • Non-capturing group
    (?:...)
  • Capturing group
    (...)
  • Zero or one of a
    a?
  • Zero or more of a
    a*
  • One or more of a
    a+
  • Exactly 3 of a
    a{3}
  • 3 or more of a
    a{3,}
  • Between 3 and 6 of a
    a{3,6}
  • Start of string
    ^
  • End of string
    $
  • A word boundary
    \b
  • Non-word boundary
    \B

Regular Expression

/
/
gms

Test String

Code Generator

Generated Code

# coding=utf8 # the above tag defines encoding for this document and is for Python 2.x compatibility import re regex = r"(?<text>N?'((?:''|[^'])*)'|N?\"((?:\"\"|[^\"])*)\")" test_str = ("USE [master]\n" "GO\n\n" "IF OBJECT_ID('dbo.sp_foreach_db') IS NOT NULL\n" " DROP PROCEDURE [dbo].[sp_foreach_db] \n" "GO\n\n" "CREATE PROCEDURE [dbo].[sp_foreach_db] \n" "(\n" " @tsql NVARCHAR(MAX) = NULL\n" " ,@replacedatabasenamepattern NVARCHAR(20) = N'?'\n\n" " ,@break_on_error BIT = 1\n\n" " ,@db_name_like NVARCHAR(128) = NULL\n" " ,@db_name_in NVARCHAR(4000) = NULL\n" " ,@db_name_not_in NVARCHAR(4000) = NULL\n" " ,@db_use_system_db BIT = 0\n" " ,@db_use_temp_db BIT = 0\n\n" " ,@push_info_msg BIT = 1\n" ")\n" "WITH EXECUTE AS CALLER \n" "AS\n" "/*\n" "Version: 1.0.1.0\n" "Developer: Paw Jershauge\n" "Created: 18-03-2016 13:01:00\n" "*/\n" "BEGIN\n" " IF @tsql IS NULL\n" " RETURN 0;\n" " \n" " SET NOCOUNT ON;\n" " DECLARE @dbtsql NVARCHAR(MAX)\n" " ,@firecode NVARCHAR(MAX)\n" " ,@db_name NVARCHAR(128)\n" " ,@dbnames NVARCHAR(MAX) = N''\n" " ,@has_replacement BIT = 0\n" " ,@msg_str NVARCHAR(2044)\n\n" " SET @dbtsql = N'SELECT @dbnames += [name] + CHAR(10) FROM [master].[sys].[databases] WHERE [state] = 0'\n" " SET @dbtsql += IIF(ISNULL(@db_use_temp_db, 0) = 1, N'', N' AND [database_id] <> 2')\n" " SET @dbtsql += IIF(ISNULL(@db_use_system_db, 0) = 1, N'', N' AND [database_id] > 4')\n" " SET @dbtsql += IIF(NULLIF(@db_name_like, N'') IS NULL, N'', N' AND [name] LIKE ''' + @db_name_like + N'''')\n" " SET @dbtsql += IIF(NULLIF(@db_name_in, N'') IS NULL, N'', N' AND [name] IN (' + @db_name_in + N')')\n" " SET @dbtsql += IIF(NULLIF(@db_name_not_in, N'') IS NULL, N'', N' AND [name] NOT IN (' + @db_name_not_in + N')')\n" " \n" " EXEC sp_executesql @dbtsql, N'@dbnames NVARCHAR(MAX) OUTPUT', @dbnames = @dbnames OUTPUT;\n\n" " SELECT @has_replacement = CAST(CHARINDEX(ISNULL(@replacedatabasenamepattern, N'?'), @tsql) AS BIT)\n" " \n" " DECLARE cur_db CURSOR LOCAL FORWARD_ONLY FAST_FORWARD READ_ONLY FOR SELECT [value] FROM STRING_SPLIT(@dbnames, CHAR(10)) WHERE [value] <> N'' ORDER BY [value]\n" " OPEN cur_db\n" " FETCH NEXT FROM cur_db INTO @db_name\n" " WHILE @@FETCH_STATUS = 0 \n" " BEGIN \n" " BEGIN TRY\n" " SELECT @firecode = N'EXEC (N''' + IIF(@has_replacement = 0,N'Use [' + @db_name + N']; ', N'') + REPLACE(REPLACE(@tsql, ISNULL(@replacedatabasenamepattern, N'?'), @db_name), N'''', N'''''') + N''')'\n" " \n" " IF @push_info_msg = 1\n" " BEGIN\n" " SET @msg_str = CONVERT(NVARCHAR(40),GETDATE(), 121) + N':Executing on ' + @db_name\n" " RAISERROR (@msg_str, 0, 1) WITH NOWAIT\n" " END\n" " EXEC sp_executesql @firecode\n" " IF @push_info_msg = 1\n" " BEGIN\n" " SET @msg_str = CONVERT(NVARCHAR(40),GETDATE(), 121) + N':' + @db_name + N' Done!'\n" " RAISERROR (@msg_str, 0, 1) WITH NOWAIT\n" " END\n" " END TRY\n" " BEGIN CATCH\n" " SET @msg_str = 'Database ' + @db_name + ' failed with the following message:'\n" " RAISERROR (@msg_str, 0, 1) WITH NOWAIT\n" " SET @msg_str = ERROR_MESSAGE()\n" " RAISERROR (@msg_str, 0, 1) WITH NOWAIT --Workaround of the PRINT Command that is stacked and released in reverse order, on remote servers\n\n" " IF @break_on_error = 1\n" " THROW\n" " END CATCH\n\n" " FETCH NEXT FROM cur_db INTO @db_name\n" " END\n" " CLOSE cur_db\n" " DEALLOCATE cur_db\n\n" "END\n" "GO\n\n" "EXEC sys.sp_MS_marksystemobject sp_foreach_db\n" "GO") matches = re.finditer(regex, test_str, re.MULTILINE | re.DOTALL) for matchNum, match in enumerate(matches, start=1): print ("Match {matchNum} was found at {start}-{end}: {match}".format(matchNum = matchNum, start = match.start(), end = match.end(), match = match.group())) for groupNum in range(0, len(match.groups())): groupNum = groupNum + 1 print ("Group {groupNum} found at {start}-{end}: {group}".format(groupNum = groupNum, start = match.start(groupNum), end = match.end(groupNum), group = match.group(groupNum))) # Note: for Python 2.7 compatibility, use ur"" to prefix the regex and u"" to prefix the test string and substitution.

Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for Python, please visit: https://docs.python.org/3/library/re.html