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
No Match

/
/
gmi

Test String

Code Generator

Generated Code

using System; using System.Text.RegularExpressions; public class Example { public static void Main() { string pattern = @"^([0-9]{8,10}[^a-zA-Z\s])$"; string input = @"This is a number also 123-456-7890 This is not a number: 123456 This is not a number, but an address 123 456 My parameters are 90/60/90 - 120/150/120 - not a phone number I am cheeky and I use letter 0 instead of zero 604_213_0293 Or I use | symbol and also 604_2|3_0293 may happen The most classical Canadian number is +1(604)123-4567 hi hi Dashes can be different 123-456–7890 And looks like phone +I(800)-2l4-15O but contain combination with letter 'oi+I(222)3334444oi', 'io+0 (222) 333 444Ooi', 'O+l ( 222 ) 3|3 4O40O', '+O+l ( 222 ) 3|3 4O40O', 069123521 0691235523 06912355312 in text 123-567 +(1)-23567 text in text 123567-2 text --------------------------------------- in text 1-800-123-123 1-800-123-123 text in text 1-800-123-123 text --------------------------------------- in text 1-800-123-123 1-800-123-123 text in text 1-800-123-123 text --------------------------------------- n0OOOOOOOOOOOO h1iiiiiiiiiiiii in text +l-800-123-123 1-8oo-I23-i23 text in text +1 (800)-l23-123 text phone '012345678', '0123456789', '01234567891', '012345678912', '0123456789123', '01234567891234', '012345678912345', '800-1234567', '8Oo-I234567-l', '+1(222)3334444', '+1 (222) 333 4444', '+1 ( 222 ) 333 4444', '+1-222-333-4444', '+1 222 333 4444', '236 - 332 - 6669', '(604)123-4567', '604 123 4567', '604_123_4567', '2368337551', '12223334444', '222) 333 4444', '3333 4444', '(437)1234567', '(888)1234123', not a phone '0', '01', '012', '0123', '01234', '012345', '0123456', '01234567', '90-60-90', '90/60/90', '0123456789123456', '+X (XXX) XXX XXXX', '1/23-4', '1 / 23 - 4', '1234/12,3-1234'"; RegexOptions options = RegexOptions.Multiline | RegexOptions.IgnoreCase; foreach (Match m in Regex.Matches(input, pattern, options)) { Console.WriteLine("'{0}' found at index {1}.", m.Value, m.Index); } } }

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 C#, please visit: https://msdn.microsoft.com/en-us/library/system.text.regularexpressions.regex(v=vs.110).aspx