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

/
/
g

Test String

Code Generator

Generated Code

import Foundation let pattern = #"<p>(\s|&nbsp;|<\s?br\s?\/>)*<\/p>"# let regex = try! NSRegularExpression(pattern: pattern) let testString = ##""" <p>ABI - Verbale incontro 10 Nov 2015</p> <p>Gestione Documentale</p> <p>Presenti:</p> <ul> <li>Marco Acorte (Thaos)</li> <li>Giampiero Sareceno (ABI) &lt;g.saraceno@abi.it&gt;</li> <li>Mariani Sergio (ABI) &lt;s.mariani@abi.it&gt;</li> </ul> <h2>Modifiche alla gestione formati html in modifica Scheda ODG</h2> <h3>Editor html</h3> <ul> <li>I paragrafi all&#39;interno del campo testo devono essere tutti giustificati</li> <li>quando si fa copia e incolla da word vengono importati molti tag html con gli stili impostati dal modello di word dal quale &egrave; stato ricavato il documento sorgente, occorre eseguire una pulizia lasciando solo i tag html standard <ul> <li><strong>&lt;span style=&quot;font-weight: bold&quot;&gt;</strong> deve essere equivalente a <strong>&lt;strong&gt;</strong></li> <li><strong>&lt;span style=&quot;text-decoration: underline&quot;&gt; </strong>deve essere equivalente a <strong>&lt;u&gt;</strong></li> <li>eliminare tag <strong>&lt;font&gt;&lt;/font&gt;</strong></li> <li>eliminare paragrafi inutili es:<strong> &lt;p style=&quot;stile1&quot;&gt;&amp;nbps;&lt;/p&gt;</strong></li> <li>togliere gli stili dai <strong>&lt;p&gt;</strong> non vuoti.<br /> Es: <strong>&lt;p style=&quot;stile1&quot;&gt;bla bla bla bla...&lt;/p&gt;</strong> deve diventare <strong>&lt;p&gt;bla bla bla bla...&lt;/p&gt;</strong></li> <li>togliere eventuali commenti in html.<br /> Es: <strong>&lt;!-- sadkfjhgakjdhfjkgkjasdhf &rarr;</strong></li> </ul> </li> <li>Aggiungere tasto &ldquo;mostra Caratteri speciali&rdquo; (non stampabili es. invio, tab, spazio)</li> </ul> <h3>Modello Word</h3> <ul> <li>Paragrafo standard &ndash; aggiungere aggiungere spaziatura dopo ogni paragrafo</li> <li>Punti elenco/numerato - aggiungere spaziatura dopo ogni singola voce</li> </ul> <p>&nbsp;</p> <p>&nbsp;&nbsp;&nbsp;&nbsp;</p> <p></p> <p> </p> <!-- ([space]) --> <p> </p> <!-- (That's a [tab] character in there --> <p>&nbsp;</p> <p><br /></p> <p> &nbsp;</p> <p> <br /><br /> &nbsp;</p> <h2>Richieste nuove funzionalit&agrave;</h2> <h3>Rilascia Schede bloccate</h3> <p>nuova funzione massiva &quot;Rilascia Schede bloccate&quot; da inserire nella pagina &quot;Modifica Ordine del Giorno&quot; (/ordinedelgiorno/AmmModificaODG.aspx) che dato un ODG cambi lo stato delle schede da BLOCCATO --&gt; PRONTO.</p> """## 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