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

/
/
gis

Test String

Code Generator

Generated Code

const regex = /Public(?: ReadOnly)? Property (?<prop>.*?)\(\) As [a-zA-Z]*(?: Implements )?.*? End Property/gis; // Alternative syntax using RegExp constructor // const regex = new RegExp('Public(?: ReadOnly)? Property (?<prop>.*?)\\(\\) As [a-zA-Z]*(?: Implements )?.*? End Property', 'gis') const str = `Imports System.Windows.Forms Imports System.Drawing Imports CapReportLib Imports ReportControls.Database Imports System.Threading Imports System.ComponentModel Public Class Report : Implements IReport #Region "Fields" Friend MyReportRunTime As TimeSpan Friend MyLastException As New Exception("No Errors") Friend RepParams As New ReportParameters() Friend Shared DBA As ReportControls.Database.DataAccess Private lastResult As New ReportResult(False, New TimeSpan, String.Empty) Private Shared delRunReport As IReport.Execute = Nothing Private repSync As ISynchronizeInvoke Private repThread As Thread Private repHandler As IReport.ReportHandler Private repParamsSync As ISynchronizeInvoke Private threadIsRunning As Boolean = False Private Delegate Sub EnableCsvHandler(ByVal enable As Boolean, ByVal value As String) Private enableCsv As EnableCsvHandler Private csvData As String = String.Empty Private startDate As Date = Now Private endDate As Date = Now Private origin As ReportControls.Types.OriginItem #End Region #Region "Threading" Private Sub RunReportThread() threadIsRunning = True Dim rs = ExecuteReport() If repThread.ThreadState <> ThreadState.AbortRequested Then repSync.Invoke(repHandler, New Object() {rs}) repParamsSync.Invoke(enableCsv, New Object() {True, csvData}) End If threadIsRunning = False End Sub Public Sub ExecuteReportThread() Implements IReport.ExecuteReportThread repParamsSync = RepParams enableCsv = AddressOf RepParams.EnableCsvExport repThread = New Thread(AddressOf RunReportThread) repThread.Start() End Sub Public Sub SetReportHandler(ByVal rh As IReport.ReportHandler) Implements IReport.SetReportHandler repHandler = rh End Sub Public Sub StopReport() Implements IReport.StopReport If repThread Is Nothing Then Return If repThread.IsAlive Then repThread.Abort() End If End Sub Public Sub SetSyncInvoke(ByVal syn As ISynchronizeInvoke) Implements IReport.SetSyncInvoke repSync = syn End Sub Public Sub SetReportHandler(ByVal rh As IReport.ReportHandler, ByVal syn As ISynchronizeInvoke) _ Implements IReport.SetReportHandler repSync = syn repHandler = rh End Sub #End Region #Region "Initialize" Public Sub SetOutPut(ByVal pnlControls As Panel) Implements IReport.SetOutPut RepParams.Parent = pnlControls RepParams.Dock = DockStyle.Fill RepParams.Show() End Sub Public Sub SetReportDataAccess(ByVal connectionString As String) Implements CapReportLib.IReport.SetReportConnectionString SetDataAccess(New ReportControls.Database.DataAccess(connectionString)) End Sub Public Sub SetDataAccess(ByVal db As DataAccess) DBA = db SetOrigins() End Sub Private Sub SetOrigins() RepParams.OriginSelector.SetConnectionString(DBA.ConnectionString) End Sub Public Sub SetReportExecute(ByVal exc As IReport.Execute) Implements IReport.SetReportExecute delRunReport = exc End Sub Public Sub Dispose() Implements IReport.Dispose DBA.Dispose() RepParams.Dispose() End Sub #End Region #Region "Run Report" Public Function ExecuteReport() As ReportResult Implements IReport.ExecuteReport RepParams.CsvButton1.EnableCsvExport(False, String.Empty) Dim success As Boolean = True Dim startRun = Now Dim repData As String = String.Empty Try If HasValidParametersSelected Then Dim rep As New DocsStoredReport(startDate, endDate) repData = rep.Execute(origin, RepParams.ChkRunOriginal.Checked) csvData = rep.CsvOutput If Not threadIsRunning Then RepParams.CsvButton1.EnableCsvExport(True, rep.CsvOutput) End If End If Catch ex As Exception success = False MyLastException = ex End Try Dim endRun = Now lastResult = New ReportResult(success, endRun - startRun, repData) Return lastResult End Function Friend Shared Sub RunReport() If delRunReport Is Nothing Then Return delRunReport.Invoke() End Sub #End Region #Region "Format Output" Public Sub FormatText(ByRef disp As RichTextBox) Implements IReport.FormatText Dim iFont As New Font("Courier New", 12, FontStyle.Regular, GraphicsUnit.Pixel) disp.SelectAll() disp.SelectionFont = iFont 'disp.SelectionAlignment = HorizontalAlignment.Center If disp.Find(DocsStoredReport.Header) = -1 Then Return disp.SelectionStart = disp.Find(DocsStoredReport.Header) disp.SelectionLength = DocsStoredReport.Header.Length iFont = New Font("Courier New", 18, FontStyle.Bold, GraphicsUnit.Pixel) disp.SelectionFont = iFont 'disp.SelectionAlignment = HorizontalAlignment.Center disp.SelectionStart = disp.Find(DocsStoredReport.DateRange) disp.SelectionLength = DocsStoredReport.DateRange.Length iFont = New Font("Courier New", 18, FontStyle.Bold, GraphicsUnit.Pixel) disp.SelectionFont = iFont 'disp.SelectionAlignment = HorizontalAlignment.Center disp.SelectionStart = disp.Find(DocsStoredReport.SubHeader) disp.SelectionLength = DocsStoredReport.SubHeader.Length iFont = New Font("Courier New", 12, FontStyle.Bold Xor FontStyle.Underline, GraphicsUnit.Pixel) disp.SelectionFont = iFont 'disp.SelectionAlignment = HorizontalAlignment.Center disp.SelectionStart = disp.Find("Export Files") disp.SelectionLength = "Export Files".Length iFont = New Font("courier new", 12, FontStyle.Bold, GraphicsUnit.Pixel) disp.SelectionFont = iFont disp.SelectionStart = disp.Find(ExportFile.HeaderRow(False)) disp.SelectionLength = ExportFile.HeaderRow(False).Length iFont = New Font("Courier New", 12, FontStyle.Bold Xor FontStyle.Underline, GraphicsUnit.Pixel) disp.SelectionFont = iFont End Sub Public Sub FormatText(ByRef lbl As Label) Implements IReport.FormatText End Sub Public Sub FormatTreeView(ByRef trv As TreeView) Implements IReport.FormatTreeView End Sub Public ReadOnly Property DisplayXmlRootNode() As Boolean Implements IReport.DisplayXmlRootNode Get Return True End Get End Property Public ReadOnly Property AttributeDisplay() As XmlAttributeDisplay Implements IReport.AttributeDisplay Get Return XmlAttributeDisplay.None End Get End Property Public ReadOnly Property AttributeDelimiter() As String Implements IReport.AttributeDelimiter Get Return " | " End Get End Property #End Region #Region "Information" Public ReadOnly Property ReportName() As String Implements IReport.ReportName Get Return "Documents Stored" End Get End Property Public ReadOnly Property ReportDescription() As String Implements IReport.ReportDescription Get Return "Totals for the documents stored" End Get End Property Public ReadOnly Property ReportExportType() As ExportType Implements IReport.ReportExportType Get Return ExportType.Text End Get End Property Public ReadOnly Property HasValidParametersSelected() As Boolean Implements IReport.HasValidParametersSelected Get endDate = RepParams.DateRange.GetEndDate startDate = RepParams.DateRange.GetStartDate origin = RepParams.OriginSelector.SelectedValue Return RepParams.ValidParameters End Get End Property Public ReadOnly Property ReportHeader() As String Implements IReport.ReportHeader Get Return String.Empty End Get End Property Public Property LastError() As Exception Implements IReport.LastError Get Return MyLastException End Get End Property Public ReadOnly Property ReportResults() As ReportResult Implements IReport.ReportResults Get Return lastResult End Get End Property Public ReadOnly Property AllowExport() As Boolean Implements IReport.AllowExport Get Return True End Get End Property Public ReadOnly Property Parameters() As String Implements IReport.Parameters Get Dim params As Object() = New Object() _ {vbNewLine, RepParams.DateRange.GetStartDate.ToShortDateString, _ RepParams.DateRange.GetEndDate.ToShortDateString} Return String.Format("StartDate: {1}{0}EndDate: {2}{0}", params) End Get End Property Public Overloads Function ToString() As String Implements IReport.ToString Return ReportName End Function #End Region End Class `; // Reset `lastIndex` if this regex is defined globally // regex.lastIndex = 0; let m; while ((m = regex.exec(str)) !== null) { // This is necessary to avoid infinite loops with zero-width matches if (m.index === regex.lastIndex) { regex.lastIndex++; } // The result can be accessed through the `m`-variable. m.forEach((match, groupIndex) => { console.log(`Found match, group ${groupIndex}: ${match}`); }); }

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 JavaScript, please visit: https://developer.mozilla.org/en/docs/Web/JavaScript/Guide/Regular_Expressions