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

#include <StringConstants.au3> ; to declare the Constants of StringRegExp #include <Array.au3> ; UDF needed for _ArrayDisplay and _ArrayConcatenate Local $sRegex = "^(?:(?:25[0-5]|(?:2[0-4]|1\d|[1-9]|)\d)\.?\b){4}$" Local $sString = "Purpose:" & @CRLF & _ "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." & @CRLF & _ "Later down the road, at task 10-13 we start introducing them to lookarounds. These are fairly basic for now." & @CRLF & _ "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." & @CRLF & _ "Task 16 and forward is supposed to introduce \G and other nifty ideas. This is where its supposed to be more advanced and trickier." & @CRLF & _ "" & @CRLF & _ "" & @CRLF & _ "Task order:" & @CRLF & _ " 1: Word Boundaries" & @CRLF & _ " 2: Capitalize i" & @CRLF & _ " 3: Uppercase Consonants" & @CRLF & _ " 4: Retreive Numbers" & @CRLF & _ " 5: Whitespaces" & @CRLF & _ " 6: Broken Keyboard" & @CRLF & _ " 7: Validate an IP" & @CRLF & _ " 8: Html Tags" & @CRLF & _ " 9: Validate floating numbers" & @CRLF & _ " 10: Followed by #" & @CRLF & _ " 11: Spam filter" & @CRLF & _ " 12: Match an E-Mail (Simplified)" & @CRLF & _ " 13: Not surrounded by digits" & @CRLF & _ " 14: Repeated words" & @CRLF & _ " 15: start before end" & @CRLF & _ " 16: Every other digit" & @CRLF & _ " 17: The thousands" & @CRLF & _ " 18: Quoted text with escapes" & @CRLF & _ " 19: Replace text, not code" & @CRLF & _ " 20: Tokenized list" & @CRLF & _ " 21: Replace in between - Match * inside square brackets" & @CRLF & _ " 22: Outermost Brackets" & @CRLF & _ "" & @CRLF & _ "Task 1: Word Boundaries" & @CRLF & _ " Strings: " & @CRLF & _ " word valid" & @CRLF & _ " aworda invalid" & @CRLF & _ " thisisnotaword invalid" & @CRLF & _ " wordnot invalid" & @CRLF & _ " wor invalid" & @CRLF & _ " wOrdd invalid" & @CRLF & _ " WORD valid" & @CRLF & _ " WORDz invalid" & @CRLF & _ " zWORD invalid" & @CRLF & _ " Valid regex:" & @CRLF & _ " /\bword\b/i" & @CRLF & _ " " & @CRLF & _ "Task 2: Capitalize" & @CRLF & _ " Strings:" & @CRLF & _ " I am a cat invalid (questionable)" & @CRLF & _ " this is invalid invalid" & @CRLF & _ " i am a cat valid -> I am a cat" & @CRLF & _ " capitalize this i valid -> capitalize this I" & @CRLF & _ " abc i abc valid -> abc I abc" & @CRLF & _ " ii i ii valid -> ii I ii" & @CRLF & _ " i valid -> I" & @CRLF & _ " abc i abc i abc i abc valid -> abc I abc I abc I abc" & @CRLF & _ " Valid regsub:" & @CRLF & _ " /\bi\b/g I" & @CRLF & _ " " & @CRLF & _ "Task 3: Uppercase Consonants" & @CRLF & _ " Strings:" & @CRLF & _ " BCDFGHJKLMNPQRSTVXZ valid" & @CRLF & _ " Valid regex:" & @CRLF & _ " /[B-DF-HJ-NP-TV-Z]/g" & @CRLF & _ " " & @CRLF & _ "Task 4: Retreive Numbers" & @CRLF & _ " Strings:" & @CRLF & _ " 12345 valid -> 12345" & @CRLF & _ " 12abc12 valid -> 12, 12" & @CRLF & _ " 1a2b3 valid -> 1, 2, 3" & @CRLF & _ " Valid Regex:" & @CRLF & _ " /(\d+)/g" & @CRLF & _ " " & @CRLF & _ "Task 5: Whitespaces" & @CRLF & _ " Strings:" & @CRLF & _ " ??" & @CRLF & _ " Valid regex:" & @CRLF & _ " /[ ]{4}/S" & @CRLF & _ " " & @CRLF & _ "Task 6: Match a e-mail (simplified)" & @CRLF & _ " Strings:" & @CRLF & _ " @someone.com invalid" & @CRLF & _ " a.@com invalid" & @CRLF & _ " .a@.com invalid" & @CRLF & _ " .@.com invalid" & @CRLF & _ " .@abc.com invalid" & @CRLF & _ " email@.a.com invalid" & @CRLF & _ " mail@mail.com valid" & @CRLF & _ " ()[]\;:,<>@example.com invalid" & @CRLF & _ " A@b@c@example.com invalid" & @CRLF & _ " abc@abc.loooong invalid" & @CRLF & _ " abc@abc.abc.com valid" & @CRLF & _ " Valid regex:" & @CRLF & _ " /^[\w.%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i" & @CRLF & _ " " & @CRLF & _ "Task 7: Validate an IP" & @CRLF & _ " Strings:" & @CRLF & _ " too lazy for this one lol" & @CRLF & _ " Valid regex:" & @CRLF & _ " /^(?:(?:[01]?\d?\d|2[0-4]\d|25[0-5])\.){3}(?:[01]?\d?\d|2[0-4]\d|25[0-5])$/" & @CRLF & _ " " & @CRLF & _ "Task 8: HTML Tags" & @CRLF & _ " Strings:" & @CRLF & _ " ..." & @CRLF & _ " Valid regex:" & @CRLF & _ " /<[^>]*|[^<]*>/g" & @CRLF & _ " " & @CRLF & _ "Task 9: Validate floating numbers" & @CRLF & _ " Strings:" & @CRLF & _ " 1.0 valid" & @CRLF & _ " 12,123 valid" & @CRLF & _ " +10 valid" & @CRLF & _ " -0.1 valid" & @CRLF & _ " -.1 valid" & @CRLF & _ " -1.5e20 valid" & @CRLF & _ " +50e1 valid" & @CRLF & _ " +20.0 valid" & @CRLF & _ " +1.01e10 valid" & @CRLF & _ " +1. invalid" & @CRLF & _ " +1 valid" & @CRLF & _ " .+1 invalid" & @CRLF & _ " 1.4e10 valid" & @CRLF & _ " 1.4.e10 invalid" & @CRLF & _ " . invalid" & @CRLF & _ " .e0 invalid" & @CRLF & _ " Valid regex:" & @CRLF & _ " /^[+-]?(?:\d+(?:\.(?!$))?|\.)\d*(?:e\d+)?$/ need update" & @CRLF & _ " " & @CRLF & _ "Task 10: Broken keyboard" & @CRLF & _ " Strings:" & @CRLF & _ " ..." & @CRLF & _ " Valid regex:" & @CRLF & _ " /(.)\1\1/" & @CRLF & _ " " & @CRLF & _ "Task 11: Followed by #" & @CRLF & _ " Strings:" & @CRLF & _ " ..." & @CRLF & _ " Valid regex:" & @CRLF & _ " /(.)(?=#)/g" & @CRLF & _ "" & @CRLF & _ "Task 12: Spam filter" & @CRLF & _ " Strings:" & @CRLF & _ " ..." & @CRLF & _ " Valid regex:" & @CRLF & _ " /^(?!.*(filter|mirc|not allowed)).*(?:http:\/\/|www\.|porn|credit card)/i" & @CRLF & _ " " & @CRLF & _ "Task 13: Not surrounded by digits" & @CRLF & _ " Strings:" & @CRLF & _ " ..." & @CRLF & _ " Valid regex:" & @CRLF & _ " /(?<!\d)\.|\.(?!\d)/g" & @CRLF & _ "" & @CRLF & _ "Task 14: Repeated words" & @CRLF & _ " Strings:" & @CRLF & _ " ..." & @CRLF & _ " Valid regex:" & @CRLF & _ " /(\b\w{4,}\b)(?=(?:.*\b\1\b){2})(?!(.*\b\1\b){3})/ig" & @CRLF & _ "" & @CRLF & _ "Task 15: start before end" & @CRLF & _ " Strings:" & @CRLF & _ " ..." & @CRLF & _ " Valid regex:" & @CRLF & _ " /^(?:(?!end).)*start/" & @CRLF & _ " " & @CRLF & _ "Task 16: Every other digit" & @CRLF & _ " Strings:" & @CRLF & _ " ..." & @CRLF & _ " Valid regsub:" & @CRLF & _ " /\G((?:.\D)*.)\d/g \1*" & @CRLF & _ " " & @CRLF & _ "Task 17: The thousands" & @CRLF & _ " Strings:" & @CRLF & _ " 100 invalid" & @CRLF & _ " 1000 valid -> 1,000" & @CRLF & _ " 9999 valid -> 9,999" & @CRLF & _ " 12345 valid -> 12,345" & @CRLF & _ " 9999999999999999 valid -> 9,999,999,999,999,999" & @CRLF & _ " Valid regsub:" & @CRLF & _ " /(\d)(?=(\d{3})+\b)/ \1," & @CRLF & _ " " & @CRLF & _ "Task 18: Quoted text with escapes" & @CRLF & _ " Strings:" & @CRLF & _ " ..." & @CRLF & _ " Valid regex:" & @CRLF & _ " /^"((?>\\.|[^"])*)"$/" & @CRLF & _ " " & @CRLF & _ "Task 21: Match * inside square brackets" & @CRLF & _ " Strings:" & @CRLF & _ " ..." & @CRLF & _ " Valid regex/regsub:" & @CRLF & _ " /^([^[]+)|([^]]+)$|(\][^[]+\[)|\*/ \1\2\3" & @CRLF & _ " /(?(?=^)[^]]*\[\K|(?(?=\])..*?\[\K|\*))/g" & @CRLF & _ " " & @CRLF & _ "Task 22: Outermost Brackets" & @CRLF & _ " Strings: " & @CRLF & _ " ..." & @CRLF & _ " Valid regex:" & @CRLF & _ " /(\((?>[^()]+|(?R))+\))/g" & @CRLF & _ " " & @CRLF & _ "Task 23: Validate string with X lower case and X upper case characters." & @CRLF & _ "" & @CRLF & _ "Left to document:" & @CRLF & _ " Match regex" & @CRLF & _ " Match a valid mathematical expression" & @CRLF & _ " Highlight text with colors (maintaining current color codes)" & @CRLF & _ " Palindromes" & @CRLF & _ " Ignoring punctuation" & @CRLF & _ "" & @CRLF & _ "Task 26: Word Boundaries" & @CRLF & _ " Strings:" & @CRLF & _ " Check if a string contains the word word in it (case insensitive). If you have no idea, I guess you could try /word/." & @CRLF & _ " Valid regex/regsub:" & @CRLF & _ " /\bword\b/i" & @CRLF & _ "" & @CRLF & _ "Task 27: Capitalizing I" & @CRLF & _ " Strings:" & @CRLF & _ " 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?." & @CRLF & _ " A regex match is replaced with the text in the Substitution field when using substitution." & @CRLF & _ " Valid regex/regsub:" & @CRLF & _ " /\bi\b/I/g" & @CRLF & _ "" & @CRLF & _ "Task 28: Uppercase Consonants" & @CRLF & _ " Strings:" & @CRLF & _ " 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...." & @CRLF & _ " Example: the regex /./g will return 3 when run against the string abc." & @CRLF & _ " Valid regex/regsub:" & @CRLF & _ " /[^AEIOUa-z_\W\d]\g" & @CRLF & _ "" & @CRLF & _ "Task 29: Retrieve Numbers" & @CRLF & _ " Strings:" & @CRLF & _ " Count the number of integers in a given string. Integers are, for example: 1, 2, 65, 2579, etc." & @CRLF & _ " Valid regex/regsub:" & @CRLF & _ " /\d+/g" & @CRLF & _ "" & @CRLF & _ "Task 30: Whitespace" & @CRLF & _ " Strings:" & @CRLF & _ " Find all occurrences of 4 or more whitespace characters in a row throughout the string." & @CRLF & _ " Valid regex/regsub:" & @CRLF & _ " /\s{4,}/g" & @CRLF & _ "" & @CRLF & _ "Task 31: Broken Keyboard" & @CRLF & _ " Strings:" & @CRLF & _ " 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." & @CRLF & _ " Can you ppplease help me fix thhhis?" & @CRLF & _ " Valid regex/regsub:" & @CRLF & _ " /(.)\1{2}/$1/g" & @CRLF & _ "" & @CRLF & _ "Task 32: Validate IP" & @CRLF & _ " Strings:" & @CRLF & _ " 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." & @CRLF & _ " Valid regex/regsub:" & @CRLF & _ " /^(?:(?:25[0-5]|(?:2[0-4]|1\d|[1-9]|)\d)\.?\b){4}$/" & @CRLF & _ "" & @CRLF & _ "Task 33: HTML TAGS" & @CRLF & _ " Strings:" & @CRLF & _ " Strip all HTML tags from a string. HTML tags are enclosed in < and >." & @CRLF & _ " 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." & @CRLF & _ " Note: This task is meant to be a learning exercise, and not necessarily the best way to parse HTML." & @CRLF & _ " Valid regex/regsub:" & @CRLF & _ " /<?[^<]*>|<.*//g" & @CRLF & _ "" & @CRLF & _ "Task 34: MATCH AN EMAIL" & @CRLF & _ " Strings:" & @CRLF & _ " Verify that a given e-mail address is valid." & @CRLF & _ " We all know how complex emails are, but despite this, let's give it a try and see what we can come up with." & @CRLF & _ " You could start by trying to match contact@regex101.com (denoted as <local-part>@<domain>.<top-level-domain>)." & @CRLF & _ " Valid regex/regsub:" & @CRLF & _ " /^((?!\.)[\w\d](?!.*\.\.)[a-zA-Z0-9\.!#$%&'*+\-\/=?^_`{|}~]*[^\.\s])@([a-zA-Z][a-zA-Z\d-]+[a-zA-Z\d]+\.)+[a-zA-Z]{2,6}$/gm" & @CRLF & _ "" & @CRLF & _ "Task 35: Followed bY" & @CRLF & _ " Strings:" & @CRLF & _ " For every occurrence of the char #, match the previous character and save it in a group (backreference)." & @CRLF & _ " Example: for the text "a#bc# -#", set backreferences with a, c and -." & @CRLF & _ " You are not allowed to consume the hash character." & @CRLF & _ " Valid regex/regsub:" & @CRLF & _ " /(\S)(?=#)/g" & @CRLF & _ "" & @CRLF & _ "Task 36: Validate Floating Point" & @CRLF & _ " Strings:" & @CRLF & _ " Check if a floating point number (e.g. 3.14159) is in a valid format." & @CRLF & _ " Valid regex/regsub:" & @CRLF & _ " /^[-+]?(\d+[,.]|\d*[.,]?\d+)(e[-+]?\d+)?$/i" & @CRLF & _ "" & @CRLF & _ "Task 37: Match any number between 0-100" & @CRLF & _ " Strings:" & @CRLF & _ " Could you help me validate my input and only match positive integers between the range of 0 and 100?" & @CRLF & _ " There can be several numbers in a string which I would want to retrieve." & @CRLF & _ " Try out these example strings:" & @CRLF & _ " Sam has 200 apples. He gives Todd 20 and Mary 125." & @CRLF & _ " The weather is -5 C today, but will be +5 C tomorrow." & @CRLF & _ " Valid regex/regsub:" & @CRLF & _ " /\b(?<!-)(?:\d{1,2}|100)\b/gmi" & @CRLF & _ "" & @CRLF & _ "Task 38: Match alternating 0s 1s" & @CRLF & _ " Strings:" & @CRLF & _ " 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." & @CRLF & _ " Try matching this: 0101010, 1010101010 or 1" & @CRLF & _ " Valid regex/regsub:" & @CRLF & _ " /\b(?!\d*(\d)\1)[10]+\b/gmi" & @CRLF & _ "" & @CRLF & _ "Task 39: Spam Filter" & @CRLF & _ " Strings:" & @CRLF & _ " 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." & @CRLF & _ " Don't use word boundaries (anywhere in the text is fine)." & @CRLF & _ " Valid regex/regsub:" & @CRLF & _ " /^(?!.*(filter|mirc|not allowed)).*(?:http:\/\/|www\.|porn|credit card)/i" & @CRLF & _ "" & @CRLF & _ "Task 40: Not surrounded by digits" & @CRLF & _ " Strings:" & @CRLF & _ " 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-" & @CRLF & _ " Valid regex/regsub:" & @CRLF & _ " /(?<!\d(?=\.\d))\./g" & @CRLF & _ "" & @CRLF & _ "Task 41: Repeated Words" & @CRLF & _ " Strings:" & @CRLF & _ " 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)." & @CRLF & _ " If so, set one (and only one) backreference for each word." & @CRLF & _ " Valid regex/regsub:" & @CRLF & _ " /(\b\w{4,}\b)(?=(?:.*\b\1\b){2})(?!(.*\b\1\b){3})/ig" & @CRLF & _ "" & @CRLF & _ "Task 42: Start before end" & @CRLF & _ " Strings:" & @CRLF & _ " Only match lines with the text start, unless text end appears prior to start. Note: end may or may not be in the string." & @CRLF & _ " Match start line_end; and don't match line_end; start" & @CRLF & _ " Valid regex/regsub:" & @CRLF & _ " /^(?:(?!end).)*start/" & @CRLF & _ "" & @CRLF & _ "Task 43: Every other digit" & @CRLF & _ " Strings:" & @CRLF & _ " Replace every other character if it's a \d with * (only those in even positions: 2, 4, 6, etc)." & @CRLF & _ " Example: a1b2cde3~g45hi6 should become a*b*cde*~g4*hi6" & @CRLF & _ " Valid regex/regsub:" & @CRLF & _ " /((?:.\D)*.)\d/$1*/gA" & @CRLF & _ "" & @CRLF & _ "" & @CRLF & _ "" & @CRLF & _ "" & @CRLF & _ "" & @CRLF & _ "" & @CRLF & _ "" & @CRLF & _ "" & @CRLF & _ "" & @CRLF & _ "" & @CRLF & _ "" & @CRLF & _ "" & @CRLF & _ "" & @CRLF & _ "" & @CRLF & _ "" & @CRLF & _ "" & @CRLF & _ "" & @CRLF & _ "" & @CRLF & _ "" & @CRLF & _ "" & @CRLF & _ "" & @CRLF & _ "" & @CRLF & _ "" & @CRLF & _ "" & @CRLF & _ "" & @CRLF & _ "" & @CRLF & _ "" & @CRLF & _ "" & @CRLF & _ "" & @CRLF & _ "" & @CRLF & _ "" & @CRLF & _ "" & @CRLF & _ "" & @CRLF & _ "" & @CRLF & _ "" & @CRLF & _ "" & @CRLF & _ "" & @CRLF & _ "" & @CRLF & _ "" & @CRLF & _ "" & @CRLF & _ "" & @CRLF & _ "" & @CRLF & _ "" & @CRLF & _ "" Local $aArray = StringRegExp($sString, $sRegex, $STR_REGEXPARRAYFULLMATCH) ; Present the entire match result _ArrayDisplay($aArray, "Result")

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 AutoIt, please visit: https://www.autoitscript.com/autoit3/docs/functions/StringRegExp.htm