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
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

#include <MsgBoxConstants.au3> ; to declare the Constants of MsgBox Local $sRegex = "(?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*}))" Local $sString = "" & @CRLF & _ "================================" & @CRLF & _ "Valid JSON :" & @CRLF & _ "=================================" & @CRLF & _ "" & @CRLF & _ "1. Nested Objects and Arrays (Company Structure)" & @CRLF & _ "This example demonstrates a company structure with departments, each containing a list of employees." & @CRLF & _ "" & @CRLF & _ "{" & @CRLF & _ " "company": "Global Solutions Inc."," & @CRLF & _ " "headquarters": {" & @CRLF & _ " "city": "New York"," & @CRLF & _ " "state": "NY"," & @CRLF & _ " "zip": "10001"" & @CRLF & _ " }," & @CRLF & _ " "departments": [" & @CRLF & _ " {" & @CRLF & _ " "name": "Engineering"," & @CRLF & _ " "manager": {" & @CRLF & _ " "id": "ENG001"," & @CRLF & _ " "firstName": "Alice"," & @CRLF & _ " "lastName": "Johnson"" & @CRLF & _ " }," & @CRLF & _ " "employees": [" & @CRLF & _ " {" & @CRLF & _ " "id": "EMP001"," & @CRLF & _ " "firstName": "Bob"," & @CRLF & _ " "lastName": "Williams"," & @CRLF & _ " "position": "Software Engineer"," & @CRLF & _ " "skills": ["Python", "Java", "SQL"]" & @CRLF & _ " }," & @CRLF & _ " {" & @CRLF & _ " "id": "EMP002"," & @CRLF & _ " "firstName": "Charlie"," & @CRLF & _ " "lastName": "Davis"," & @CRLF & _ " "position": "DevOps Engineer"," & @CRLF & _ " "skills": ["AWS", "Docker", "Kubernetes"]" & @CRLF & _ " }" & @CRLF & _ " ]" & @CRLF & _ " }," & @CRLF & _ " {" & @CRLF & _ " "name": "Marketing"," & @CRLF & _ " "manager": {" & @CRLF & _ " "id": "MKT001"," & @CRLF & _ " "firstName": "David"," & @CRLF & _ " "lastName": "Miller"" & @CRLF & _ " }," & @CRLF & _ " "employees": [" & @CRLF & _ " {" & @CRLF & _ " "id": "EMP003"," & @CRLF & _ " "firstName": "Eve"," & @CRLF & _ " "lastName": "Taylor"," & @CRLF & _ " "position": "Marketing Specialist"," & @CRLF & _ " "skills": ["SEO", "Content Creation"]" & @CRLF & _ " }" & @CRLF & _ " ]" & @CRLF & _ " }" & @CRLF & _ " ]," & @CRLF & _ " "projects": [" & @CRLF & _ " {" & @CRLF & _ " "projectId": "PROJ101"," & @CRLF & _ " "projectName": "New Product Launch"," & @CRLF & _ " "status": "In Progress"," & @CRLF & _ " "teamMembers": ["EMP001", "EMP003"]" & @CRLF & _ " }" & @CRLF & _ " ]" & @CRLF & _ "}" & @CRLF & _ "" & @CRLF & _ "" & @CRLF & _ "2. GeoJSON Feature Collection (Geospatial Data)" & @CRLF & _ "This example uses the GeoJSON standard to represent a collection of geographic features." & @CRLF & _ "" & @CRLF & _ "{" & @CRLF & _ " "type": "FeatureCollection"," & @CRLF & _ " "features": [" & @CRLF & _ " {" & @CRLF & _ " "type": "Feature"," & @CRLF & _ " "properties": {" & @CRLF & _ " "name": "Eiffel Tower"," & @CRLF & _ " "country": "France"," & @CRLF & _ " "city": "Paris"" & @CRLF & _ " }," & @CRLF & _ " "geometry": {" & @CRLF & _ " "type": "Point"," & @CRLF & _ " "coordinates": [2.2945, 48.8584]" & @CRLF & _ " }" & @CRLF & _ " }," & @CRLF & _ " {" & @CRLF & _ " "type": "Feature"," & @CRLF & _ " "properties": {" & @CRLF & _ " "name": "Central Park"," & @CRLF & _ " "country": "USA"," & @CRLF & _ " "city": "New York"" & @CRLF & _ " }," & @CRLF & _ " "geometry": {" & @CRLF & _ " "type": "Polygon"," & @CRLF & _ " "coordinates": [" & @CRLF & _ " [" & @CRLF & _ " [-73.981, 40.768]," & @CRLF & _ " [-73.958, 40.796]," & @CRLF & _ " [-73.949, 40.793]," & @CRLF & _ " [-73.981, 40.768]" & @CRLF & _ " ]" & @CRLF & _ " ]" & @CRLF & _ " }" & @CRLF & _ " }" & @CRLF & _ " ]" & @CRLF & _ "}" & @CRLF & _ "" & @CRLF & _ "" & @CRLF & _ "3. Product Catalog with Variations (E-commerce)" & @CRLF & _ "This JSON structure represents a product catalog with various product details and variations." & @CRLF & _ "" & @CRLF & _ "{" & @CRLF & _ " "catalogId": "CAT001"," & @CRLF & _ " "products": [" & @CRLF & _ " {" & @CRLF & _ " "productId": "PROD001"," & @CRLF & _ " "name": "T-Shirt"," & @CRLF & _ " "description": "Comfortable cotton t-shirt."," & @CRLF & _ " "price": 19.99," & @CRLF & _ " "availableSizes": ["S", "M", "L", "XL"]," & @CRLF & _ " "colors": [" & @CRLF & _ " {" & @CRLF & _ " "name": "Red"," & @CRLF & _ " "hexCode": "#FF0000"," & @CRLF & _ " "stock": { "S": 10, "M": 15, "L": 8, "XL": 5 }" & @CRLF & _ " }," & @CRLF & _ " {" & @CRLF & _ " "name": "Blue"," & @CRLF & _ " "hexCode": "#0000FF"," & @CRLF & _ " "stock": { "S": 12, "M": 20, "L": 10, "XL": 7 }" & @CRLF & _ " }" & @CRLF & _ " ]," & @CRLF & _ " "reviews": [" & @CRLF & _ " {" & @CRLF & _ " "userId": "USER001"," & @CRLF & _ " "rating": 5," & @CRLF & _ " "comment": "Great quality and fit!"" & @CRLF & _ " }," & @CRLF & _ " {" & @CRLF & _ " "userId": "USER002"," & @CRLF & _ " "rating": 4," & @CRLF & _ " "comment": "Good t-shirt, a bit pricey."" & @CRLF & _ " }" & @CRLF & _ " ]" & @CRLF & _ " }," & @CRLF & _ " {" & @CRLF & _ " "productId": "PROD002"," & @CRLF & _ " "name": "Jeans"," & @CRLF & _ " "description": "Classic denim jeans."," & @CRLF & _ " "price": 49.99," & @CRLF & _ " "availableSizes": ["28", "30", "32", "34"]," & @CRLF & _ " "colors": [" & @CRLF & _ " {" & @CRLF & _ " "name": "Indigo"," & @CRLF & _ " "hexCode": "#4B0082"," & @CRLF & _ " "stock": { "28": 7, "30": 10, "32": 12, "34": 8 }" & @CRLF & _ " }" & @CRLF & _ " ]" & @CRLF & _ " }" & @CRLF & _ " ]" & @CRLF & _ "}" & @CRLF & _ "" & @CRLF & _ "" & @CRLF & _ "=========================" & @CRLF & _ "Invalid JSON :" & @CRLF & _ "=========================" & @CRLF & _ "" & @CRLF & _ "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." & @CRLF & _ "" & @CRLF & _ " -----------------" & @CRLF & _ "1. Missing Double Quotes around Keys:" & @CRLF & _ "Code" & @CRLF & _ "" & @CRLF & _ "{" & @CRLF & _ " name: "Alice"," & @CRLF & _ " "age": 30," & @CRLF & _ " "city": "New York"" & @CRLF & _ "}" & @CRLF & _ "" & @CRLF & _ "Invalidity: The key name is not enclosed in double quotes, which is a requirement in JSON. " & @CRLF & _ "" & @CRLF & _ " -----------------" & @CRLF & _ "2. Trailing Commas in Objects or Arrays:" & @CRLF & _ "Code" & @CRLF & _ "" & @CRLF & _ "{" & @CRLF & _ " "item1": "value1"," & @CRLF & _ " "item2": "value2"," & @CRLF & _ "}" & @CRLF & _ "" & @CRLF & _ " Invalidity: There is a trailing comma after the last key-value pair in the object. " & @CRLF & _ "" & @CRLF & _ "Code" & @CRLF & _ "" & @CRLF & _ "[" & @CRLF & _ " "element1"," & @CRLF & _ " "element2"," & @CRLF & _ "]" & @CRLF & _ "" & @CRLF & _ "Invalidity: There is a trailing comma after the last element in the array. " & @CRLF & _ "" & @CRLF & _ " -----------------" & @CRLF & _ "3. Incorrect Data Types or Values:" & @CRLF & _ "Code" & @CRLF & _ "" & @CRLF & _ "{" & @CRLF & _ " "product": "Laptop"," & @CRLF & _ " "price": $1200.00," & @CRLF & _ " "available": True" & @CRLF & _ "}" & @CRLF & _ "" & @CRLF & _ "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). " & @CRLF & _ "" & @CRLF & _ " -----------------" & @CRLF & _ "4. Mismatched Braces or Brackets:" & @CRLF & _ "Code" & @CRLF & _ "" & @CRLF & _ "{" & @CRLF & _ " "user": {" & @CRLF & _ " "id": 123," & @CRLF & _ " "name": "Bob"," & @CRLF & _ " "details": [" & @CRLF & _ " "email": "bob@example.com"" & @CRLF & _ " }" & @CRLF & _ " ]" & @CRLF & _ "}" & @CRLF & _ "" & @CRLF & _ "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. " & @CRLF & _ "" & @CRLF & _ " -----------------" & @CRLF & _ "5. Unescaped Characters within Strings:" & @CRLF & _ "Code" & @CRLF & _ "" & @CRLF & _ "{" & @CRLF & _ " "message": "He said, "Hello!""" & @CRLF & _ "}" & @CRLF & _ "" & @CRLF & _ " Invalidity: The double quotes within the string "Hello!" are not escaped, causing a syntax error. They should be \"Hello!\". " & @CRLF & _ "" & @CRLF & _ " -----------------" & @CRLF & _ "6. Comments within JSON:" & @CRLF & _ "Code" & @CRLF & _ "" & @CRLF & _ "{" & @CRLF & _ " // This is a comment" & @CRLF & _ " "data": "some value"" & @CRLF & _ "}" & @CRLF & _ "" & @CRLF & _ " Invalidity: JSON does not support comments. " & @CRLF & _ "" & @CRLF & _ " -----------------" & @CRLF & _ "7. Missing Colons or Values in Key-Value Pairs:" & @CRLF & _ "Code" & @CRLF & _ "" & @CRLF & _ "{" & @CRLF & _ " "key1": "value1"," & @CRLF & _ " "key2"" & @CRLF & _ " "key3": "value3"" & @CRLF & _ "}" & @CRLF & _ "" & @CRLF & _ " Invalidity: The key2 entry is missing a colon and a value. " & @CRLF & _ "" & @CRLF & _ " -----------------" & @CRLF & _ "8. Invalid JSON Structure (e.g., array with object syntax):" & @CRLF & _ "Code" & @CRLF & _ "" & @CRLF & _ "[" & @CRLF & _ " "item1": "value1"," & @CRLF & _ " "item2": "value2"" & @CRLF & _ "]" & @CRLF & _ "" & @CRLF & _ "Invalidity: An array cannot contain key-value pairs; it should contain elements directly. " & @CRLF & _ "" & @CRLF & _ "" & @CRLF & _ "{" & @CRLF & _ " "key1" : "value1"," & @CRLF & _ " "key2" "lk"" & @CRLF & _ "}" & @CRLF & _ " -----------------" & @CRLF & _ "" & @CRLF & _ "{" & @CRLF & _ " "key1" : "value1"" & @CRLF & _ " "key2" "lk"" & @CRLF & _ "}" & @CRLF & _ " -----------------" & @CRLF & _ "" & @CRLF & _ "{" & @CRLF & _ " "key1" : "value1"," & @CRLF & _ " "key2" "lk"" & @CRLF & _ "}" & @CRLF & _ " -----------------" & @CRLF & _ "" & @CRLF & _ "{" & @CRLF & _ " "key1" : "value1"," & @CRLF & _ " "key2" : mj" & @CRLF & _ "}" & @CRLF & _ " -----------------" & @CRLF & _ "" & @CRLF & _ "{" & @CRLF & _ " "key1" : "value1"," & @CRLF & _ " "key2" : "mj" & @CRLF & _ "}" & @CRLF & _ " -----------------" & @CRLF & _ "" & @CRLF & _ "{" & @CRLF & _ " "key1" : "value1"," & @CRLF & _ " "hhh" : [ " & @CRLF & _ " { " & @CRLF & _ " "key0" : [{"MMM" : "a"}, "b","c"]," & @CRLF & _ " "key2" : "mj"," & @CRLF & _ " "key3" :" & @CRLF & _ " [ " & @CRLF & _ " -----------------" & @CRLF & _ "" & @CRLF & _ "[" & @CRLF & _ " "mn" " & @CRLF & _ " {"MMM" : a"} ," & @CRLF & _ " "b" , "c"" & @CRLF & _ "]" & @CRLF & _ "" & @CRLF & _ "" Local $sSubst = "$1 ERROR_here>> " Local $sResult = StringRegExpReplace($sString, $sRegex, $sSubst) MsgBox($MB_SYSTEMMODAL, "Result", $sResult)

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