Regular Expressions 101

Save & Share

  • Regex Version: ver. 1
  • Update Regex
    ctrl+⇧+s
  • Save new Regex
    ctrl+s
  • Add to Community Library

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

/
/
gm

Test String

Code Generator

Generated Code

import Foundation let pattern = #"\[{"@context".*?\]"# let regex = try! NSRegularExpression(pattern: pattern, options: .anchorsMatchLines) let testString = #"[{"@context":"http://schema.org","@type":"http://schema.org/Restaurant","name":"Ray's In The City","address":{"@type":"PostalAddress","addressCountry":"US","streetAddress":"240 Peachtree St NW","addressLocality":"Atlanta","addressRegion":"GA","postalCode":"30303"},"aggregateRating":{"@type":"AggregateRating","ratingValue":4,"reviewCount":23},"geo":{"@type":"GeoCoordinates","latitude":33.76062,"longitude":-84.38833},"priceRange":"$$","telephone":"(404) 524-9224","review":{"@type":"Review","itemReviewed":"Ray's In The City","author":"YP User","reviewBody":"We went to Ray's for our 3rd anniversary- the food was ridiculous. An ample amount of delicious food that will not leave you hungry- exceptional service starting from the hostess through the wait st…"},"url":"http://www.raysrestaurants.com/raysinthecity/home","image":"http://www.yellowpages.com//i1.ypcdn.com/ypu/images/default-thumbnails/thumbnail-2.svg?417c5cb"},{"@context":"http://schema.org","@type":"http://schema.org/Restaurant","name":"Agatha's A Taste Of Mystery","address":{"@type":"PostalAddress","addressCountry":"US","streetAddress":"161 Peachtree Center Ave NE","addressLocality":"Atlanta","addressRegion":"GA","postalCode":"30303"},"aggregateRating":{"@type":"AggregateRating","ratingValue":3.12,"reviewCount":26},"geo":{"@type":"GeoCoordinates","latitude":33.75876,"longitude":-84.386},"telephone":"(678) 999-7059","review":{"@type":"Review","itemReviewed":"Agatha's A Taste Of Mystery","author":"YP User","reviewBody":"We've all been there, trying to find some fun social activity that means NOT hanging out in a bar. Agatha's is the place to go! Reserve your seats and be prepared to enjoy delicious appetizers, and a…"},"url":"http://www.agathas.com","image":"https://sapi.everyscape.com/XmlApi/Image.svc/Images/VSTHUMB/VS19115870_W287_H93"},{"@context":"http://schema.org","@type":"http://schema.org/Restaurant","name":"Basil's Restaurant & Tapas Bar","address":{"@type":"PostalAddress","addressCountry":"US","streetAddress":"2985 Grandview Ave NE","addressLocality":"Atlanta","addressRegion":"GA","postalCode":"30305"},"aggregateRating":{"@type":"AggregateRating","ratingValue":4.5,"reviewCount":2},"geo":{"@type":"GeoCoordinates","latitude":33.83604,"longitude":-84.37594},"priceRange":"$$","telephone":"(404) 334-2850","review":{"@type":"Review","itemReviewed":"Basil's Restaurant & Tapas Bar","author":"YP User","reviewBody":"If you are looking for great ambience, attentive service, and divine mediterranean food at a reasonable price, Basil's can't be beat. Dining on the outdoor patio under the huge trees listening to liv…"},"url":"http://basils-restaurant.com/HOME.html","image":"http://www.yellowpages.com//i1.ypcdn.com/ypu/images/default-thumbnails/thumbnail-2.svg?417c5cb"},{"@context":"http://schema.org","@type":"http://schema.org/Restaurant","name":"Cook Daddy's","address":{"@type":"PostalAddress","addressCountry":"US","streetAddress":"4805 Briarcliff Rd NE","addressLocality":"Atlanta","addressRegion":"GA","postalCode":"30345"},"geo":{"@type":"GeoCoordinates","latitude":33.84849,"longitude":-84.25732},"telephone":"(678) 292-6569","url":"http://www.cookdaddys.com","image":"https://i4.ypcdn.com/blob/2ad1cd586dd21809b4970da2fa20287a5fce8ab0_50x50_crop.jpg?417c5cb\n"},{"@context":"http://schema.org","@type":"http://schema.org/Restaurant","name":"Bone's Restaurant","address":{"@type":"PostalAddress","addressCountry":"US","streetAddress":"3130 Piedmont Rd NE","addressLocality":"Atlanta","addressRegion":"GA","postalCode":"30305"},"aggregateRating":{"@type":"AggregateRating","ratingValue":4.37,"reviewCount":97},"geo":{"@type":"GeoCoordinates","latitude":33.84233,"longitude":-84.37123},"priceRange":"$$$$","telephone":"(404) 237-2663","review":{"@type":"Review","itemReviewed":"Bone's Restaurant","author":"erica koup","reviewBody":"Outstanding service. They deserve more than 5 stars! Superb food! Had dinner and dessert-- out of this world delicious!"},"url":"http://www.bonesrestaurant.com","image":"https://i4.ypcdn.com/blob/dd7fc86b4f74740818e41e453be85443bbe2bee0_50x50_crop.jpg?417c5cb\n"},{"@context":"http://schema.org","@type":"http://schema.org/Restaurant","name":"Fogo de Chao","address":{"@type":"PostalAddress","addressCountry":"US","streetAddress":"3101 Piedmont Rd NE","addressLocality":"Atlanta","addressRegion":"GA","postalCode":"30305"},"aggregateRating":{"@type":"AggregateRating","ratingValue":4.66,"reviewCount":126},"geo":{"@type":"GeoCoordinates","latitude":33.84045,"longitude":-84.36961},"priceRange":"$","telephone":"(404) 266-9988","review":{"@type":"Review","itemReviewed":"Fogo de Chao","author":"tori bowker","reviewBody":"Where to start... Fogo de chao is a special place I love to go when I'm in the mood for meat! It's an &quot;all you can eat&quot; style restaurant with amazing meat. They also offer a salad bar that is included…"},"url":"http://www.fogo.com","image":"https://i4.ypcdn.com/blob/d7e7ae711cc9404b8bb41c6a7bd98f9bea2a2cd7_50x50_crop.jpg?417c5cb\n"},{"@context":"http://schema.org","@type":"http://schema.org/Restaurant","name":"The Vortex","address":{"@type":"PostalAddress","addressCountry":"US","streetAddress":"438 Moreland Ave NE","addressLocality":"Atlanta","addressRegion":"GA","postalCode":"30307"},"aggregateRating":{"@type":"AggregateRating","ratingValue":4.1,"reviewCount":72},"geo":{"@type":"GeoCoordinates","latitude":33.76635,"longitude":-84.34937},"priceRange":"$$","telephone":"(404) 688-1828","review":{"@type":"Review","itemReviewed":"The Vortex","author":"Stacey Gregory","reviewBody":"I have been coming here for years!! The burgers and drinks are the best!!!! The customer service never disappoints."},"url":"http://www.thevortexbarandgrill.com","image":"http://www.yellowpages.com//i1.ypcdn.com/ypu/images/default-thumbnails/thumbnail-2.svg?417c5cb"},{"@context":"http://schema.org","@type":"http://schema.org/Restaurant","name":"Atlanta Fish Market","address":{"@type":"PostalAddress","addressCountry":"US","streetAddress":"265 Pharr Rd NE","addressLocality":"Atlanta","addressRegion":"GA","postalCode":"30305"},"aggregateRating":{"@type":"AggregateRating","ratingValue":3.52,"reviewCount":84},"geo":{"@type":"GeoCoordinates","latitude":33.83623,"longitude":-84.37886},"priceRange":"$$","telephone":"(404) 262-3165","review":{"@type":"Review","itemReviewed":"Atlanta Fish Market","author":"Carol Kennedy-Sims","reviewBody":"I took my husband for lunch on his birthday, it was awesome!! The food was outstanding and the waiter was great!"},"image":"https://i4.ypcdn.com/blob/65f721379fc67967c99599869c7ffc4117dd665b_50x50_crop.jpg?417c5cb\n"},{"@context":"http://schema.org","@type":"http://schema.org/Restaurant","name":"The Capital Grille","address":{"@type":"PostalAddress","addressCountry":"US","streetAddress":"255 E Paces Ferry Rd NE","addressLocality":"Atlanta","addressRegion":"GA","postalCode":"30305"},"aggregateRating":{"@type":"AggregateRating","ratingValue":4.6,"reviewCount":52},"geo":{"@type":"GeoCoordinates","latitude":33.83892,"longitude":-84.37911},"priceRange":"$$$","telephone":"(404) 262-1162","review":{"@type":"Review","itemReviewed":"The Capital Grille","author":"YP User","reviewBody":"Anniversary Gift!\\r My husband surpirsed me and took me here. This was great the food was great and the service ws great! We loved it, we will return. \\r"},"url":"http://www.thecapitalgrille.com","image":"https://i4.ypcdn.com/blob/d2c91c63bc1e910c36f1bddda64c197b82348d70_50x50_crop.jpg?417c5cb\n"},{"@context":"http://schema.org","@type":"http://schema.org/Restaurant","name":"Einstein's","address":{"@type":"PostalAddress","addressCountry":"US","streetAddress":"1077 Juniper St NE","addressLocality":"Atlanta","addressRegion":"GA","postalCode":"30309"},"aggregateRating":{"@type":"AggregateRating","ratingValue":3.47,"reviewCount":45},"geo":{"@type":"GeoCoordinates","latitude":33.78404,"longitude":-84.38198},"priceRange":"$$","telephone":"(404) 876-7925","review":{"@type":"Review","itemReviewed":"Einstein's","author":"Nichole Wolf","reviewBody":"One of my very favorite brunch spots in Atlanta! Always quick service, a huge menu with tons of options, and a large outdoor patio. You might have to wait a bit if you come on a weekend, but it's norm…"},"url":"http://einsteinsatlanta.com","image":"http://www.yellowpages.com//i1.ypcdn.com/ypu/images/default-thumbnails/thumbnail-2.svg?417c5cb"},{"@context":"http://schema.org","@type":"http://schema.org/Restaurant","name":"Shorty's","address":{"@type":"PostalAddress","addressCountry":"US","streetAddress":"2884 N Druid Hills Rd NE Ste B","addressLocality":"Atlanta","addressRegion":"GA","postalCode":"30329"},"aggregateRating":{"@type":"AggregateRating","ratingValue":4.57,"reviewCount":23},"geo":{"@type":"GeoCoordinates","latitude":33.81713,"longitude":-84.3114},"priceRange":"$$","telephone":"(404) 315-6262","review":{"@type":"Review","itemReviewed":"Shorty's","author":"Norman Robinson","reviewBody":"Shorts is always good and very consistent with the quality. I have been voting her now for over four years. "},"url":"http://shortyspizza.com","image":"http://www.yellowpages.com//i1.ypcdn.com/ypu/images/default-thumbnails/thumbnail-2.svg?417c5cb"},{"@context":"http://schema.org","@type":"http://schema.org/Restaurant","name":"Mediterranean Grill","address":{"@type":"PostalAddress","addressCountry":"US","streetAddress":"985 Monroe Dr NE Ste F","addressLocality":"Atlanta","addressRegion":"GA","postalCode":"30308"},"aggregateRating":{"@type":"AggregateRating","ratingValue":4.5,"reviewCount":24},"geo":{"@type":"GeoCoordinates","latitude":33.78137,"longitude":-84.36835},"priceRange":"$","telephone":"(404) 917-1100","review":{"@type":"Review","itemReviewed":"Mediterranean Grill","author":"Nichole Wolf","reviewBody":"This is a totally unexpected spot located in a little strip mall across from Piedmont Park. You would never think this food is as good as it is - but it's one of my favorite Mediterranean spots in tow…"},"url":"http://www.…"# let stringRange = NSRange(location: 0, length: testString.utf16.count) let matches = regex.matches(in: testString, range: stringRange) var result: [[String]] = [] for match in matches { var groups: [String] = [] for rangeIndex in 1 ..< match.numberOfRanges { let nsRange = match.range(at: rangeIndex) guard !NSEqualRanges(nsRange, NSMakeRange(NSNotFound, 0)) else { continue } let string = (testString as NSString).substring(with: nsRange) groups.append(string) } if !groups.isEmpty { result.append(groups) } } 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