Regular Expressions 101

Save & Share

  • Regex Version: ver. 8
  • 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

Substitution

Processing...

Code Generator

Generated Code

import Foundation let pattern = #"^((?:.+\n .+\n)+)$"# let regex = try! NSRegularExpression(pattern: pattern, options: .anchorsMatchLines) let testString = #""" Mr. Lewes reaches this conclusion: "If, therefore, we reflect what a poem _Faust_ is, and that it contains almost every variety of style and metre, it will be tolerably evident that no one unacquainted with the original can form an adequate idea of it from translation," which is certainly correct of any translation wherein something of the rhythmical variety and beauty of the original is not retained. That very much of the rhythmical character may be retained in English, was long ago shown by Mr. Carlyle, in the passages which he translated, both literally and rhythmically, from the _Helena_ (Part Second). In fact, we have so many instances of the possibility of reciprocally transferring the finest qualities of English and German poetry, that there is no sufficient excuse for an unmetrical translation of _Faust_. I refer especially to such subtile and melodious lyrics as "The Castle by the Sea," of Uhland, and the "Silent Land" of Salis, translated by Mr. Longfellow; Goethe's "Minstrel" and "Coptic Song," by Dr. Hedge; Heine's "Two Grenadiers," by Dr. Furness and many of Heine's songs by Mr Leland; and also to the German translations of English lyrics, by Freiligrath and Strodtmann. > Life of Goethe (Book VI.). > Mr. Lewes gives the following advice: "The English reader would perhaps best succeed who should first read Dr. Anster's brilliant paraphrase, and then carefully go through Hayward's prose translation." This is singularly at variance with the view he has just expressed. Dr. Anster's version is an almost incredible dilution of the original, written in _other_ metres; while Hayward's entirely omits the element of poetry. > Foreign Review, 1828. > When Freiligrath can thus give us Walter Scott:— "Kommt, wie der Wind kommt, Wenn Wälder erzittern Kommt, wie die Brandung Wenn Flotten zersplittern! Schnell heran, schnell herab, Schneller kommt Al'e!— Häuptling und Bub' und Knapp, Herr und Vasalle!" And now, for something completely different. I offer to you a limmerick, composed with care and not nearly enough caffeine to write properly. There once was a lady in Rome, Whose face was like that of a gnome. She asked for the hand Of a man with much land And sat on his lawn, now her home. ...Yeah. Oh! and here's this, too. abra ca dabra op lorem ipsum holy this line doesn't have an indent but it's in continuity of the structure that sits together. This one ignores the last line since it doesn't have a child with spaces """# let stringRange = NSRange(location: 0, length: testString.utf16.count) let substitutionString = #"```\n\1```"# 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