Regular Expressions 101

Save & Share

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

/
/
i

Test String

Code Generator

Generated Code

# coding=utf8 # the above tag defines encoding for this document and is for Python 2.x compatibility import re regex = r"\[serviceOrderId:(?<oc_request_id>.[^]]*)\]" test_str = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\"><html><head> <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"></head><body><div><font face=\"Arial\" color=\"#000000\" size=\"2\">***** Reply to this email to append information to [[WO#17561]] *****<br><br>WO# 17561 was amended to include the following information:<br><br>Thank you for contacting us.<br><br><br>We have received your request and will respond within 2 business days.<br><br><br>Your request number is: REQ-20665.<br><br><br>If you would like to speak with a representative, please contact us at 1 855 242-4492 and reference your request number.<br><br><br>Our hours of operation are Monday to Friday, 8:00 am to 5:00 pm AST.<br><br><br>Thank you for your continued business with Bell Aliant.<br><br><br>Client Services<br>Bell Business Markets<br><br><br><br><br><br><br><br>Legal | Privacy | bell.ca <br><br>Corporate Secretary’s Office of Bell Canada, Bell TV, Bell Mobility, Bell Media and Bell Aliant<br>1 carrefour Alexander-Graham-Bell, Building A-7, Verdun, Quebec, H3E 3B3 <br><br><br><br>© Bell Canada, 2020. All rights reserved.<br><br><br><br><br>[serviceOrderId:bsd_REQ-20665] <br><br>Merci de nous avoir contacté.<br><br><br>Nous avons bien reçu votre demande et nous y réponderons dans 2 jours ouvrables.<br><br><br>Votre numéro de demande est : REQ-20665.<br><br><br>Si vous souhaitez parler à un conseiller, veuillez composer le 1 855 242-4492 et faire référence à votre numéro de demande.<br><br><br>Nos heures d'ouverture sont de 8 h à 17 h AST, du lundi au vendredi.<br><br><br>Merci de votre fidélité envers Bell Aliant.<br><br><br>Services clients<br>Bell Marché Affaires<br><br><br><br><br><br><br><br>Avis juridiques | Sécurité | bell.ca <br><br>Bureau du secrétaire de Bell Canada, Bell Télé, Bell Mobilité, Bell Média et Bell Aliant<br>1, carrefour Alexander-Graham-Bell, Aile A-7, Verdun (Québec) H3E 3B3 <br><br><br><br>© Bell Canada, 2020. Tous droits réservés.<br><br><br><br><br>[serviceOrderId:bsd_REQ-20665]<br><br></font></div><hr> <span style=\"font-size: 12px;\"><font color=\"blue\"><em><strong>External Email:</strong> Please use caution when opening links and attachments / </em></font></span><em> <span style=\"font-size: 12px;\"><font color=\"blue\"><strong>Courriel externe:</strong> Soyez prudent avec les liens et documents joints </font></span></em></body></html>" matches = re.search(regex, test_str, re.IGNORECASE) if matches: print ("Match was found at {start}-{end}: {match}".format(start = matches.start(), end = matches.end(), match = matches.group())) for groupNum in range(0, len(matches.groups())): groupNum = groupNum + 1 print ("Group {groupNum} found at {start}-{end}: {group}".format(groupNum = groupNum, start = matches.start(groupNum), end = matches.end(groupNum), group = matches.group(groupNum))) # Note: for Python 2.7 compatibility, use ur"" to prefix the regex and u"" to prefix the test string and substitution.

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 Python, please visit: https://docs.python.org/3/library/re.html