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

/
/
gm

Test String

Code Generator

Generated Code

re = /^(The|A|An)/m str = 'A Great Calamity. An Original idea and Time of the Day. Alice in Wonderland and a and the Tools The Runner and a Scam The Boy and the Car and a Box They Had a Dream the dream is here Try clicking the "Run Match" button (or F5) to see what happens. After a successful match click in the results box to highlight the matched text. Click the "Replace" button using the "Dates" example. Notice in the results box that the format of all the dates has been altered. Using the "Expression Library" tab double-click another regular expression and "Run Match". Click "Show the Builder" (or Ctrl+D) to start designing your own regular expressions; or enter them directly in the regular expression box. Enter the text you want to search in the sample text box. Expand nodes in the "Regex Analyzer" to see a description of the current regular expression. Right-click a node to edit the expression. --- Sample text --- Comma Separated Values a,b,c,d,e Dog,Cat,Puppy US phone numbers, (800) 325-3535, (650) 555 1212 US zip codes, 95076-1234, 90210-6473 Dates:12/25/02 9/11/2001 11/22/63 12/7/41 URLs: http://www.usgs.gov http://www.acl.lanl.gov/URI/archive/uri-archive.index.html ftp://@host.com/ ftp://host.com/ ftp://foo:@host.com/ ftp://myname@host.dom/%2Fetc/motd file://vms.host.edu/disk$user/my/notes/note12345.txt prospero://host.dom//pros/name IP addresses: 123.54.63.0 255.257.0.1 -> This one has an illegal number 0.255.255.12 UK Postal Codes: M1 1AA M60 1NW ABD3 7BB -> This is an illegal format M1 1CA -> This has illegal letters (CIKMOV) in the second half W1A 1HQ Canadian Postal Codes: A1A 1A1 B6C 8X7 5 Shoreham Drive, Downsview, Ontario, M3N 1S4 Netherlands Postal Codes: 1234 ZA 5678 KL 0123 AZ -> Illegal number, must be 1000-9999 5432 ABQ -> Too many characters Here is some text as bait for the multiple captures example: XYZAbcAbcAbcXYZAbcAb acacacac Paris in the the spring {a[b(c)d]e} Email: esupport@ultrapico.com maynard@krebs.com Hyperlinks <a href="http://www.codeproject.com">The Code Project</a> Href="http://ultrapico.com/Expresso.htm" Key-Value Pairs FurryAnimal=Dog NonFurryAnimal=Turtle HTML Tags <p>Hello World</p> <H1>Regular Expressions</H1> Social Security Numbers 123-45-6789 987-65-4321 Mastercard 5134567890123456 Visa 4234567890123 Amex 343456789012345 Diners Club 30045678901234 Discover 6011567890123456 Numbers 123.56 -234.5 12 +13 0.56 -.2 .3 25. 1.38E-23 0.2E45 6E+19 0AFD 1CA0234F' # Print the match result str.scan(re) do |match| puts match.to_s end

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 Ruby, please visit: http://ruby-doc.org/core-2.2.0/Regexp.html