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
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
  • Match everything enclosed
    (?:...)
  • Capture everything enclosed
    (...)
  • 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

/
/

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"(CA\d{16}).*([A-Za-z0-9].CA) " test_str = ("CA9458541531029720 0 Õ  € Q X a j r t „€ ‘ –   JOSEAREVALO6156479343/31/1993F2786386CA Xñ\n" "CA9458570739857370 0 Õ¥ MaryjhuneBragado5609341351/17/1982b9356556CA `ñ\n" "CA9458570741955170 0 Õ  € R X a j r t „€ ‘ –   jameswalker4331149435/15/1959b9234757CA dñ\n" "CA9066056240517090 0 ìÔ  € R U ^ g o q €Œ Ž “  BRIANTIK5458156197/16/1975B3342077CA 4ñ\n" "CA9458031025531130 0 íÔ  U [ d m u w ‡€ ’ — ¡ TRINIDADSANTOS5533361392/19/1959N5248608CA 5ñ\n" "CA9458292533964780 0 îÔ  € R [ d m u w ‡€‘ “ ˜ ¢ adelagutierrez5548982784/13/1971a8350300CA 6ñ\n" "CA9066056290801050 0 ïÔ  S U ^ h p r ‚€‹  ’ œ EDMONDYU57208906011/30/1953N3297882CA 7ñ\n" "CA9458292564880620 0 ðÔ  € Q X a j q s ƒ€Œ Ž “  KyleEastman6265480213/15/1990D989010CA 8ñ\n" "CA9458292596399750 0 ñÔ  R W ` h p r ‚€  ” ž dianegomez5472938769/7/1963c0290788CA 9ñ\n" "CA9066056220118960 0 òÔ  € S X a i q s ƒ€Œ Ž “  ALICIADAVIS5695934389/4/1962C0388640CA :ñ\n" "CA9458292520067250 0 óÔ  S Y b j q s ƒ€Ž  • Ÿ BeckiiBarton3098605121/5/1968E787241CA ;ñ\n" "CA9066056262386540 0 ôÔ  € R X a k s u …€Œ Ž “  DAVIDFOSTER62122280512/21/1988d6853927CA <ñ\n" "CABAZONCA9223095184920980 0 õÔ  S \\ e n v x ˆ€‘ “ ˜ ¢ kelleycarpenter5494747231/22/1962n8892633CA =ñ\n" "CA9458292558065680 0 öÔ  € Q U ^ g o q €† ˆ  — carlneal4302148345/10/1971a2722482CA >ñ\n" "CA9530720923065470 0 ÷Ô  V ] f o w y ‰€’ ” ™ £ constancedobbins2472191646/22/1966d3711758CA ?ñ\n" "CA9458251043273810 0 øÔ  € U Z c l u w ‡€’ ” ™ £ katalinagomez61940050311/3/1989e19556116CA @ñ\n" "CA9066056223622510 0 ùÔ  R W ` i q s ƒ€Œ Ž “  BryanKlein6000974524/18/1969F2898009CA Añ\n" "CA9458292536237930 0 úÔ  € R [ d l t v †€‘ “ ˜ ¢ davidvelasquez5513188625/2/1965a5657005CA Bñ\n" "CA9066056292863440 0 ûÔ  S [ d n v x ˆ€“ • š ¤ MiguelQuintero57135015811/19/1969A4071434CA Cñ\n" "CA9066056286159880 0 üÔ  € Q W ` i q s ƒ€Œ Ž “  johnlarson5707996146/14/1967c3807957CA Dñ\n" "CA9458392582819850 0 ýÔ  S Z c l t v †€ ‘ –   cherylkovacik3417255601/16/1960C3577212CA Eñ\n" "CA9458392573576640 0 þÔ  € Q X a k s u …€ ’ — ¡ SaraTurcios62405747711/21/1984D6159809CA Fñ\n" "CA9066056285295550 0 ÿÔ  V ^ g p x z Š€• — œ ¦ GENEVIEVEWILLIAMS5554784895/11/1961N8713815CA Gñ\n" "CA9066056278752300 0 Õ  € R Y b l t v †€ ‘ –   dianawhitney56963328110/19/1964c2511736CA Hñ\n" "CA9458392582846560 0 Õ  Q U ^ f n p €€‹  ’ œ Rubyrojo5505175434/7/1977b6078175CA Iñ\n" "CA9066056266591570 0 Õ  € S Y b k s u …€Œ Ž “  DebbieWallis5521147635/22/1962N8062380CA Jñ\n" "CA9223095184920980 0 Õ  T Y b j r t „€ ‘ –   caridadavila5604585905/7/1976b3393184CA Kñ\n" "CA9066033324713270 0 Õ  € P T ] e m o €ˆ Š  ™ TomMini5568607044/7/1952E0158545CA Lñ\n" "CA9458351055771600 0 Õ  Q V _ h p r ‚€‹  ’ œ JohnCrain5567459788/19/1948R0055874CA Mñ\n" "CA9458392520042480 0 Õ  € P T ] g o q €Š Œ ‘ › MANWONG54505011411/30/1962C5674431CA Nñ\n" "CA9458351036739820 0 Õ  Q W ` i q s ƒ€Ž  • Ÿ josegarcia6201498175/11/1966c3174253CA Oñ\n" "CA9066032333159770 0 Õ  € S W ` i q s ƒ€Œ Ž “  danielsoto6187064407/21/1964b3054964CA Pñ\n" "CA9211661989032070 0 Õ  Q Z c l t v †€‘ “ ˜ ¢ kararay craig45539607110/6/1975B5902264CA Qñ") matches = re.search(regex, test_str) 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