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

/
/

Test String

Code Generator

Generated Code

#include <StringConstants.au3> ; to declare the Constants of StringRegExp #include <Array.au3> ; UDF needed for _ArrayDisplay and _ArrayConcatenate Local $sRegex = "^" Local $sString = "class FinUrad" & @CRLF & _ "{" & @CRLF & _ " public string Kod { get; set; }" & @CRLF & _ " public string Nazev { get; set; }" & @CRLF & _ " public override string ToString()" & @CRLF & _ " {" & @CRLF & _ " return Nazev;" & @CRLF & _ " }" & @CRLF & _ "}" & @CRLF & _ "" & @CRLF & _ "<Grid Margin="20">" & @CRLF & _ " <Grid.RowDefinitions>" & @CRLF & _ " <RowDefinition Height="Auto"/>" & @CRLF & _ " <RowDefinition Height="*"/>" & @CRLF & _ " <RowDefinition Height="Auto"/>" & @CRLF & _ " </Grid.RowDefinitions>" & @CRLF & _ " <StackPanel Grid.Row="0" Margin="0,0,0,10">" & @CRLF & _ " <TextBlock Text="Jméno:"/>" & @CRLF & _ " <TextBox x:Name="txtJmeno" Margin="0,5,0,10"/>" & @CRLF & _ "" & @CRLF & _ " <TextBlock Text="Rodné číslo (XXXXXX/XXXX):"/>" & @CRLF & _ " <TextBox x:Name="txtRC" Margin="0,5,0,10"/>" & @CRLF & _ "" & @CRLF & _ " <TextBlock Text="Finanční úřad:"/>" & @CRLF & _ " <ComboBox x:Name="comboFinUrady" DisplayMemberPath="Nazev" Margin="0,5,0,10"/>" & @CRLF & _ "" & @CRLF & _ " <TextBlock Text="Zpráva:"/>" & @CRLF & _ " <TextBox x:Name="txtZprava" AcceptsReturn="True" Height="80" Margin="0,5,0,10"/>" & @CRLF & _ " </StackPanel>" & @CRLF & _ " <StackPanel Grid.Row="2" Orientation="Horizontal" HorizontalAlignment="Right">" & @CRLF & _ " <Button x:Name="btnZobrazitHistorii" Content="Zobrazit historii" Click="btnZobrazitHistorii_Click"/>" & @CRLF & _ " " & @CRLF & _ " <Button x:Name="btnOdeslat" Content="Odeslat" Click="btnOdeslat_Click"/>" & @CRLF & _ " </StackPanel>" & @CRLF & _ "</Grid>" & @CRLF & _ "" & @CRLF & _ "public partial class MainWindow : Window" & @CRLF & _ "{" & @CRLF & _ " private ObservableCollection<FinUrad> urady = new();" & @CRLF & _ " private const string ApiUrl = "https://csharp.janjanousek.cz/api/financni-urady/";" & @CRLF & _ " private const string ApiKey = "VSB";" & @CRLF & _ " public MainWindow()" & @CRLF & _ " {" & @CRLF & _ " InitializeComponent();" & @CRLF & _ " LoadFinUrady();" & @CRLF & _ " }" & @CRLF & _ " private async void LoadFinUrady()" & @CRLF & _ " {" & @CRLF & _ " while (true)" & @CRLF & _ " {" & @CRLF & _ " try" & @CRLF & _ " {" & @CRLF & _ " using HttpClient client = new();" & @CRLF & _ " client.DefaultRequestHeaders.Add("x-api-key", ApiKey);" & @CRLF & _ " var response = await client.GetAsync(ApiUrl);" & @CRLF & _ " if (response.StatusCode == System.Net.HttpStatusCode.TooManyRequests)" & @CRLF & _ " {" & @CRLF & _ " if (response.Headers.TryGetValues("Retry-After", out var values))" & @CRLF & _ " {" & @CRLF & _ " int secods = int.Parse(values.First());" & @CRLF & _ " await Task.Delay(secods * 1000);" & @CRLF & _ " continue;" & @CRLF & _ " }" & @CRLF & _ " }" & @CRLF & _ " response.EnsureSuccessStatusCode();" & @CRLF & _ " var xmlString = await response.Content.ReadAsStringAsync();" & @CRLF & _ " " & @CRLF & _ " var xml = XDocument.Parse(xmlString);" & @CRLF & _ " var polozky = xml.Descendants("POLOZKA");" & @CRLF & _ " foreach (var elem in polozky)" & @CRLF & _ " {" & @CRLF & _ " string id = elem.Element("CHODNOTA")?.Value;" & @CRLF & _ " string name = elem.Element("ZKRTEXT")?.Value;" & @CRLF & _ " if (!string.IsNullOrEmpty(id) && !string.IsNullOrEmpty(name))" & @CRLF & _ " {" & @CRLF & _ " urady.Add(new FinUrad { Kod = id, Nazev = name });" & @CRLF & _ " }" & @CRLF & _ " }" & @CRLF & _ " comboFinUrady.ItemsSource = urady;" & @CRLF & _ " comboFinUrady.SelectedIndex = 0;" & @CRLF & _ " break;" & @CRLF & _ " }" & @CRLF & _ " catch (Exception ex)" & @CRLF & _ " {" & @CRLF & _ " MessageBox.Show("Chyba pri nacitani financnich uradu: " + ex.Message);" & @CRLF & _ " break;" & @CRLF & _ " }" & @CRLF & _ " }" & @CRLF & _ " }" & @CRLF & _ " private void btnOdeslat_Click(object sender, RoutedEventArgs e)" & @CRLF & _ " {" & @CRLF & _ " string jmeno = txtJmeno.Text;" & @CRLF & _ " string rc = txtRC.Text;" & @CRLF & _ " var urad = comboFinUrady.SelectedItem as FinUrad;" & @CRLF & _ " string zprava = txtZprava.Text;" & @CRLF & _ " if(string.IsNullOrEmpty(jmeno) || string.IsNullOrEmpty(rc) || urad == null || string.IsNullOrEmpty(zprava) || zprava.Length < 10)" & @CRLF & _ " {" & @CRLF & _ " MessageBox.Show("Všechna pole musí být vyplněna a zpráva musí mít alespoň 10 znaků.");" & @CRLF & _ " return;" & @CRLF & _ " }" & @CRLF & _ " if(!System.Text.RegularExpressions.Regex.IsMatch(rc, @"^\d{6}/\d{4}$"))" & @CRLF & _ " {" & @CRLF & _ " MessageBox.Show("Rodné číslo musí mít formát 123456/1234.");" & @CRLF & _ " return;" & @CRLF & _ " }" & @CRLF & _ " SaveToDatabase(jmeno, rc, urad.Kod, urad.Nazev, zprava);" & @CRLF & _ " new ResultsWindow().Show();" & @CRLF & _ " }" & @CRLF & _ " private void btnZobrazitHistorii_Click(object sender, RoutedEventArgs e)" & @CRLF & _ " {" & @CRLF & _ " new ResultsWindow().Show();" & @CRLF & _ " }" & @CRLF & _ " private void SaveToDatabase(string jmeno, string rc, string idUrad, string nazevUrad, string zprava)" & @CRLF & _ " {" & @CRLF & _ " string dbPath = "formulare.db";" & @CRLF & _ " if(!File.Exists(dbPath))" & @CRLF & _ " {" & @CRLF & _ " using var connection = new SqliteConnection($"Data Source={dbPath}");" & @CRLF & _ " connection.Open();" & @CRLF & _ " var cmd = connection.CreateCommand();" & @CRLF & _ " cmd.CommandText = @"" & @CRLF & _ " CREATE TABLE IF NOT EXISTS Formulare (" & @CRLF & _ " Id INTEGER PRIMARY KEY AUTOINCREMENT," & @CRLF & _ " Jmeno TEXT NOT NULL," & @CRLF & _ " RodneCislo TEXT NOT NULL," & @CRLF & _ " IdUrad TEXT NOT NULL," & @CRLF & _ " NazevUrad TEXT NOT NULL," & @CRLF & _ " Zprava TEXT NOT NULL" & @CRLF & _ " )";" & @CRLF & _ " cmd.ExecuteNonQuery();" & @CRLF & _ " }" & @CRLF & _ " using var conn = new SqliteConnection($"Data Source={dbPath}");" & @CRLF & _ " conn.Open();" & @CRLF & _ " var insert = conn.CreateCommand();" & @CRLF & _ " insert.CommandText = @"" & @CRLF & _ " INSERT INTO Formulare (Jmeno, RodneCislo, IdUrad, NazevUrad, Zprava)" & @CRLF & _ " VALUES ($jmeno, $rc, $idUrad, $nazevUrad, $zprava)";" & @CRLF & _ " insert.Parameters.AddWithValue("$jmeno", jmeno);" & @CRLF & _ " insert.Parameters.AddWithValue("$rc", rc);" & @CRLF & _ " insert.Parameters.AddWithValue("$idUrad", idUrad);" & @CRLF & _ " insert.Parameters.AddWithValue("$nazevUrad", nazevUrad);" & @CRLF & _ " insert.Parameters.AddWithValue("$zprava", zprava);" & @CRLF & _ " insert.ExecuteNonQuery();" & @CRLF & _ " }" & @CRLF & _ "}" & @CRLF & _ "" & @CRLF & _ "<Grid>" & @CRLF & _ " <ListBox x:Name="lstHistorie"/>" & @CRLF & _ "</Grid>" & @CRLF & _ "" & @CRLF & _ "public partial class ResultsWindow : Window" & @CRLF & _ "{" & @CRLF & _ " public ResultsWindow()" & @CRLF & _ " {" & @CRLF & _ " InitializeComponent();" & @CRLF & _ " LoadData();" & @CRLF & _ " }" & @CRLF & _ " private void LoadData()" & @CRLF & _ " {" & @CRLF & _ " string dbPath = "formulare.db";" & @CRLF & _ " if (!File.Exists(dbPath)) return;" & @CRLF & _ " using var conn = new SqliteConnection($"Data Source={dbPath}");" & @CRLF & _ " conn.Open();" & @CRLF & _ " var cmd = conn.CreateCommand();" & @CRLF & _ " cmd.CommandText = @"" & @CRLF & _ " SELECT Jmeno, RodneCislo, IdUrad, NazevUrad, Zprava" & @CRLF & _ " FROM Formulare";" & @CRLF & _ " using var reader = cmd.ExecuteReader();" & @CRLF & _ " while (reader.Read())" & @CRLF & _ " {" & @CRLF & _ " lstHistorie.Items.Add($"Jméno: {reader.GetString(0)}, RČ: {reader.GetString(1)}, Úřad: {reader.GetString(2)}, Zpráva: {reader.GetString(3)}");" & @CRLF & _ " }" & @CRLF & _ " }" & @CRLF & _ "}" Local $aArray = StringRegExp($sString, $sRegex, $STR_REGEXPARRAYFULLMATCH) ; Present the entire match result _ArrayDisplay($aArray, "Result")

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 AutoIt, please visit: https://www.autoitscript.com/autoit3/docs/functions/StringRegExp.htm