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
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
  • Match everything enclosed
    (?:...)
  • Capture everything enclosed
    (...)
  • 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

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