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

/
/
g

Test String

Code Generator

Generated Code

import java.util.regex.Matcher; import java.util.regex.Pattern; public class Example { public static void main(String[] args) { final String regex = "(\\/)(\\*)(.|\\n)*?\\2\\1|(\\/\\/.*)"; final String string = "#include<stdio.h>\n" + "#include<stdlib.h>\n" + "typedef struct Node\n" + "{\n" + " int data;\n" + " struct Node *next;\n" + " struct Node *prev;\n" + "}node;\n" + "void insert(node *pointer, int data)\n" + "{\n" + " /* Iterate through the list till we encounter the last node.*/\n" + " while(pointer->next!=NULL)\n" + " {\n" + " pointer = pointer -> next;\n" + " }\n" + " /* Allocate memory for the new node and put data in it.*/\n" + " pointer->next = (node *)malloc(sizeof(node));\n" + " (pointer->next)->prev = pointer;\n" + " pointer = pointer->next;\n" + " pointer->data = data;\n" + " pointer->next = NULL;\n" + "}\n" + "int find(node *pointer, int key)\n" + "{\n" + " pointer = pointer -> next; //First node is dummy node.\n" + " /* Iterate through the entire linked list and search for the key. */\n" + " while(pointer!=NULL)\n" + " {\n" + " if(pointer->data == key) //key is found.\n" + " {\n" + " return 1;\n" + " }\n" + " pointer = pointer -> next;//Search in the next node.\n" + " }\n" + " /*Key is not found */\n" + " return 0;\n" + "}\n" + "void delete(node *pointer, int data)\n" + "{\n" + " /* Go to the node for which the node next to it has to be deleted */\n" + " while(pointer->next!=NULL && (pointer->next)->data != data)\n" + " {\n" + " pointer = pointer -> next;\n" + " }\n" + " if(pointer->next==NULL)\n" + " {\n" + " printf(\"Element %d is not present in the list\\n\",data);\n" + " return;\n" + " }\n" + " /* Now pointer points to a node and the node next to it has to be removed */\n" + " node *temp;\n" + " temp = pointer -> next;\n" + " /*temp points to the node which has to be removed*/\n" + " pointer->next = temp->next;\n" + " temp->prev = pointer;\n" + " /*We removed the node which is next to the pointer (which is also temp) */\n" + " free(temp);\n" + " /* Beacuse we deleted the node, we no longer require the memory used for it .\n" + " free() will deallocate the memory.\n" + " */\n" + " return;\n" + "}\n" + "void print(node *pointer)\n" + "{\n" + " if(pointer==NULL)\n" + " {\n" + " return;\n" + " }\n" + " printf(\"%d \",pointer->data);\n" + " print(pointer->next);\n" + "}\n" + "int main()\n" + "{\n" + " /* start always points to the first node of the linked list.\n" + " temp is used to point to the last node of the linked list.*/\n" + " node *start,*temp;\n" + " start = (node *)malloc(sizeof(node));\n" + " temp = start;\n" + " temp -> next = NULL;\n" + " temp -> prev = NULL;\n" + " /* Here in this code, we take the first node as a dummy node.\n" + " The first node does not contain data, but it used because to avoid handling special cases\n" + " in insert and delete functions.\n" + " */\n" + " printf(\"1. Insert\\n\");\n" + " printf(\"2. Delete\\n\");\n" + " printf(\"3. Print\\n\");\n" + " printf(\"4. Find\\n\");\n" + " while(1)\n" + " {\n" + " int query;\n" + " scanf(\"%d\",&query);\n" + " if(query==1)\n" + " {\n" + " int data;\n" + " scanf(\"%d\",&data);\n" + " insert(start,data);\n" + " }\n" + " else if(query==2)\n" + " {\n" + " int data;\n" + " scanf(\"%d\",&data);\n" + " delete(start,data);\n" + " }\n" + " else if(query==3)\n" + " {\n" + " printf(\"The list is \");\n" + " print(start->next);\n" + " printf(\"\\n\");\n" + " }\n" + " else if(query==4)\n" + " {\n" + " int data;\n" + " scanf(\"%d\",&data);\n" + " int status = find(start,data);\n" + " if(status)\n" + " {\n" + " printf(\"Element Found\\n\");\n" + " }\n" + " else\n" + " {\n" + " printf(\"Element Not Found\\n\");\n\n" + " }\n" + " }\n" + " }\n\n\n" + "}"; final Pattern pattern = Pattern.compile(regex); final Matcher matcher = pattern.matcher(string); while (matcher.find()) { System.out.println("Full match: " + matcher.group(0)); for (int i = 1; i <= matcher.groupCount(); i++) { System.out.println("Group " + i + ": " + matcher.group(i)); } } } }

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 Java, please visit: https://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html