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

/
/
g

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 = "(?############ Let's catch paths without "" or '' ############################" & @CRLF & _ ")(?<opening>(?# First, catch the starting path, the <opening> ###################" & @CRLF & _ " )\b(?<montage>[a-zA-Z]:[\/\\])(?# montage = 'C:/'" & @CRLF & _ " )|[\/\\][\/\\](?<!http:\/\/)(?<!https:\/\/)(?>(?# check not 'http[s]:' prefix" & @CRLF & _ " )[?.][\/\\](?:[^\/\\<>:"|?\n\r ]+[\/\\])?(?# '//[?or.]/xxxxx' or '//[?or.]/server/'" & @CRLF & _ " )(?&montage)?(?# '//[?or.]/c:/' or '//[?or.]/server/c:/'" & @CRLF & _ " )|(?!(?&montage)))(?# '//[addressIP/ or serverName/ but not C:/]'" & @CRLF & _ " )|%\w+%[\/\\]?(?# '%EnvVariable%[/]'" & @CRLF & _ "))(?# So, <opening> catch : " & @CRLF & _ " 'C:/' or" & @CRLF & _ " '//[?or.]/[UNC/]C:/' or" & @CRLF & _ " '//[?or.]/[UNC/]' or" & @CRLF & _ " '//[next characters must be something other than C:/]' or" & @CRLF & _ " '%EnvironementVariable%[/]'" & @CRLF & _ ")(?:(?# now, we catch each directory name wich is between [/] ########################" & @CRLF & _ ")[^\/\\<>:"|?\n\r ,'](?# the first character should not be [ ,']" & @CRLF & _ ")[^\/\\<>:"|?\n\r]*(?# Any pathFrendly character" & @CRLF & _ ")(?<![ ,'])(?# The last directory name's character must not be [ ,']" & @CRLF & _ ")[\/\\](?# End of directory name - who are between '/' -" & @CRLF & _ "))*(?# Catch most 'directoryName/' as possible" & @CRLF & _ ")(?:(?# Lets catch the End path. There is a file ? a directory ? or just a useless '/' ?" & @CRLF & _ ")(?=[^\/\\<>:"'|?\n\r;, ])(?#if next character is not pathFriendly or ' ' or [,'], we have reach the end of the path => we don't catch the last '/' and the the Regex end now." & @CRLF & _ "You can't catch fileName who begin by [,'] because they are probably a delimiter between 2 path. but '.' is allowed" & @CRLF & _ ")(?:(?#If we are here, that mean there is a fileName or directoryName to catch" & @CRLF & _ "###### We will catch the last directoryName or the fileName without the extention ######" & @CRLF & _ " )(?:[^\/\\<>:"|?\n\r;, .](?# catch any character pathFriendly exept ' ' or [,.]" & @CRLF & _ " )(?: (?=[\w\-]))?(?# If we find a ' ', we catch him if next charcter is not a delimiter. I see '-' after an ' ' not like a delimiter." & @CRLF & _ " )(?:\*(?!= ))?(?# If we find a '*', we stop the catch if next character is an ' '" & @CRLF & _ " )(?!(?&montage))(?# If we find a string who look like 'C:/', we stop the catch" & @CRLF & _ "))+(?# We catch theses word delimited by ' ' as much as possible" & @CRLF & _ "))?(?# it's possible the fileName have no name, but just an extention" & @CRLF & _ ")(?:\.\w+(?# #### an extention begin by '.' and at least one none delimiter chracter" & @CRLF & _ "))*(?# we can add more extention until the first none '.' delimiter character. So, after the first '.' character inside a fileName, we cannot catch any ' ' character" & @CRLF & _ "If we don't find one extention, so the filename is a directory name, and we stop the catch." & @CRLF & _ "))(?# ############# END OF PATH CATCHING WITHOUT QUOTE "" and '' #######################" & @CRLF & _ ")|(?:(?# ######### Catching path quoted '' ###########################" & @CRLF & _ "Path quoted '' is difficult because ['] is also a pathFrendly character" & @CRLF & _ ")'(?&opening)(?# We catch .* between quote only if string start with an <opening>" & @CRLF & _ ")(?=.*'\W|.*'$)(?# We catch .* between quote only if we are sure we will find end quote. End quote must be ['] and delimiter character or ['] and end string" & @CRLF & _ ")(?:[^\/\\<>:'"|?\n\r]+(?# We take any pathFriendly character exept quote [']" & @CRLF & _ ")(?:'(?=\w))?(?# we catch quote ['] if next character is not a delimiter" & @CRLF & _ ")[\/\\]?)*(?# Path quoted must respect this patern until end quote character [']" & @CRLF & _ ")')(?# end quoted '' path" & @CRLF & _ ")|(?# ######### Catching path quoted "" ###########################" & @CRLF & _ ")"(?&opening)(?# We catch .* between quote only if string start with an <opening>" & @CRLF & _ ")(?=.*")(?# We catch .* between quote only if we are sure we will find end quote ["]" & @CRLF & _ ")(?:[^\/\\<>:"|?\n\r]+(?# We take any pathFriendly character" & @CRLF & _ ")[\/\\]?(?# pathFriendly characters can be is delimited by '\'" & @CRLF & _ "))*(?# Path quoted must respect this patern until end quote character" & @CRLF & _ ")"(?# end quoted path" & @CRLF & _ ")" Local $sString = "THIS IS COMMENTED VERSION !" & @CRLF & _ "to simple copy and use it, go https://regex101.com/r/zWGLMP" & @CRLF & _ "" & @CRLF & _ "C:/testOk\dot.Dirname/.nameFileBeginByDot first space after a dot in file name stop the match" & @CRLF & _ "C:/testOk\_.._AsDirName/../file name.ext1.ext2 first space after a dot stop the match" & @CRLF & _ "start text don't match C:/testOk\lastDir Or FileName WithDouble..dot stop the match" & @CRLF & _ "C:/testOk\lastDir Or FileName dot ended. stop the match like an end sentence. So, a last name with a space after a dot is not catch" & @CRLF & _ "C:/testOk\LastNameIs/DirName C:/testOk\2Paths_ _separated/f.ext space after extention stop match" & @CRLF & _ "C:/testOK\Last_/_isNotmatched/fgfj.gjjb/uhloext/ and [ ,'] after '\' stop match" & @CRLF & _ "\\127.0.0.1/this', 'isOkInMidDirName\butSimple',' stop match in last DirName or FileName" & @CRLF & _ "\\.\c:/this exotic path begining work\and\ space after '\' stop the match" & @CRLF & _ "\\?\c:/this exotic path begining work too\and \space before '\' stop the match too" & @CRLF & _ "\\testOk/this' - 'is ok in dirName/and - in lastName.ext" & @CRLF & _ "i:/dir/fileName with a .space before dot stop the match" & @CRLF & _ "\\?\server1\e:\utilities\\filecomparer\ this double \\ is interpretated as new path" & @CRLF & _ "" & @CRLF & _ "@"c:\testOk\double quote character is more permissive/ '' , ; .txt, .ext2"," & @CRLF & _ "@"\\127.0.0.1\c$\temp\t'est-file.txt, if end double quote is missing, we use unquote match" & @CRLF & _ "@"\c:\LOCALHOST\c$\ thisIsNotMatched" "temp\test-file.txt", quoted path must have a right opening to be matched" & @CRLF & _ "@'\\.\c:\temp\te'st-file.txt' simple quoted is ok " & @CRLF & _ "'c:\simpleQuoteInsideStill'Match\but' stopMatch if next is space character," & @CRLF & _ "'c:\simpleQuoteInsideStill'Match\but\'stopMatch if is fisrt character after \" & @CRLF & _ "'c:\simpleQuoteInsideStill'Match\but''stopMatch if he is double" & @CRLF & _ "@"\\?\c:\te ' mp\est-file.txt"," & @CRLF & _ "@"\\.\UNC\LOCALHOST\c$\temp\test-file.txt"," & @CRLF & _ "@"\\127.0.0.1\c$\temp\test-file.txt"" & @CRLF & _ "" & @CRLF & _ "/\serverName\mix/and\still match" double quote character stop match" & @CRLF & _ "\\\IfMoreThan2_\_we take only the 2 lasts.ext first space after ext stop the match" & @CRLF & _ "/testNotMatch/html" & @CRLF & _ "/testNotMatch.html" & @CRLF & _ "testNotMatch.html" & @CRLF & _ "// -> this simple // or \\ is not matched, but this //isMatched !" & @CRLF & _ "/ -> this simple / is not matched, and this /notMatchedToo" & @CRLF & _ "b-renice\sauvegardes\B-HIER\GEO\Geo_NetAct_Atoll_Planet\UR_Est\Custom Data"" & @CRLF & _ ""b-renice\sauvegardes\B-HIER\GEO\Geo_NetAct_Atoll_Planet\UR_Est\Custom Data"" & @CRLF & _ ""\\b-renice\sauvegardes\B-HIER\GEO\Geo_NetAct_Atoll_Planet\UR_Est\Custom Data"" & @CRLF & _ "" & @CRLF & _ "error Message test:" & @CRLF & _ "---------------------------" & @CRLF & _ "Tentative d'accès à C:\Users\tpgz4017\App - Data\Local\Temp\tempShapeFile_CrossWave Calibration Zones - Atoll CrossWave Model.shx après sa fin." & @CRLF & _ "---------------------------" & @CRLF & _ "" & @CRLF & _ "local url path :" & @CRLF & _ "file://C:/Users/Downloads/20220516_32289275_1049383.pdf" & @CRLF & _ "urlPath :" & @CRLF & _ "file://p-eco2.rd.fr/vol_H0037_01$/599/livraison/20220516_32289275_1049383.pdf" & @CRLF & _ "" & @CRLF & _ "c:\temp\test-file.txt"," & @CRLF & _ "\\127.0.0.1\c$\temp\test-file.txt"," & @CRLF & _ "\\LOCALHOST\c$\ temp\test-file.txt"," & @CRLF & _ "\\LOCALHOST\c$ \temp\test-file.txt"," & @CRLF & _ "\\.\c:\temp\t\est-file.txt"," & @CRLF & _ "\\?\c:\temp\test-file.txt"," & @CRLF & _ "\\.\UNC\LOCALHOST\c$\temp\test-file.txt"," & @CRLF & _ "\\?\UNC\ServerName\ temp\test-file.txt"," & @CRLF & _ "\\127.0.0.1\c$\temp\test -file.txt"" & @CRLF & _ "" & @CRLF & _ "" & @CRLF & _ "error Message test:" & @CRLF & _ "Site0 / 3: - Warning . See log file 'C:\ProgramData\InfoVista\Planet 7.4\7.4\RPE\Log\Plugins\Universal_Model_masked\log_Universal_Model.txt' for details" & @CRLF & _ "" & @CRLF & _ "C:/test\gvk.hv/fgfj.gjjb/uhloext : some random text" & @CRLF & _ "" & @CRLF & _ "\\b-renice\sauvegardes\B-HIER\GEO\Geo_NetAct_Atoll_Planet\UR_Est\Polygon\Haguenau\Building\Haguenau hgtfhyt "C:/te-st.html" "C:/te-st.html" gd"dhbcsk "C:/te/dsst.ikpo fdsf "C:\test" "C:// test.html" gd" & @CRLF & _ ""//te s t/e, llo.html " & @CRLF & _ "C:/test\f/uhlo/. " & @CRLF & _ "C://te?st.html" & @CRLF & _ "b-renice\sauvegardes\B-HIER\GEO\Geo_NetAct_Atoll_Planet\UR_Est\Custom Data"" & @CRLF & _ "; dfsdf "\\b-renice\sauvegardes\B-HIER\GEO\Geo_NetAct_Atoll_Planet\UR_Est\Custom Data"" & @CRLF & _ "; dfsdf "\\" & @CRLF & _ ""\\b-renice\sauvegardes\B-HIER\GEO\Geo_NetAct_Atoll_Planet\UR_Est\Custom Data"Haguenau_Building.tab : Data format of \\b-renice\sauvegardes\B-HIER\GEO\Geo_NetAct_Atoll_Planet\UR_Est\Polygon\Haguenau\Building\Haguenau Building.* C: is invalid" & @CRLF & _ "Haguenau_Building.tab : Data format of \\b-renice\sauvegardes\B-HIER\GEO\Geo NetAct Atoll_Planet\UR_Est\Polygon\Haguenau\Building\Haguenau Building.TAB, is invalid" & @CRLF & _ "Haguenau_Building.tab : Data format of \\b-renice\sauvegardes\B-HIER\GEO\Geo_NetAct_Atoll_Planet\UR_Est\Polygon\Haguenau\Building\Haguenau Buildi*.*ng.*, is invalid" & @CRLF & _ "C:/test/../hjgbkl C:/test/../hjgbkl.gfgdfgrdgfdgr C:/test/../hjgbkl" & @CRLF & _ "C:/test.html" & @CRLF & _ "C://test/ .h/hel,lo.html//test/./hello.html" & @CRLF & _ "C:/test//hello.html" & @CRLF & _ "//test" & @CRLF & _ "//hello.html" & @CRLF & _ "/test" & @CRLF & _ ""%tmp%/fsdfs"" & @CRLF & _ "%tmp%/fsdfs" & @CRLF & _ "ERROR 8/31/2021 - 6:45:39 PM HighResClutter .RasterFile : \\b-ren ice\sauv egardes\B-HIER\GEO%dsq%\NewJersey_NewYork\DTM\DTM\CENTRAL_JERSE..Y_New_York_2 m_Z18N_0_DTM_02_06.bil : Le fichier spécifié est introuvable. " & @CRLF & _ "\\b-ren ice\sauv egardes\..\B-HIER\GEO\NewJersey_NewYork\DTM\DTM\CENTRAL_JERSE..Y_New_York_2 m_Z18N_0_DTM_02_06.bil C:\b-ren ice\sauv egardes\B-HIER\GEO\NewJersey_NewYork\DTM\DTM\CENTRAL_JERSE..Y_New_York_2 m_Z18N_0_DTM_02_06.bil \\b-ren ice\sauv egardes\B-HIER\GEO\NewJersey_NewYork\DTM\DTM\CENTRAL_JER SE.Y_New_York_2 m_Z18N_0_DTM_02_06.bil. " & @CRLF & _ "//test.html" & @CRLF & _ "\\10.1.1.107" & @CRLF & _ "//10.1.1.107/test.html" & @CRLF & _ "//10.1.1.107/te st/hello.html" & @CRLF & _ "//10.1.1.107/test/hello" & @CRLF & _ "//test/hello.txt" & @CRLF & _ "//test/hello.txt." & @CRLF & _ "" & @CRLF & _ "\\.\UNC\Server\Share\Test\Foo.txt " & @CRLF & _ "\\?\UNC\Server\Share\Test\Foo.txt" & @CRLF & _ "" & @CRLF & _ "Pour les chemins UNC de périphérique, la partie serveur/partage forme le volume. Par exemple, dans \\?\server1\e:\utilities\filecomparer\ , la partie serveur/partage est server1\utilities . Ceci est important quand" & @CRLF & _ "'\\127.0.0.1\c$\temp\test-fi'le.txt'" 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