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

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