Regular Expressions 101

Save & Manage Regex

  • Current Version: 1
  • Save & Share
  • Community Library

Flavor

  • PCRE2 (PHP)
  • ECMAScript (JavaScript)
  • Python
  • Golang
  • Java
  • .NET 7.0 (C#)
  • Rust
  • PCRE (Legacy)
  • Regex Flavor Guide

Function

  • Match
  • Substitution
  • List
  • Unit Tests
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
Processing...

Test String

Substitution
Processing...

Code Generator

Generated Code

$re = '/(?m)^(?:(?:(?&V_Obj)|(?&V_Ary))|(?<Invalid>(?&Er_Obj)|(?&Er_Ary))(*SKIP)(*FAIL))(?(DEFINE)(?<Sep_Ary>(?:,(?!\s*[}\]])|(?=\s*[\]])))(?<Sep_Obj>(?:,(?!\s*[}\]])|(?=\s*[}])))(?<Er_Obj>(?>{(?:\s*(?&Str)(?:\s*:(?:\s*(?:(?&Er_Value)|(?<Er_Ary>\[(?:\s*(?:(?&Er_Value)|(?&Er_Ary)|(?&Er_Obj))(?:\s*(?&Sep_Ary)|(*ACCEPT)))*(?:\s*\]|(*ACCEPT)))|(?&Er_Obj))(?:\s*(?&Sep_Obj)|(*ACCEPT))|(*ACCEPT))|(*ACCEPT)))*(?:\s*}|(*ACCEPT))))(?<Er_Value>(?>(?&Numb)|(?>true|false|null)|(?&Str)))(?<Str>(?>"[^\\\\"]*(?:\\\\[\s\S][^\\\\"]*)*"))(?<Numb>(?>[+-]?(?:\d+(?:\.\d*)?|\.\d+)(?:[eE][+-]?\d+)?|(?:[eE][+-]?\d+)))(?<V_KeyVal>(?>\s*(?&Str)\s*:\s*(?&V_Value)\s*))(?<V_Value>(?>(?&Numb)|(?>true|false|null)|(?&Str)|(?&V_Obj)|(?&V_Ary)))(?<V_Ary>\[(?>\s*(?&V_Value)\s*(?&Sep_Ary))*\s*\])(?<V_Obj>{(?>\s*(?&V_KeyVal)\s*(?&Sep_Obj))*\s*}))/'; $str = ' ================================ Valid JSON : ================================= 1. Nested Objects and Arrays (Company Structure) This example demonstrates a company structure with departments, each containing a list of employees. { "company": "Global Solutions Inc.", "headquarters": { "city": "New York", "state": "NY", "zip": "10001" }, "departments": [ { "name": "Engineering", "manager": { "id": "ENG001", "firstName": "Alice", "lastName": "Johnson" }, "employees": [ { "id": "EMP001", "firstName": "Bob", "lastName": "Williams", "position": "Software Engineer", "skills": ["Python", "Java", "SQL"] }, { "id": "EMP002", "firstName": "Charlie", "lastName": "Davis", "position": "DevOps Engineer", "skills": ["AWS", "Docker", "Kubernetes"] } ] }, { "name": "Marketing", "manager": { "id": "MKT001", "firstName": "David", "lastName": "Miller" }, "employees": [ { "id": "EMP003", "firstName": "Eve", "lastName": "Taylor", "position": "Marketing Specialist", "skills": ["SEO", "Content Creation"] } ] } ], "projects": [ { "projectId": "PROJ101", "projectName": "New Product Launch", "status": "In Progress", "teamMembers": ["EMP001", "EMP003"] } ] } 2. GeoJSON Feature Collection (Geospatial Data) This example uses the GeoJSON standard to represent a collection of geographic features. { "type": "FeatureCollection", "features": [ { "type": "Feature", "properties": { "name": "Eiffel Tower", "country": "France", "city": "Paris" }, "geometry": { "type": "Point", "coordinates": [2.2945, 48.8584] } }, { "type": "Feature", "properties": { "name": "Central Park", "country": "USA", "city": "New York" }, "geometry": { "type": "Polygon", "coordinates": [ [ [-73.981, 40.768], [-73.958, 40.796], [-73.949, 40.793], [-73.981, 40.768] ] ] } } ] } 3. Product Catalog with Variations (E-commerce) This JSON structure represents a product catalog with various product details and variations. { "catalogId": "CAT001", "products": [ { "productId": "PROD001", "name": "T-Shirt", "description": "Comfortable cotton t-shirt.", "price": 19.99, "availableSizes": ["S", "M", "L", "XL"], "colors": [ { "name": "Red", "hexCode": "#FF0000", "stock": { "S": 10, "M": 15, "L": 8, "XL": 5 } }, { "name": "Blue", "hexCode": "#0000FF", "stock": { "S": 12, "M": 20, "L": 10, "XL": 7 } } ], "reviews": [ { "userId": "USER001", "rating": 5, "comment": "Great quality and fit!" }, { "userId": "USER002", "rating": 4, "comment": "Good t-shirt, a bit pricey." } ] }, { "productId": "PROD002", "name": "Jeans", "description": "Classic denim jeans.", "price": 49.99, "availableSizes": ["28", "30", "32", "34"], "colors": [ { "name": "Indigo", "hexCode": "#4B0082", "stock": { "28": 7, "30": 10, "32": 12, "34": 8 } } ] } ] } ========================= Invalid JSON : ========================= These examples illustrate various ways in which a JSON file can be considered invalid according to the JSON specification. Developers often encounter these types of errors during data serialization or parsing. ----------------- 1. Missing Double Quotes around Keys: Code { name: "Alice", "age": 30, "city": "New York" } Invalidity: The key name is not enclosed in double quotes, which is a requirement in JSON. ----------------- 2. Trailing Commas in Objects or Arrays: Code { "item1": "value1", "item2": "value2", } Invalidity: There is a trailing comma after the last key-value pair in the object. Code [ "element1", "element2", ] Invalidity: There is a trailing comma after the last element in the array. ----------------- 3. Incorrect Data Types or Values: Code { "product": "Laptop", "price": $1200.00, "available": True } Invalidity: The price value $1200.00 is not a valid JSON number (currency symbols are not allowed). The available value True is not a valid JSON boolean (must be lowercase true or false). ----------------- 4. Mismatched Braces or Brackets: Code { "user": { "id": 123, "name": "Bob", "details": [ "email": "bob@example.com" } ] } Invalidity: The details array contains an object that is closed with a } instead of a ], and the array itself is closed with a ] after the object, leading to a structural mismatch. ----------------- 5. Unescaped Characters within Strings: Code { "message": "He said, "Hello!"" } Invalidity: The double quotes within the string "Hello!" are not escaped, causing a syntax error. They should be \\"Hello!\\". ----------------- 6. Comments within JSON: Code { // This is a comment "data": "some value" } Invalidity: JSON does not support comments. ----------------- 7. Missing Colons or Values in Key-Value Pairs: Code { "key1": "value1", "key2" "key3": "value3" } Invalidity: The key2 entry is missing a colon and a value. ----------------- 8. Invalid JSON Structure (e.g., array with object syntax): Code [ "item1": "value1", "item2": "value2" ] Invalidity: An array cannot contain key-value pairs; it should contain elements directly. { "key1" : "value1", "key2" "lk" } ----------------- { "key1" : "value1" "key2" "lk" } ----------------- { "key1" : "value1", "key2" "lk" } ----------------- { "key1" : "value1", "key2" : mj } ----------------- { "key1" : "value1", "key2" : "mj } ----------------- { "key1" : "value1", "hhh" : [ { "key0" : [{"MMM" : "a"}, "b","c"], "key2" : "mj", "key3" : [ ----------------- [ "mn" {"MMM" : a"} , "b" , "c" ] '; $subst = "$1 ERROR_here>> "; $result = preg_replace($re, $subst, $str); echo "The result of the substitution is ".$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 PHP, please visit: http://php.net/manual/en/ref.pcre.php