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

/
/
ig

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"([[]WebMethod.*[]])\s*public\s+static\s+void[^{]*(?<prevTry>{\s*)(?<try>.{3})" test_str = ("using Framework.Core.Impls.Dependency;\n" "using Framework.Core.Impls.Extensions;\n" "using Framework.Data.Models;\n" "using Framework.DataAccess.Interfaces;\n" "using KocSistem.LeasingCommon;\n" "using Leasing.Data.Modules;\n" "using Leasing.Service.Interfaces;\n" "using System;\n" "using System.Collections.Generic;\n" "using System.Linq;\n" "using System.Web;\n" "using System.Web.Services;\n" "using System.Web.UI;\n" "using System.Web.UI.WebControls;\n" "using Telerik.Web.UI;\n\n" "namespace LeasingWeb.Test\n" "{\n" " public partial class EF_DEMO : AppCode.BasePage\n" " {\n" " protected void Page_Load(object sender, EventArgs e)\n" " {\n" " Framework.Core.Logging.LoggerFactory.Info(\"TEST\");\n" " Framework.Core.Logging.LoggerFactory.Error(new Exception(\"TEST EXC\"));\n" " Framework.Core.Logging.LoggerFactory.Debug(\"DEBUG\");\n\n" " BindGrid();\n\n" " //this.ShowMessage(\"test\", KocSistem.LeasingCommon.MessageSeverity.Success);\n" " }\n\n" " /*public void Page_Error(object sender, EventArgs e)\n" " { \n" " HandleError();\n" " }*/\n\n" " private void BindGrid()\n" " {\n" " using (var demo = DependencyFactory.Resolve<IDemoService>())\n" " {\n" " var result = demo.GetPagedList(new Framework.Data.Models.PagedListInput\n" " {\n" " PageIndex = 1,\n" " PageSize = 3,\n" " Sorting = \"Id\",\n" " SortType = Framework.Data.Models.PagedListSortType.Descending\n" " });\n" " grdUsers.DataSource = result.Items;\n" " grdUsers.DataBind();\n" " }\n" " }\n\n" " [WebMethod(true)]\n" " public static string GetGridData(KsGridPagingParams ksGridPagingParams, \n\n" " string a_customParams123)\n" " {\n" " /*using (var da = DependencyFactory.Resolve<IDataBaseFactory>().GetDataBase())\n" " {\n" " var input = new PagedListInput();\n" " input.TableName = \"DEMO_MASTER\"; // Tablo adı\n" " input.SelectColumns = \"*\"; \n" " input.PageIndex = ksGridPagingParams.PageIndex;\n" " input.PageSize = ksGridPagingParams.PageSize;\n\n" " input.Filter = ksGridPagingParams.Filters.ToFilterSqlString(ksGridPagingParams.ModelFields);\n" " input.Sorting = ksGridPagingParams.Sorts.ToSortString();\n\n" " var result = da.GetPagedList(input);\n" " return result.ToJsonString();\n" " }*/\n\n" " using (var demo = DependencyFactory.Resolve<IDemoService>())\n" " {\n" " return demo.GetPagedList(ksGridPagingParams).ToJsonString();\n" " //return demo.GetAll().ToJsonString();\n" " }\n" " }\n\n" " [WebMethod(true)]\n" " public static string SaveGridRow(DemoMasterData model, string customParams)\n" " {\n" " using (var demo = DependencyFactory.Resolve<IDemoService>())\n" " {\n" " return demo.Save(model).ToJsonString();\n" " }\n" " }\n" " [WebMethod(true)]\n" " public static void DeleteGridRow(DemoMasterData model, string customParams)\n" " {\n" " using (var demo = DependencyFactory.Resolve<IDemoService>())\n" " {\n" " demo.Delete(model);\n" " }\n" " }\n\n" " [WebMethod(true)]\n" " public static string GetDetailGridData(KsGridPagingParams ksGridPagingParams)\n" " {\n" " using (var demo = DependencyFactory.Resolve<IDemoDetailService>())\n" " {\n" " var parentId = ksGridPagingParams.ParentId.ToInt();\n" " //return demo.GetPagedList(ksGridPagingParams, d => d.MasterId == parentId).ToJsonString(); ;\n" " return demo.GetPagedList(ksGridPagingParams).ToJsonString(); ;\n" " }\n" " }\n" " [WebMethod(true)]\n" " public static string SaveDetailGridRow(DemoDetailData model)\n" " {\n" " using (var demo = DependencyFactory.Resolve<IDemoDetailService>())\n" " {\n" " return demo.Save(model).ToJsonString();\n" " }\n" " }\n" " [WebMethod(true)]\n" " public static void DeleteDetailGridRow(DemoDetailData model)\n" " {\n" " using (var demo = DependencyFactory.Resolve<IDemoDetailService>())\n" " {\n" " demo.Delete(model);\n" " }\n" " }\n\n\n" " struct City\n" " {\n" " public int CityId { get; set; }\n" " public string Name { get; set; }\n" " }\n" " struct District\n" " {\n" " public int DistrictId { get; set; }\n" " public string Name { get; set; }\n" " public int CityId { get; set; }\n" " }\n" " struct Address\n" " {\n" " public int AddressId { get; set; }\n" " public string Name { get; set; }\n" " }\n" " struct Phone\n" " {\n" " public int PhoneId { get; set; }\n" " public int DistrictId { get; set; }\n" " public int AddressId { get; set; }\n" " public string Number { get; set; }\n" " }\n\n" " [WebMethod(true)]\n" " public static object GetCities(KsFilterParams filter)\n" " {\n" " var cities = new List<City>();\n\n" " if (cities is null)\n" " {\n" " cities = new List<City>();\n" " }\n\n" " cities.Add(new City { CityId = 1, Name = \"İstanbul\" });\n" " cities.Add(new City { CityId = 2, Name = \"Ankara\" });\n" " cities.Add(new City { CityId = 3, Name = \"İzmir\" });\n" " cities.Add(new City { CityId = 4, Name = \"Bilecik\" });\n\n\n" " for (int i = 5; i <= 10; i++)\n" " {\n" " cities.Add(new City { CityId = i, Name = string.Format(\"Test Şehir({0})\", i) });\n" " }\n\n" " return cities.Filter(filter);\n" " }\n\n" " [WebMethod(true)]\n" " public static object GetDistricts(KsFilterParams filter)\n" " {\n" " var districts = new List<District>();\n\n" " districts.Add(new District { CityId = 1, DistrictId = 1, Name = \"Esenler\" });\n" " districts.Add(new District { CityId = 1, DistrictId = 2, Name = \"Güngören\" });\n" " districts.Add(new District { CityId = 1, DistrictId = 3, Name = \"Bağcılar\" });\n\n" " districts.Add(new District { CityId = 2, DistrictId = 4, Name = \"Çankaya\" });\n" " districts.Add(new District { CityId = 2, DistrictId = 5, Name = \"Keçiören\" });\n\n" " districts.Add(new District { CityId = 3, DistrictId = 6, Name = \"Karşıyaka\" });\n" " districts.Add(new District { CityId = 3, DistrictId = 7, Name = \"Göztepe\" });\n\n\n" " for (int i = 8; i <= 20; i++)\n" " {\n" " districts.Add(new District { DistrictId = i, CityId = (i % 990) + 1, Name = string.Format(\"Test İlçe({0})\", i) });\n" " }\n\n\n" " return districts.Filter(filter);\n" " }\n\n" " [WebMethod(true)]\n" " public static object GetAddresses(KsFilterParams filter)\n" " {\n" " var addresses = new List<Address>();\n\n" " addresses.Add(new Address { AddressId = 1, Name = \"Adres1\" });\n" " addresses.Add(new Address { AddressId = 2, Name = \"Adres2\" });\n" " addresses.Add(new Address { AddressId = 3, Name = \"Adres3\" });\n" " addresses.Add(new Address { AddressId = 4, Name = \"Adres4\" });\n" " addresses.Add(new Address { AddressId = 5, Name = \"Adres5\" });\n" " addresses.Add(new Address { AddressId = 6, Name = \"Adres6\" });\n" " addresses.Add(new Address { AddressId = 7, Name = \"Adres7\" });\n\n" " return addresses.Filter(filter);\n" " }\n" " [WebMethod(true)]\n" " public static object GetPhones(KsFilterParams filter)\n" " {\n" " var phones = new List<Phone>();\n\n" " phones.Add(new Phone { AddressId = 1, DistrictId = 1, PhoneId = 1, Number = \"11\" });\n" " phones.Add(new Phone { AddressId = 1, DistrictId = 1, PhoneId = 2, Number = \"22\" });\n" " phones.Add(new Phone { AddressId = 2, DistrictId = 1, PhoneId = 3, Number = \"33\" });\n" " phones.Add(new Phone { AddressId = 3, DistrictId = 2, PhoneId = 4, Number = \"44\" });\n" " phones.Add(new Phone { AddressId = 2, DistrictId = 1, PhoneId = 5, Number = \"55\" });\n" " phones.Add(new Phone { AddressId = 4, DistrictId = 3, PhoneId = 6, Number = \"66\" });\n" " phones.Add(new Phone { AddressId = 6, DistrictId = 7, PhoneId = 7, Number = \"77\" });\n\n\n" " return phones.Filter(filter);\n" " }\n\n" " struct ComboData\n" " {\n" " public int Id { get; set; }\n" " public string Text { get; set; }\n" " }\n\n" " [WebMethod(true)]\n" " public static string GetComboData()\n" " {\n" " var data = new List<ComboData> {\n" " new ComboData { Id = 1, Text =\"Genel\" },\n" " new ComboData { Id = 2, Text =\"Özel\" },\n" " new ComboData { Id = 3, Text =\"Diğer\" }\n" " };\n\n" " return data.ToJsonString();\n" " }\n\n" " protected void btnNew_Click(object sender, EventArgs e)\n" " {\n" " try\n" " {\n" " Response.Redirect(\"EF_DEMO_DETAIL.aspx\");\n" " }\n" " catch (Exception ex) { this.PublishException(ex); }\n" " }\n\n" " protected void grdUsers_ItemCommand(object source, GridCommandEventArgs e)\n" " {\n" " var dataItem = e.Item.DataItem as DemoMasterData;\n\n" " //if (e.CommandName == \"Detail\")\n" " //{\n" " // Response.Redirect(string.Format(\"EF_DEMO_DETAIL.aspx?ID={0}\", dataItem.Id));\n" " //}\n" " if (e.CommandName == \"Delete\")\n" " {\n" " using (var demo = DependencyFactory.Resolve<IDemoService>())\n" " {\n" " demo.Delete(dataItem.Id);\n" " }\n" " Response.Redirect(\"EF_DEMO.aspx\");\n" " }\n" " }\n\n" " protected void btnSaveAll_Click(object sender, EventArgs e)\n" " {\n" " try\n" " {\n" " //this.ShowMessage(\"errTest\", KocSistem.LeasingCommon.MessageSeverity.Error);\n" " string x = null;\n" " var length = x.Length;\n\n" " using (var demo = DependencyFactory.Resolve<IDemoService>())\n" " {\n" " var identityNumber = demo.GetAll().Max(u => u.Identity_Number);\n\n" " var users = new List<DemoMasterData>();\n" " users.Add(new DemoMasterData { Identity_Number = ++identityNumber, NameSurname = \"1\" });\n" " users.Add(new DemoMasterData { Identity_Number = ++identityNumber, NameSurname = \"2\" });\n" " users.Add(new DemoMasterData { Identity_Number = ++identityNumber, NameSurname = \"3\" });\n" " users.Add(new DemoMasterData { Identity_Number = ++identityNumber, NameSurname = \"4\" });\n" " demo.SaveAll(users);\n" " }\n" " }\n" " catch (Exception ex)\n" " {\n" " if (chkHandleExc.Checked)\n" " {\n" " this.PublishException(ex);\n" " return;\n" " }\n\n" " throw; \n" " }\n" " }\n\n" " protected void btnWordToPdf_Click(object sender, EventArgs e)\n" " {\n" " try\n" " {\n" " var application = new Microsoft.Office.Interop.Word.Application();\n" " var document = application.Documents.Open(txtWordPath.Text);\n" " document.Activate();\n" " var pdfName = \"TestPDF_\" + DateTime.Now.ToString(\"yyyyMMddHHmmss\") + \".pdf\";\n" " document.SaveAs2(Server.MapPath(ReportingUtility.ExcelTempPath + pdfName), Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatPDF);\n" " document.Close();\n\n" " OpenPopup(Request.GetApplicationPath() + \"/Reporting/Temporary/\" + pdfName);\n" " }\n" " catch (Exception ex)\n" " {\n" " this.PublishException(ex);\n" " }\n" " }\n" " }\n" "}") matches = re.finditer(regex, test_str, re.IGNORECASE) 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