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 (1)

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

/
/
gis

Test String

Code Generator

Generated Code

import java.util.regex.Matcher; import java.util.regex.Pattern; public class Example { public static void main(String[] args) { final String regex = "(?:Public|Private|Friend) (?:Shared )?(?:Overloads |Overrides )?(?:Sub|Function) (?<func>.*?)\\(.+?\\)(?: Implements )?.*?End (?:Sub|Function)"; final String string = "Imports System.Windows.Forms\n" + "Imports System.Drawing\n" + "Imports CapReportLib\n" + "Imports ReportControls.Database\n" + "Imports System.Threading\n" + "Imports System.ComponentModel\n\n" + "Public Class Report : Implements IReport\n" + "#Region \"Fields\"\n" + " Friend MyReportRunTime As TimeSpan\n" + " Friend MyLastException As New Exception(\"No Errors\")\n" + " Friend RepParams As New ReportParameters()\n" + " Friend Shared DBA As ReportControls.Database.DataAccess\n" + " Private lastResult As New ReportResult(False, New TimeSpan, String.Empty)\n" + " Private Shared delRunReport As IReport.Execute = Nothing\n\n\n" + " Private repSync As ISynchronizeInvoke\n" + " Private repThread As Thread\n" + " Private repHandler As IReport.ReportHandler\n\n" + " Private repParamsSync As ISynchronizeInvoke\n" + " Private threadIsRunning As Boolean = False\n" + " Private Delegate Sub EnableCsvHandler(ByVal enable As Boolean, ByVal value As String)\n" + " Private enableCsv As EnableCsvHandler\n" + " Private csvData As String = String.Empty\n\n" + " Private startDate As Date = Now\n" + " Private endDate As Date = Now\n" + " Private origin As ReportControls.Types.OriginItem\n" + "#End Region\n\n" + "#Region \"Threading\"\n" + " Private Sub RunReportThread()\n" + " threadIsRunning = True\n" + " Dim rs = ExecuteReport()\n" + " If repThread.ThreadState <> ThreadState.AbortRequested Then\n" + " repSync.Invoke(repHandler, New Object() {rs})\n" + " repParamsSync.Invoke(enableCsv, New Object() {True, csvData})\n" + " End If\n" + " threadIsRunning = False\n" + " End Sub\n\n" + " Public Sub ExecuteReportThread() Implements IReport.ExecuteReportThread\n" + " repParamsSync = RepParams\n" + " enableCsv = AddressOf RepParams.EnableCsvExport\n" + " repThread = New Thread(AddressOf RunReportThread)\n" + " repThread.Start()\n" + " End Sub\n\n" + " Public Sub SetReportHandler(ByVal rh As IReport.ReportHandler) Implements IReport.SetReportHandler\n" + " repHandler = rh\n" + " End Sub\n\n" + " Public Sub StopReport() Implements IReport.StopReport\n" + " If repThread Is Nothing Then Return\n\n" + " If repThread.IsAlive Then\n" + " repThread.Abort()\n" + " End If\n" + " End Sub\n\n" + " Public Sub SetSyncInvoke(ByVal syn As ISynchronizeInvoke) Implements IReport.SetSyncInvoke\n" + " repSync = syn\n" + " End Sub\n\n" + " Public Sub SetReportHandler(ByVal rh As IReport.ReportHandler, ByVal syn As ISynchronizeInvoke) _\n" + " Implements IReport.SetReportHandler\n" + " repSync = syn\n" + " repHandler = rh\n" + " End Sub\n\n" + "#End Region\n\n" + "#Region \"Initialize\"\n" + " Public Sub SetOutPut(ByVal pnlControls As Panel) Implements IReport.SetOutPut\n" + " RepParams.Parent = pnlControls\n" + " RepParams.Dock = DockStyle.Fill\n" + " RepParams.Show()\n" + " End Sub\n\n" + " Public Sub SetReportDataAccess(ByVal connectionString As String) Implements CapReportLib.IReport.SetReportConnectionString\n" + " SetDataAccess(New ReportControls.Database.DataAccess(connectionString))\n" + " End Sub\n\n" + " Public Sub SetDataAccess(ByVal db As DataAccess)\n" + " DBA = db\n\n" + " SetOrigins()\n" + " End Sub\n\n" + " Private Sub SetOrigins()\n" + " RepParams.OriginSelector.SetConnectionString(DBA.ConnectionString)\n" + " End Sub\n\n" + " Public Sub SetReportExecute(ByVal exc As IReport.Execute) Implements IReport.SetReportExecute\n" + " delRunReport = exc\n" + " End Sub\n\n" + " Public Sub Dispose() Implements IReport.Dispose\n" + " DBA.Dispose()\n" + " RepParams.Dispose()\n" + " End Sub\n\n" + "#End Region\n\n" + "#Region \"Run Report\"\n" + " Public Function ExecuteReport() As ReportResult Implements IReport.ExecuteReport\n" + " RepParams.CsvButton1.EnableCsvExport(False, String.Empty)\n" + " Dim success As Boolean = True\n" + " Dim startRun = Now\n" + " Dim repData As String = String.Empty\n" + " Try\n" + " If HasValidParametersSelected Then\n" + " \n" + " Dim rep As New DocsStoredReport(startDate, endDate)\n\n\n" + " repData = rep.Execute(origin, RepParams.ChkRunOriginal.Checked)\n\n" + " csvData = rep.CsvOutput\n" + " If Not threadIsRunning Then\n" + " RepParams.CsvButton1.EnableCsvExport(True, rep.CsvOutput)\n" + " End If\n\n" + " End If\n" + " Catch ex As Exception\n" + " success = False\n" + " MyLastException = ex\n" + " End Try\n" + " Dim endRun = Now\n" + " lastResult = New ReportResult(success, endRun - startRun, repData)\n" + " Return lastResult\n" + " End Function\n\n" + " Friend Shared Sub RunReport()\n" + " If delRunReport Is Nothing Then Return\n" + " delRunReport.Invoke()\n" + " End Sub\n\n" + "#End Region\n\n" + "#Region \"Format Output\"\n" + " Public Sub FormatText(ByRef disp As RichTextBox) Implements IReport.FormatText\n" + " Dim iFont As New Font(\"Courier New\", 12, FontStyle.Regular, GraphicsUnit.Pixel)\n" + " disp.SelectAll()\n" + " disp.SelectionFont = iFont\n" + " 'disp.SelectionAlignment = HorizontalAlignment.Center\n\n" + " If disp.Find(DocsStoredReport.Header) = -1 Then Return\n\n" + " disp.SelectionStart = disp.Find(DocsStoredReport.Header)\n" + " disp.SelectionLength = DocsStoredReport.Header.Length\n" + " iFont = New Font(\"Courier New\", 18, FontStyle.Bold, GraphicsUnit.Pixel)\n" + " disp.SelectionFont = iFont\n" + " 'disp.SelectionAlignment = HorizontalAlignment.Center\n\n" + " disp.SelectionStart = disp.Find(DocsStoredReport.DateRange)\n" + " disp.SelectionLength = DocsStoredReport.DateRange.Length\n" + " iFont = New Font(\"Courier New\", 18, FontStyle.Bold, GraphicsUnit.Pixel)\n" + " disp.SelectionFont = iFont\n" + " 'disp.SelectionAlignment = HorizontalAlignment.Center\n\n\n" + " disp.SelectionStart = disp.Find(DocsStoredReport.SubHeader)\n" + " disp.SelectionLength = DocsStoredReport.SubHeader.Length\n" + " iFont = New Font(\"Courier New\", 12, FontStyle.Bold Xor FontStyle.Underline, GraphicsUnit.Pixel)\n" + " disp.SelectionFont = iFont\n" + " 'disp.SelectionAlignment = HorizontalAlignment.Center\n\n" + " disp.SelectionStart = disp.Find(\"Export Files\")\n" + " disp.SelectionLength = \"Export Files\".Length\n" + " iFont = New Font(\"courier new\", 12, FontStyle.Bold, GraphicsUnit.Pixel)\n" + " disp.SelectionFont = iFont\n\n\n" + " disp.SelectionStart = disp.Find(ExportFile.HeaderRow(False))\n" + " disp.SelectionLength = ExportFile.HeaderRow(False).Length\n" + " iFont = New Font(\"Courier New\", 12, FontStyle.Bold Xor FontStyle.Underline, GraphicsUnit.Pixel)\n" + " disp.SelectionFont = iFont\n" + " End Sub\n\n" + " Public Sub FormatText(ByRef lbl As Label) Implements IReport.FormatText\n" + " End Sub\n\n" + " Public Sub FormatTreeView(ByRef trv As TreeView) Implements IReport.FormatTreeView\n" + " End Sub\n\n" + " Public ReadOnly Property DisplayXmlRootNode() As Boolean Implements IReport.DisplayXmlRootNode\n" + " Get\n" + " Return True\n" + " End Get\n" + " End Property\n\n" + " Public ReadOnly Property AttributeDisplay() As XmlAttributeDisplay Implements IReport.AttributeDisplay\n" + " Get\n" + " Return XmlAttributeDisplay.None\n" + " End Get\n" + " End Property\n\n" + " Public ReadOnly Property AttributeDelimiter() As String Implements IReport.AttributeDelimiter\n" + " Get\n" + " Return \" | \"\n" + " End Get\n" + " End Property\n\n" + "#End Region\n\n" + "#Region \"Information\"\n" + " Public ReadOnly Property ReportName() As String Implements IReport.ReportName\n" + " Get\n" + " Return \"Documents Stored\"\n" + " End Get\n" + " End Property\n\n" + " Public ReadOnly Property ReportDescription() As String Implements IReport.ReportDescription\n" + " Get\n" + " Return \"Totals for the documents stored\"\n" + " End Get\n" + " End Property\n\n" + " Public ReadOnly Property ReportExportType() As ExportType Implements IReport.ReportExportType\n" + " Get\n" + " Return ExportType.Text\n" + " End Get\n" + " End Property\n\n" + " Public ReadOnly Property HasValidParametersSelected() As Boolean Implements IReport.HasValidParametersSelected\n" + " Get\n" + " endDate = RepParams.DateRange.GetEndDate\n" + " startDate = RepParams.DateRange.GetStartDate\n" + " origin = RepParams.OriginSelector.SelectedValue\n" + " Return RepParams.ValidParameters\n" + " End Get\n" + " End Property\n\n" + " Public ReadOnly Property ReportHeader() As String Implements IReport.ReportHeader\n" + " Get\n" + " Return String.Empty\n" + " End Get\n" + " End Property\n\n" + " Public ReadOnly Property LastError() As Exception Implements IReport.LastError\n" + " Get\n" + " Return MyLastException\n" + " End Get\n" + " End Property\n\n" + " Public ReadOnly Property ReportResults() As ReportResult Implements IReport.ReportResults\n" + " Get\n" + " Return lastResult\n" + " End Get\n" + " End Property\n\n" + " Public ReadOnly Property AllowExport() As Boolean Implements IReport.AllowExport\n" + " Get\n" + " Return True\n" + " End Get\n" + " End Property\n\n" + " Public ReadOnly Property Parameters() As String Implements IReport.Parameters\n" + " Get\n" + " Dim params As Object() = New Object() _\n" + " {vbNewLine, RepParams.DateRange.GetStartDate.ToShortDateString, _\n" + " RepParams.DateRange.GetEndDate.ToShortDateString}\n" + " Return String.Format(\"StartDate: {1}{0}EndDate: {2}{0}\", params)\n" + " End Get\n" + " End Property\n\n" + " Public Overloads Function ToString() As String Implements IReport.ToString\n" + " Return ReportName\n" + " End Function\n\n" + "#End Region\n" + "End Class\n"; final Pattern pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE | Pattern.DOTALL); final Matcher matcher = pattern.matcher(string); while (matcher.find()) { System.out.println("Full match: " + matcher.group(0)); for (int i = 1; i <= matcher.groupCount(); i++) { System.out.println("Group " + i + ": " + matcher.group(i)); } } } }

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 Java, please visit: https://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html