Regular Expressions 101

Save & Share

Flavor

  • PCRE2 (PHP)
  • ECMAScript (JavaScript)
  • Python
  • Golang
  • Java
  • .NET 7.0 (C#)
  • Rust
  • PCRE (Legacy)
  • Regex Flavor Guide

Function

  • Match
  • Substitution
  • List
  • Unit Tests
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
Processing...

Test String

Substitution
Processing...

Code Generator

Generated Code

import Foundation let pattern = #"dn: \t"CN=(.*?),OU=(.*?),DC=(PACRIM1|global|honeywell|namerica1|namerica2|namerica3|namerica4),.*\t\tdescription:\t\t(.*)\tdisplayName: \t\t(.*)\t\thwObjectGUID: \t\t(.*)\t\textensionAttribute7: \t\t(.*)\t\tmail: \t\t(.*)\t\tobjectClass: \t\t(.*)"# let regex = try! NSRegularExpression(pattern: pattern) let testString = ##""" dn: "CN=DL - Wipro CIM Team,OU=Distribution Lists,OU=Exchange,OU=US,DC=global,DC=ds,DC=honeywell,DC=com" hwDomain: description: !e329288 - INC000009225560 displayName: DL - Wipro CIM Team hwObjectGUID: b347d344-5d04-4d40-b053-61662ff20d45 extensionAttribute7: E130111 mail: DL-WiproCIMTeam@Honeywell.com objectClass: group dn: "CN=P3-Sales-Mktng-Europe (BI35),OU=New Objects,OU=HMX-BI35,OU=Honeywell,OU=From Aelita,OU=Exchange,DC=global,DC=ds,DC=honeywell,DC=com" hwDomain: description: displayName: P3-Sales-Mktng-Europe (BI35) hwObjectGUID: 3f042f02-cf8a-4357-8e82-4a9c110d8834 extensionAttribute7: E155937 mail: HMX-SalesMktng-Europe@Honeywell.com objectClass: group dn: "CN=ISLC Focal Point - Philippines (AS),OU=Distribution Lists,OU=Exchange,OU=APAC,DC=global,DC=ds,DC=honeywell,DC=com" hwDomain: description: displayName: ISLC Focal Point - Philippines (AS) hwObjectGUID: d26b7d04-3b2a-411e-9461-05f24459d6b4 extensionAttribute7: mail: ISLCFocalPoint-PhilippinesAS@Honeywell.com objectClass: group dn: "CN=DL_PEHA_Engg,OU=IN,DC=PACRIM1,DC=ds,DC=honeywell,DC=com" hwDomain: description: !H125516 #INC000008689478 displayName: DL_HTS_BCS_EMEA_LC_PEHA_Engg hwObjectGUID: 9dfe2963-3309-4285-8eb0-f1f23f08dce1 extensionAttribute7: H125516 mail: DL_HTS_BCS_EMEA_LC_PEHA_Engg@honeywell.com objectClass: group dn: "CN=DL_ECBI_Engg,OU=IN,DC=PACRIM1,DC=ds,DC=honeywell,DC=com" hwDomain: description: !H125516 #INC000008689478 displayName: DL_ECBI_Engg hwObjectGUID: 1ed1f115-427a-4ed7-ad59-992e71a9c011 extensionAttribute7: H125516 mail: DL_ECBI_Engg@honeywell.com objectClass: group dn: "CN=DL-GCCtransitionproject,OU=IN,DC=PACRIM1,DC=ds,DC=honeywell,DC=com" hwDomain: description: displayName: DL-GCCtransitionproject hwObjectGUID: 20d9e78b-bead-4a66-add6-504e43084b54 extensionAttribute7: !e596177 mail: DL-GCCtransitionproject@Honeywell.com objectClass: group dn: "CN=DL _Non Spares&ERB,OU=IN,DC=PACRIM1,DC=ds,DC=honeywell,DC=com" hwDomain: description: displayName: DL _Non Spares&ERB hwObjectGUID: fe7bc87d-eaa7-43fe-85cd-9e0df6e42bb5 extensionAttribute7: E553700 mail: DL_NonSparesERB@Honeywell.com objectClass: group dn: "CN=DL-India Staffing Leaders,OU=IN,DC=PACRIM1,DC=ds,DC=honeywell,DC=com" hwDomain: description: E855373-#INC000008575463 displayName: DL-India Staffing Leaders hwObjectGUID: 2cf5a77e-b6e8-49f9-9e1f-149a96f304a7 extensionAttribute7: !E580316 mail: DL-India-Staffing-Leaders@Honeywell.com objectClass: group dn: "CN=DL-HLS 3S SUITE-QA Team,OU=IN,DC=PACRIM1,DC=ds,DC=honeywell,DC=com" hwDomain: description: displayName: DL-HLS 3S SUITE-QA Team hwObjectGUID: 9293982e-de2a-47ec-84fe-95d45ed5d8ef extensionAttribute7: mail: DL-HLS3SSUITE-QATeam@Honeywell.com objectClass: group dn: "CN=DL-HPS_EFS_HTS_HYD,OU=IN,DC=PACRIM1,DC=ds,DC=honeywell,DC=com" hwDomain: description: displayName: DL-HPS_EFS_HTS_HYD hwObjectGUID: dfccf1e2-1124-443a-8a0c-b1caf67ea4af extensionAttribute7: mail: RS90329@Honeywell.com objectClass: group dn: "CN=DL CA&ESSProblemcoordinators,OU=IN,DC=PACRIM1,DC=ds,DC=honeywell,DC=com" hwDomain: description: displayName: DL CA&ESSProblemcoordinators hwObjectGUID: d8ad06bf-0d40-483b-80f4-3073fd181f66 extensionAttribute7: E283778 mail: DLCA&ESSProblemcoordinators@Honeywell.com objectClass: group dn: "CN=DL-EOPS ALL PM,OU=IN,DC=PACRIM1,DC=ds,DC=honeywell,DC=com" hwDomain: description: displayName: DL-EOPS ALL PM hwObjectGUID: 21c834ca-ba76-4b84-a4d4-0d1e99223412 extensionAttribute7: E159431 mail: DL-EOPSALLPM@Honeywell.com objectClass: group dn: "CN=DL-Fire China Engineering,OU=CN,DC=PACRIM1,DC=ds,DC=honeywell,DC=com" hwDomain: description: !E506546 - INC000009116231 displayName: DL-Fire China Engineering hwObjectGUID: 0bd5c412-9fb5-4acb-a060-b40006957674 extensionAttribute7: E227848 mail: DL-FireChinaEngineering@Honeywell.com objectClass: group """## let stringRange = NSRange(location: 0, length: testString.utf16.count) let substitutionString = #"dn:cn=$1,ou=$3,ou=Groups,o=honeywell\ncn: $1\nhwDomain: $3\ndescription: $4\ndisplayName: $5\nhwObjectGUID: $6\nowner: $7\nmail: $8\nobjectClass: $9\n"# let result = regex.stringByReplacingMatches(in: testString, range: stringRange, withTemplate: substitutionString) print(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 Swift 5.2, please visit: https://developer.apple.com/documentation/foundation/nsregularexpression