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

/
/
mg

Test String

Code Generator

Generated Code

# coding=utf8 # the above tag defines encoding for this document and is for Python 2.x compatibility import re regex = r"\d+(.*)" test_str = ("/*\n" " * To change this license header, choose License Headers in Project Properties.\n" " * To change this template file, choose Tools | Templates\n" " * and open the template in the editor.\n" " */\n\n" "6 #include <stdio.h>\n" "7 #include <sys/types.h>\n" "8 #include <string.h>\n" "9 #include <errno.h>\n" "10 #include <sys/stat.h>\n" "11 #include <fcntl.h>\n" "12 #include <sys/mman.h>\n" "13 #include <semaphore.h>\n" "14 #include <sys/signal.h>\n" "15 #include <signal.h>\n" "16 #include <dirent.h>\n" "17 #include <sys/stat.h>\n" "18 #include <sys/wait.h>\n" "19 #include <pthread.h>\n" "20\n" "21 #define BUFSIZE 10\n" "22 #define TCOUNT 10\n" "23 #define ITEMS 10000\n" "24\n" "25\n" "26 struct item {\n" "27 int value; // produced value\n" "28 int source; // index of producer\n" "29 };\n" "30\n" "31 struct buffer {\n" "32 int count;\n" "33 int in;\n" "34 int out;\n" "35 struct item data[BUFSIZE];\n" "36 pthread_mutex_t mutex;\n" "37 pthread_cond_t xp; // producer will wait for a slot using this\n" "38 pthread_cond_t xc; // consumer fill wait for an item using this\n" "39 };\n" "40\n" "41\n" "42 struct buffer *buf; // shared bounded buffer\n" "43\n" "44 static void *\n" "45 producer_thread(void *arg)\n" "46 {\n" "47\n" "48 int index; // thread index\n" "49 int i = 0;\n" "50\n" "51 index = (int) arg;\n" "52\n" "53 for (i = 0; i < ITEMS; ++i) {\n" "54\n" "55 // value of i is the item produced\n" "56\n" "57 pthread_mutex_lock(&buf->mutex);\n" "58 while (buf->count == BUFSIZE)\n" "59 pthread_cond_wait(&buf->xp, &buf->mutex);\n" "60 // sleeps in this loop as long as buffer is full\n" "61\n" "7.1. POSIX NAMED SEMAPHORES 117\n" "62\n" "63 // put the item into buffer\n" "64 buf->data[buf->in].value = i;\n" "65 buf->data[buf->in].source = index;\n" "66 buf->in = (buf->in + 1) % BUFSIZE;\n" "67 buf->count++;\n" "68\n" "69 if (buf->count == 1)\n" "70 pthread_cond_signal(&buf->xc);\n" "71\n" "72 pthread_mutex_unlock(&buf->mutex);\n" "73\n" "74 }\n" "75\n" "76 pthread_exit (NULL);\n" "77 }\n" "78\n" "79\n" "80\n" "81 static void *\n" "82 consumer_thread(void *arg)\n" "83 {\n" "84 int n = 0;\n" "85 int x;\n" "86 int s;\n" "87\n" "88 while (1) {\n" "89 pthread_mutex_lock(&buf->mutex);\n" "90\n" "91 while (buf->count == 0)\n" "92 pthread_cond_wait(&buf->xc, &buf->mutex);\n" "93 // sleeps in this loop as long as buffer is empty\n" "94\n" "95\n" "96\n" "97 // retrive an item from buffer\n" "98 x = buf->data[buf->out].value;\n" "99 s = buf->data[buf->out].source;\n" "100 buf->out = (buf->out + 1) % BUFSIZE;\n" "101 buf->count--;\n" "102\n" "103 if (buf->count == (BUFSIZE - 1)) {\n" "104 // wake up all possible workers;\n" "105 // otherwise some workers will always sleep\n" "106 pthread_cond_broadcast (&buf->xp);\n" "107 }\n" "108\n" "109 pthread_mutex_unlock(&buf->mutex);\n" "110\n" "111 n++;\n" "112\n" "113 if (n == (TCOUNT * ITEMS))\n" "114 break;\n" "115 }\n" "116\n" "117 printf (\"retrieved %d items\\n\", n);\n" "118 CHAPTER 7. SYNCHRONIZATION\n" "118\n" "119 printf (\"consumer finished successfully\\n\");\n" "120\n" "121 pthread_exit (NULL);\n" "122 }\n" "123\n" "124\n" "125 int\n" "126 main(int argc, char **argv)\n" "127 {\n" "128\n" "129 pthread_t pids[TCOUNT]; // producer tids\n" "130 pthread_t ctid; // consumer tid\n" "131 int i;\n" "132 int ret;\n" "133\n" "134\n" "135 buf = (struct buffer *) malloc(sizeof (struct buffer));\n" "136 buf->count = 0;\n" "137 buf->in = 0;\n" "138 buf->out = 0;\n" "139 pthread_mutex_init(&buf->mutex, NULL);\n" "140 pthread_cond_init(&buf->xp, NULL);\n" "141 pthread_cond_init(&buf->xc, NULL);\n" "142\n" "143\n" "144 // create producer threads\n" "145 for (i = 0; i < TCOUNT; ++i) {\n" "146 ret = pthread_create(&pids[i], NULL, producer_thread,\n" "147 (void *) i);\n" "148 if (ret < 0) {\n" "149 perror(\"thread create failed\\n\");\n" "150 exit(1);\n" "151 }\n" "152 }\n" "153\n" "154\n" "155 // create consumer thread\n" "156 ret = pthread_create(&ctid, NULL, consumer_thread, (void *) NULL);\n" "157 if (ret < 0) {\n" "158 perror(\"thread create failed\\n\");\n" "159 exit(1);\n" "160 }\n" "161\n" "162\n" "163 // wait for the producer threads to terminate\n" "164 for (i = 0; i < TCOUNT; ++i)\n" "165 pthread_join(pids[i], NULL);\n" "166\n" "167 // wait for the consumer thread to terminate\n" "168 pthread_join(ctid, NULL);\n" "169\n" "170 free(buf);\n" "171\n" "172 pthread_mutex_destroy(&buf->mutex);\n" "173 pthread_cond_destroy(&buf->xp);\n" "7.2. PETERSON’S SOLUTION TO CRITICAL REGION PROBLEM 119\n" "174 pthread_cond_destroy(&buf->xc);\n" "175\n" "176 printf(\"program ending...bye..\\n\");\n" "177 exit (0);\n" "178 }") matches = re.finditer(regex, test_str, re.MULTILINE) for matchNum, match in enumerate(matches, start=1): print ("Match {matchNum} was found at {start}-{end}: {match}".format(matchNum = matchNum, start = match.start(), end = match.end(), match = match.group())) for groupNum in range(0, len(match.groups())): groupNum = groupNum + 1 print ("Group {groupNum} found at {start}-{end}: {group}".format(groupNum = groupNum, start = match.start(groupNum), end = match.end(groupNum), group = match.group(groupNum))) # Note: for Python 2.7 compatibility, use ur"" to prefix the regex and u"" to prefix the test string and substitution.

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 Python, please visit: https://docs.python.org/3/library/re.html