Regular Expressions 101

Save & Share

  • Regex Version: ver. 26
  • Update Regex
    ctrl+⇧+s
  • Save new Regex
    ctrl+s
  • Add to Community Library

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

/
/
xgJ

Test String

Code Generator

Generated Code

using System; using System.Text.RegularExpressions; public class Example { public static void Main() { string pattern = @"(?(DEFINE) (?<add> \s*\+\s* ) (?<eq> \s*=\s* ) # cl: last digit of left operand being 1, cr: last digit of right operand being 1, \d0 checks whether last digit from result is 0 # there will be carry if cl and cr are set, or cl or cr are set and the last digit from result is 0 (?<carry> (?(cl)(?(cr)|\d0)|(?(cr)\d0|(*F))) ) # add carry with l1 (current digit of left operand being 1) and r1 (current digit of right operand being 1) # i.e. returns result of carry + l1 + r1 in Z/2Z (?<digitadd> (?(?= (?(?= (?(l1)(?(r1)|(*F))|(?(r1)(*F))) )(?&carry)|(?!(?&carry))) )1|0) ) # check for a single digit at the current offset whether the result is correct # ro: right operand out of bounds (i.e. the current digit is at a higher offset than the size of the left operand) # if we're out of bounds of the right operand, cr is just not set (i.e. handled as if there were leading zeroes) (?<recursedigit> # now, with the r and f, we can figure out r1 and cr at the current offset and also perform binary carry addition at that offset in the result (?&add) 0*+ (?:\d*(?:0|1(?<r1>)))? (?(ro)|(?=(?<cr>1)?))\k<r> (?&eq) \d*(?&digitadd)\k<f>\b # iterate through the whole left operand to find the sequences (for right operand and result) of the same length as the offset of the current digit | (?=\d* (?&add) 0*+ (?:\k<r>(?<ro>)|\d*(?<r>\d\k<r>)) (?&eq) \d*(?<f>\d\k<f>)\b) \d(?&recursedigit) ) # run the check, sets l1 and cl accordingly and initializes the r (right operand) and f (final result) groups to be empty (?<checkdigit> (?:0|1(?<l1>)) (?=(?<cl>1)?) (?<r>) (?<f>) (?&recursedigit) ) # ""trivial"" increment of a binary number, i.e. a +1 is applied to the part of the right operand which exceeds the length of the left operand (?<carryoverflow> # number contains a zero, just update the part after the last zero (?<x>\d+) 0 (?<y> \k<r> (?&eq) 0*\k<x>1 | 1(?&y)0 ) # number contains only ones, add a leading 1 and replace all the ones by zeroes | (?<z> 1\k<r> (?&eq) 0*10 | 1(?&z)0 ) ) # ensure correct lengths of the final operand and handle right operands being longer than the left operand (?<recurseoverflow> # the left operand is longer than or as long as the right one. In the latter case, the final result will always be exactly one digit longer than the operands # in the former case, if the first non-leading zero (from the left) of the left operand is at a higher or equal offset to the length of the right operand, the final result will be one digit longer than the left operand (?&add) 0*+ (?(rlast) \k<r> (?&eq) 0*+(?(ro)(?:1(?=0))?|1)\k<f>\b # the right operand has a zero at the offset equal to the length of the left operand. Then just copy the leading digits to the final result | (?:(?<remaining>\d+)(?=0\d* (?&eq) \d*(?=1)\k<f>\b)\k<r> (?&eq) (*PRUNE) 0*\k<remaining>\k<f>\b | (?&carryoverflow)\k<f>\b)) # iterate through the whole left operand to find the sequences (for right operand and result) of the same length as the left operand | (?=\d* (?&add) 0*+ (?:\k<r>(?<ro>)|(?=(?:\d\k<r>(?&eq)(?<rlast>))?)\d*(?<r>\d\k<r>)) (?&eq) \d*(?<f>\d\k<f>)\b) \d(?&recurseoverflow) ) (?<s> # Handle 0 + x or x + 0 separately to avoid messing around in the big subpatterns (0*? (?<arg>[01]+) (?&add) 0+ | 0+ (?&add) 0*? (?<arg>[01]+)) (?&eq) (*PRUNE) 0* \k<arg> | 0*+ # traverse the digits one by one and verify the correctness of each offset individually (?=(?<iteratedigits> (?=(?&checkdigit))\d (?:\b|(?&iteratedigits)) )) # assert exact format here (?=[01]+ (?&add) [01]+ (?&eq) [01]+ \b) # force an additional digit on the final result in case the left operand is only ones and the right operand not longer than the left (?<r>) (?<f>) (?&recurseoverflow) ) ) \b(?&s)\b"; string input = @"- 1 + 1 = 0 - 1 + 1 = 1 - 0 + 1 = 11 + 0 + 000 = 0000 + 1 + 1 = 10 + 10 + 000001 = 0000011 - 1 + 10 = 101 - 0 + 11 = 101 + 0 + 1 = 1 - 0 + 0 = 10 + 1 + 0 = 1 - 1 + 101 = 1010 + 10 + 0 = 10 + 10 + 1 = 11 + 11 + 1 = 100 - 11 + 1 = 00 + 100 + 1 = 101 + 01 + 10 = 11 + 10 + 10 = 100 + 010 + 010 = 100 + 101 + 11 = 1000 - 101 + 10 = 1111 - 100 + 10 = 111 - 101 + 10 = 101 + 101 + 10 = 111 + 11 + 101 = 1000 + 11 + 1011 = 1110 + 11 + 10101 = 11000 + 1 + 1111 = 10000 + 111 + 11 = 1010 + 1110 + 100 = 10010 + 11 + 11 = 110 + 1111 + 11 = 10010 - 010 + 010 = 000 - 100 + 100 = 1100 + 100 + 100 = 1000 + 1000 + 100 = 1100 + 1 + 1000 = 1001 + 1000 + 1 = 1001 + 110 + 1100 = 10010 - 110 + 1010 = 1000 - 10 + 1 = 11111001"; RegexOptions options = RegexOptions.IgnorePatternWhitespace; 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