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

/
/

Test String

Code Generator

Generated Code

$re = '/^(?:(?:25[0-5]|(?:2[0-4]|1\d|[1-9]|)\d)\.?\b){4}$/'; $str = 'Purpose: This is supposed to be a learning process. The first 1-9 tasks are supposed to be done with using "standard" regex, ie, teach the user to use \\s, \\w, \\d, aswell as backrefs and similar basic things. Later down the road, at task 10-13 we start introducing them to lookarounds. These are fairly basic for now. At task 14-15 we show them the true power of lookarounds and encourage them to use them properly and what can truly be done with them. Task 16 and forward is supposed to introduce \\G and other nifty ideas. This is where its supposed to be more advanced and trickier. Task order: 1: Word Boundaries 2: Capitalize i 3: Uppercase Consonants 4: Retreive Numbers 5: Whitespaces 6: Broken Keyboard 7: Validate an IP 8: Html Tags 9: Validate floating numbers 10: Followed by # 11: Spam filter 12: Match an E-Mail (Simplified) 13: Not surrounded by digits 14: Repeated words 15: start before end 16: Every other digit 17: The thousands 18: Quoted text with escapes 19: Replace text, not code 20: Tokenized list 21: Replace in between - Match * inside square brackets 22: Outermost Brackets Task 1: Word Boundaries Strings: word valid aworda invalid thisisnotaword invalid wordnot invalid wor invalid wOrdd invalid WORD valid WORDz invalid zWORD invalid Valid regex: /\\bword\\b/i Task 2: Capitalize Strings: I am a cat invalid (questionable) this is invalid invalid i am a cat valid -> I am a cat capitalize this i valid -> capitalize this I abc i abc valid -> abc I abc ii i ii valid -> ii I ii i valid -> I abc i abc i abc i abc valid -> abc I abc I abc I abc Valid regsub: /\\bi\\b/g I Task 3: Uppercase Consonants Strings: BCDFGHJKLMNPQRSTVXZ valid Valid regex: /[B-DF-HJ-NP-TV-Z]/g Task 4: Retreive Numbers Strings: 12345 valid -> 12345 12abc12 valid -> 12, 12 1a2b3 valid -> 1, 2, 3 Valid Regex: /(\\d+)/g Task 5: Whitespaces Strings: ?? Valid regex: /[ ]{4}/S Task 6: Match a e-mail (simplified) Strings: @someone.com invalid a.@com invalid .a@.com invalid .@.com invalid .@abc.com invalid email@.a.com invalid mail@mail.com valid ()[]\\;:,<>@example.com invalid A@b@c@example.com invalid abc@abc.loooong invalid abc@abc.abc.com valid Valid regex: /^[\\w.%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,4}$/i Task 7: Validate an IP Strings: too lazy for this one lol Valid regex: /^(?:(?:[01]?\\d?\\d|2[0-4]\\d|25[0-5])\\.){3}(?:[01]?\\d?\\d|2[0-4]\\d|25[0-5])$/ Task 8: HTML Tags Strings: ... Valid regex: /<[^>]*|[^<]*>/g Task 9: Validate floating numbers Strings: 1.0 valid 12,123 valid +10 valid -0.1 valid -.1 valid -1.5e20 valid +50e1 valid +20.0 valid +1.01e10 valid +1. invalid +1 valid .+1 invalid 1.4e10 valid 1.4.e10 invalid . invalid .e0 invalid Valid regex: /^[+-]?(?:\\d+(?:\\.(?!$))?|\\.)\\d*(?:e\\d+)?$/ need update Task 10: Broken keyboard Strings: ... Valid regex: /(.)\\1\\1/ Task 11: Followed by # Strings: ... Valid regex: /(.)(?=#)/g Task 12: Spam filter Strings: ... Valid regex: /^(?!.*(filter|mirc|not allowed)).*(?:http:\\/\\/|www\\.|porn|credit card)/i Task 13: Not surrounded by digits Strings: ... Valid regex: /(?<!\\d)\\.|\\.(?!\\d)/g Task 14: Repeated words Strings: ... Valid regex: /(\\b\\w{4,}\\b)(?=(?:.*\\b\\1\\b){2})(?!(.*\\b\\1\\b){3})/ig Task 15: start before end Strings: ... Valid regex: /^(?:(?!end).)*start/ Task 16: Every other digit Strings: ... Valid regsub: /\\G((?:.\\D)*.)\\d/g \\1* Task 17: The thousands Strings: 100 invalid 1000 valid -> 1,000 9999 valid -> 9,999 12345 valid -> 12,345 9999999999999999 valid -> 9,999,999,999,999,999 Valid regsub: /(\\d)(?=(\\d{3})+\\b)/ \\1, Task 18: Quoted text with escapes Strings: ... Valid regex: /^"((?>\\\\.|[^"])*)"$/ Task 21: Match * inside square brackets Strings: ... Valid regex/regsub: /^([^[]+)|([^]]+)$|(\\][^[]+\\[)|\\*/ \\1\\2\\3 /(?(?=^)[^]]*\\[\\K|(?(?=\\])..*?\\[\\K|\\*))/g Task 22: Outermost Brackets Strings: ... Valid regex: /(\\((?>[^()]+|(?R))+\\))/g Task 23: Validate string with X lower case and X upper case characters. Left to document: Match regex Match a valid mathematical expression Highlight text with colors (maintaining current color codes) Palindromes Ignoring punctuation Task 26: Word Boundaries Strings: Check if a string contains the word word in it (case insensitive). If you have no idea, I guess you could try /word/. Valid regex/regsub: /\\bword\\b/i Task 27: Capitalizing I Strings: Use substitution to replace every occurrence of the word i with the word I (uppercase, I as in me). E.g.: i\'m replacing it. am i not? -> I\'m replacing it. am I not?. A regex match is replaced with the text in the Substitution field when using substitution. Valid regex/regsub: /\\bi\\b/I/g Task 28: Uppercase Consonants Strings: With regex you can count the number of matches. Can you make it return the number of uppercase consonants (B,C,D,F,..,X,Y,Z) in a given string? E.g.: it should return 3 with the text ABcDeFO!. Note: Only ASCII.... Example: the regex /./g will return 3 when run against the string abc. Valid regex/regsub: /[^AEIOUa-z_\\W\\d]\\g Task 29: Retrieve Numbers Strings: Count the number of integers in a given string. Integers are, for example: 1, 2, 65, 2579, etc. Valid regex/regsub: /\\d+/g Task 30: Whitespace Strings: Find all occurrences of 4 or more whitespace characters in a row throughout the string. Valid regex/regsub: /\\s{4,}/g Task 31: Broken Keyboard Strings: Oh no! It seems my friends spilled beer all over my keyboard last night and my keys are super sticky now. Some of the time whennn I press a key, I get two duplicates. Can you ppplease help me fix thhhis? Valid regex/regsub: /(.)\\1{2}/$1/g Task 32: Validate IP Strings: Validate an IPv4 address. The addresses are four numbered separated by three dots, and can only have a maximum value of 255 in either octet. Start by trying to validate 172.16.254.1. Valid regex/regsub: /^(?:(?:25[0-5]|(?:2[0-4]|1\\d|[1-9]|)\\d)\\.?\\b){4}$/ Task 33: HTML TAGS Strings: Strip all HTML tags from a string. HTML tags are enclosed in < and >. The regex will be applied on a line-by-line basis, meaning partial tags will need to be handled by the regex. Don\'t worry about opening or closing tags; we just want to get rid of them all. Note: This task is meant to be a learning exercise, and not necessarily the best way to parse HTML. Valid regex/regsub: /<?[^<]*>|<.*//g Task 34: MATCH AN EMAIL Strings: Verify that a given e-mail address is valid. We all know how complex emails are, but despite this, let\'s give it a try and see what we can come up with. You could start by trying to match contact@regex101.com (denoted as <local-part>@<domain>.<top-level-domain>). Valid regex/regsub: /^((?!\\.)[\\w\\d](?!.*\\.\\.)[a-zA-Z0-9\\.!#$%&\'*+\\-\\/=?^_`{|}~]*[^\\.\\s])@([a-zA-Z][a-zA-Z\\d-]+[a-zA-Z\\d]+\\.)+[a-zA-Z]{2,6}$/gm Task 35: Followed bY Strings: For every occurrence of the char #, match the previous character and save it in a group (backreference). Example: for the text "a#bc# -#", set backreferences with a, c and -. You are not allowed to consume the hash character. Valid regex/regsub: /(\\S)(?=#)/g Task 36: Validate Floating Point Strings: Check if a floating point number (e.g. 3.14159) is in a valid format. Valid regex/regsub: /^[-+]?(\\d+[,.]|\\d*[.,]?\\d+)(e[-+]?\\d+)?$/i Task 37: Match any number between 0-100 Strings: Could you help me validate my input and only match positive integers between the range of 0 and 100? There can be several numbers in a string which I would want to retrieve. Try out these example strings: Sam has 200 apples. He gives Todd 20 and Mary 125. The weather is -5 C today, but will be +5 C tomorrow. Valid regex/regsub: /\\b(?<!-)(?:\\d{1,2}|100)\\b/gmi Task 38: Match alternating 0s 1s Strings: I\'m trying to match bit sequences which are alternating between 1 and 0 and never have more than one 1 or 0 in a row. They can be single digits. Try matching this: 0101010, 1010101010 or 1 Valid regex/regsub: /\\b(?!\\d*(\\d)\\1)[10]+\\b/gmi Task 39: Spam Filter Strings: Match a string that contains any of the following substrings: http://, www., porn, or credit card. But don\'t match the text if it contains one of: not allowed, filter, or mirc. Don\'t use word boundaries (anywhere in the text is fine). Valid regex/regsub: /^(?!.*(filter|mirc|not allowed)).*(?:http:\\/\\/|www\\.|porn|credit card)/i Task 40: Not surrounded by digits Strings: Replace every . (dot) with a - (hyphen) except when the dot is surrounded by digits. E.g.: .a.b.1.2. should become -a-b-1.2- Valid regex/regsub: /(?<!\\d(?=\\.\\d))\\./g Task 41: Repeated Words Strings: I\'d like to know if a text contains words with 4 characters or more which are repeated 3 or more times in the text (anywhere in the text). If so, set one (and only one) backreference for each word. Valid regex/regsub: /(\\b\\w{4,}\\b)(?=(?:.*\\b\\1\\b){2})(?!(.*\\b\\1\\b){3})/ig Task 42: Start before end Strings: Only match lines with the text start, unless text end appears prior to start. Note: end may or may not be in the string. Match start line_end; and don\'t match line_end; start Valid regex/regsub: /^(?:(?!end).)*start/ Task 43: Every other digit Strings: Replace every other character if it\'s a \\d with * (only those in even positions: 2, 4, 6, etc). Example: a1b2cde3~g45hi6 should become a*b*cde*~g4*hi6 Valid regex/regsub: /((?:.\\D)*.)\\d/$1*/gA '; preg_match($re, $str, $matches, PREG_OFFSET_CAPTURE, 0); // Print the entire match result var_dump($matches);

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 PHP, please visit: http://php.net/manual/en/ref.pcre.php