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

/
/
gsm

Test String

Code Generator

Generated Code

$re = '/^(?s)(\d+)\.(.*?)(?:(?:\r*\n|\r* \n){2})/sm'; $str = ' 1. some text is here. more text unrelated text 2. even more text. text text text you don\'t want more you don\'t want 1. Convert following decimal numbers in signed binary, octal and hexadecimal numbers using minimum possible number of bits. a) 17 b) -17 A decimal number consists of decimal digits (0 to 9), a binary number consists of binary digits (0, 1), octal number consists of octal digits (0 to 7), and a hexadecimal number consists of 16 digits (0 to 9, A, B, C, D, E and F ). A decimal digit can be converted to any other base by following three simple steps mentioned below: ●         Divide the decimal number by the base (i.e. 2 for binary, 8 for octal, and 16 for Hex ) ●         The remainder will form the lowest order digit in the converted number system. ●         Repeat steps 1 and 2 mentioned above until no divisor remains The MSB represents “sign” information. For negative numbers, MSB is one and for positive numbers, MSB is zero. Using the steps mentioned above, we will get a binary value of 10001, octal value of 21 and hexadecimal value of 0x11 for decimal number 17. Since this question specifically asks for signed representation and as we have seen that positive numbers have MSB as 0, binary number “10001” represents a negative number in signed representation. Hence, we can say that 5 bits are insufficient to represent +17 in signed binary number system (as a matter of fact, 5 bits can represent decimal numbers from -16 to 15 only). Therefore, we need 6 bits to represent +17. This means that we need to append one more “0 “ at the beginning of 10001. This would give us 010001 (a 6 bit number), as binary representation of +17. One easy way to convert a positive number to a negative number in binary representation is to take 1’s compliment and add 1 (which is same as 2’s compliment). Also, once we have a binary number, we can easily convert it into octal (grouping three binary bits starting from LSB) or hexadecimal (grouping four binary bits starting from LSB)   Therefore for -17, ●         1’s compliment of +17 (010001) would be “101110”, ●         And adding 1 to above number would give us “101111”   a)      Based on this, the decimal number 17 would be represented as: Binary = 010001 , Octal = 21 , Hexadecimal = 0x11 b)      And decimal number -17 would be represented as: Binary = 101111 , Octal = 57 , Hexadecimal = 0x2f     2. What is the decimal equivalent of hex number 0x3A? To convert a number from a non-decimal base to a decimal base, following steps are required: ●         Start from the least significant digit, and move towards most significant digit. ●         Multiply each digit with “<base> to the power of that <bit position>”, i.e. <base> <bit position> ●         Sum the results over each digit. Therefore: 0x3A = [0xA * 16 0 ] + [0x3 * 16 1 ] = [10*1 + 3*16] = 58                                 3. What is Gray code and what are some of the benefits of using Gray code over Binary code? A Gray code is a binary number system in which two successive values differ only in one bit. It is also known as reflected binary code Following table shows the Gray and Binary code for values from 0 to 7     In Binary code, a transition between two values could have transition on more than two bits and this could sometimes lead to ambiguity if different bits take different time to transition. For example: transitioning from 3 to 4 in binary (011 to 100) requires all the bits to toggle. This can lead to some intermediate values if say the three bits have different switching time. Whereas in Gray code, since only one bit changing any time, there is no possibility of any such ambiguity. One more advantage of using Gray code is: since fewer bits are toggling in Gray code, a design using Gray code would consume less power compared to one using a binary code. 5. For a given binary string: 111001, compute the proper odd parity bit. The given binary string: 111001, has four “1’s”. Using an odd parity, the total number of 1’s in the binary string including the parity bit needs to be odd. Hence, the odd parity bit for this string will be 1 . 6. What are 1\'s complement and 2\'s complement? Where are they used? If all bits in a binary number are inverted by changing each 1 to 0 and each 0 to 1, the resulting binary number is called the 1’s complement. For example: The one’s complement of a binary number 110010 is 001101 The 2’s complement of a binary number is obtained by adding a 1 to the one’s complement of the number. For example: The two’s complement of a binary number 110010 is 001101+1 = 001110 The two’s complement of a number is used to represent signed binary numbers. It is also used for subtraction of binary numbers. The one’s complement is an intermediate step to get to two’s complement. 7. What is a BCD code and how is it different from binary code? What will be the BCD and binary code for decimal number 27? BCD is Binary coded decimal and is a four bit binary code that can be used to represent any decimal digit (from 0 to 9). A binary code is a binary representation of the decimal number, and the number of bits needed for a binary code would depend on the decimal number. For decimal numbers 0 to 9, both BCD and binary code would be same. A number 27 can be represented in BCD by using four bit code for both 2 and 7. Hence, BCD for 27 will be 0010 0111 , while the binary code for 27 will be 11011 8. Which of the following code can represent numbers, characters, and special characters? a. BCD b. Gray c. EBCDIC code d. ASCII code e. Alphanumeric code e) Alphanumeric code. Alphanumeric code is a combination of alphabetic and numeric characters and can be used for representing numbers as well as character(s)/special character(s). '; preg_match_all($re, $str, $matches, PREG_SET_ORDER, 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