Regular Expressions 101

Save & Share

  • Regex Version: ver. 3
  • Update Regex
    ctrl+⇧+s
  • Save new Regex
    ctrl+s
  • Add to Community Library

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

/
/
gm

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"(?<!\w)\b[A-Z]+[\_]*[A-Z]*\b(?=\s)" test_str = ("; v1.1.31.01'de tüm desktoplarda çalışır\n\n" "#NoEnv ; Uyumlukuk için A_ ön eki ile ortam değişkenlerini kullanın\n" "#SingleInstance Force ; Sadece 1 kez açalıştırabilire\n" " \n" "#KeyHistory 0 ; Tuş basımları loglamayı engeller\n\n" "SetBatchLines, -1 ; Scripti sürekli olarak çalıştırma (nromalde her saniye 10ms uyur)\n" "ListLines, Off ; Derlenen verileri loglamaz\n\n" "#Warn ; Enable warnings to assist with detecting common errors.\n" "SendMode Input ; Recommended for new scripts due to its superior speed and reliability.\n" "SetWorkingDir %A_ScriptDir% ; Ensures a consistent starting directory.\n\n" "#MaxThreadsPerHotkey, 1 ; Yanlışlıkla 2 kere buton algılanmasını engeller\n\n" "VERSION = 1.2.0\n\n" "; Gizlenmiş pencelerin ID'si\n" "HidedWindows := []\n\n" "; TIP: Veri dizini yolu\n" "DIR_NAME = %A_AppData%\\YHotkeys\n\n" "InstallIcons()\n" "CreateOrUpdateTrayMenu()\n" "return\n\n" "IconClicked:\n" " ToggleMemWindowWithTitle(A_ThisMenuItem)\n" "Return\n\n" "ClearAll:\n" " ClearAllHidedWindows()\n" "Return\n\n" "CloseApp:\n" " ClearAllHidedWindows()\n" " ExitApp\n" "Return\n\n" "class MenuObject {\n" " ahkID := 0\n" " title := \"\"\n" " iconPath := \"\"\n" "}\n\n" "InstallIcons() {\n" " global DIR_NAME\n" " FileCreateDir, %DIR_NAME%\n" " FileInstall, .\\res\\seedling.ico, %DIR_NAME%\\seedling.ico, 1\n" " FileInstall, .\\res\\default.ico, %DIR_NAME%\\default.ico, 1\n" " FileInstall, .\\res\\clear.ico, %DIR_NAME%\\clear.ico, 1\n" " FileInstall, .\\res\\close.ico, %DIR_NAME%\\close.ico, 1\n" "}\n\n" "ClearAllHidedWindows() {\n" " ahkIDs := GetHidedWindowsIDs()\n" " For index, ahkID in ahkIDs {\n" " ToggleWindowWithID(ahkID, True)\n" " WinKill, ahk_id %ahkID%\n" " }\n" "}\n\n" "GetHidedWindowsIDs(){\n" " ahkIDs := []\n" " global HidedWindows\n" " For index, item in HidedWindows {\n" " ahkIDs.Push(item.ahkID)\n" " }\n" "return ahkIDs\n" "}\n\n" "GetHidedWindowsIDWithTitle(title){\n" " global HidedWindows\n" " For index, item in HidedWindows {\n" " if (item.title == title) {\n" " return item.ahkID\n" " }\n" " }\n" "return 0\n" "}\n\n" "GetHidedWindowsIndexWithID(ahkID){\n" " global HidedWindows\n" " For index, item in HidedWindows {\n" " if (item.ahkID == ahkID) {\n" " return index\n" " }\n" " }\n" "return 0\n" "}\n\n" "ToggleMemWindowWithTitle(menuName) {\n" " ahkID := GetHidedWindowsIDWithTitle(menuName)\n" " if ahkID\n" " ToggleWindowWithID(ahkID, True)\n" " else\n" " Run https://github.com/yedhrab/YHotkeys\n" "}\n\n" "RunUrl(url) {\n" " ; WARN: Bazı uygulamarın geç açılması soruna sebep oluyor\n" " ; BUG: Uygulamalar bazen 2 kere açılıyor\n" " try {\n" " SetTitleMatchMode, Slow\n" " \n" " RunWait, %url%\n" " }\n" "}\n\n" "ActivateWindowWithID(ahkID, wait=True) {\n" " WinActivate, ahk_id %ahkID%\n" " if wait\n" " WinWaitActive, ahk_id %ahkID%\n" "}\n\n" "ShowHidedWindowWithID(ahkId)\n" "{\n" " WinRestore, ahk_id %ahkID%\n" " WinShow, ahk_id %ahkID%\n" "}\n\n" "DropFromMem(ahkID){\n" " index := GetHidedWindowsIndexWithID(ahkID)\n" " if index {\n" " global HidedWindows\n" " HidedWindows.RemoveAt(index)\n" " }\n" "return index\n" "}\n\n" "DropActiveWindowFromTrayMenu(){\n" " WinGetTitle, title, A\n" " Menu, Tray, Delete, %title%\n" " global HidedWindows\n" " if !HidedWindows.Length()\n" " Menu, Tray, Delete, Temizle\n" "}\n\n" "DropActiveWindowFromMem(){\n" " WinGet, ahkID, ID, A\n" " \n" "return DropFromMem(ahkID)\n" "}\n\n" "KeepInMem(ahkID, title, iconPath) {\n" " item := new MenuObject\n" " \n" " item.ahkID := ahkID\n" " item.title := title\n" " item.iconPath := iconPath\n" " \n" " global HidedWindows\n" " HidedWindows.Push(item)\n" "}\n\n" "; Gizlenmeden önce kullanılmazsa id alamaz\n" "KeepActiveWindowInMem() {\n" " WinGetActiveTitle, title\n" " WinGet, ahkID, ID, A\n" " WinGet, iconPath, ProcessPath, A\n" " \n" " KeepInMem(ahkID, title, iconPath)\n" "}\n\n" "AddTrayMenuIcon(title, iconPath, default=True) {\n" " if FileExist(iconPath) {\n" " Menu, Tray, Icon, %title%, %iconPath%,, 20\n" " } else if default {\n" " global DIR_NAME\n" " iconPath := DIR_NAME . \"\\default.ico\"\n" " AddTrayMenuIcon(title, iconPath, False)\n" " }\n" "}\n\n" "CreateOrUpdateTrayMenu(){\n" " #Persistent\n" " Menu, Tray, UseErrorLevel , On\n" " Menu, Tray, NoStandard\n" " Menu, Tray, Add, YHotkeys, IconClicked\n" " \n" " global VERSION\n" " Menu, Tray, Tip, YHotkeys v%VERSION% ~ YEmreAk\n" " Menu, Tray, Click, 1\n" " \n" " \n" " global DIR_NAME\n" " iconPath := DIR_NAME . \"\\seedling.ico\"\n" " if FileExist(iconPath) {\n" " Menu, Tray, Icon, %iconPath%,, 20\n" " iconPath := DIR_NAME . \"\\seedling.ico\"\n" " AddTrayMenuIcon(\"YHotkeys\", iconPath)\n" " }\n" " \n" " global HidedWindows\n" " if (HidedWindows.Length() > 0) {\n" " Menu, Tray, Add, Temizle, ClearAll\n" " Menu, Tray, Delete, Temizle\n" " Menu, Tray, Delete, Kapat\n" " \n" " iconPath := HidedWindows[HidedWindows.Length()].iconPath\n" " mainTitle := HidedWindows[HidedWindows.Length()].title\n" " \n" " For index, item in HidedWindows {\n" " title := item.title\n" " iconPath := item.iconPath\n" " \n" " Menu, Tray, Add, %title%, IconClicked\n" " AddTrayMenuIcon(title, iconPath)\n" " }\n" " \n" " Menu, Tray, Add, Temizle, ClearAll\n" " \n" " iconPath := DIR_NAME . \"\\clear.ico\"\n" " AddTrayMenuIcon(\"Temizle\", iconPath)\n" " \n" " } else {\n" " mainTitle := \"YHotkeys\"\n" " }\n" " \n" " Menu, Tray, Default, YHotkeys\n" " Menu, Tray, Add, Kapat, CloseApp\n" " \n" " iconPath := DIR_NAME . \"\\close.ico\"\n" " AddTrayMenuIcon(\"Kapat\", iconPath)\n" "}\n\n" "SendWindowToTrayByID(ahkID) {\n" " WinHide ahk_id %ahkID%\n" "}\n\n" "; WARN: Bug sebebi olabilir (bundan değil bug)\n" "; WARN: Eğer uyarı mesajı verilirse, odaklanma bozuluyor\n" "FocusPreviusWindow(ahkID) {\n" " SendEvent, !{Esc}\n" " WinWaitNotActive, ahk_id %ahkID%\n" "}\n\n" "ToggleWindowWithID(ahkID, hide=False) {\n" " DetectHiddenWindows, Off\n" " if !WinExist(\"ahk_id\" . ahkID) {\n" " if hide {\n" " ShowHidedWindowWithID(ahkID)\n" " ActivateWindowWithID(ahkID)\n" " if DropActiveWindowFromMem()\n" " DropActiveWindowFromTrayMenu()\n" " CreateOrUpdateTrayMenu()\n" " } else {\n" " ActivateWindowWithID(ahkID)\n" " }\n" " } else {\n" " if WinActive(\"ahk_id\" . ahkID) {\n" " if hide {\n" " KeepActiveWindowInMem()\n" " FocusPreviusWindow(ahkID)\n" " SendWindowToTrayByID(ahkID)\n" " CreateOrUpdateTrayMenu()\n" " } else {\n" " WinMinimize, A\n" " }\n" " } else {\n" " ActivateWindowWithID(ahkID)\n" " }\n" " }\n" "}\n\n" "OpenWindowInTray(selector, name, url, mode=3) {\n" " SetTitleMatchMode, %mode%\n" " DetectHiddenWindows, On\n" " \n" " IDlist := []\n" " if (selector == \"title\") {\n" " WinGet, IDlist, list, %name%\n" " } else if (selector == \"class\") {\n" " WinGet, IDlist, list, ahk_class %name%\n" " } else if (selector == \"exe\") {\n" " WinGet, IDlist, list, ahk_exe %name%\n" " }\n" " \n" " found := False\n" " Loop, %IDlist% {\n" " ahkID := IDlist%A_INDEX%\n" " if WinExist(\"ahk_id\" . ahkID) {\n" " WinGetTitle, title\n" " if (title == \"\")\n" " continue\n" " \n" " ToggleWindowWithID(ahkID, True)\n" " found := True\n" " }\n" " }\n" " if !found\n" " RunUrl(url)\n" "}\n\n" "OpenWindowByTitle(title, url, mode=3) {\n" " SetTitleMatchMode, %mode%\n" " DetectHiddenWindows, Off\n" " \n" " if WinExist(title) {\n" " WinGet, ahkID, ID, %title%\n" " ToggleWindowWithID(ahkID, False)\n" " } else {\n" " RunUrl(url)\n" " }\n" "}\n\n" "GetEnvPath(envvar, path=\"\"){\n" " EnvGet, prepath, %envvar%\n" " path = %prepath%%path%\n" "return path\n" "}\n\n" "; ####################################################################################\n" "; ## ##\n" "; ## KISAYOLLAR ##\n" "; ## ##\n" "; ####################################################################################\n\n" "; ---------------------------------- Göster / Gizle ----------------------------------\n" "#q::\n" " name := \"- OneNote\"\n" " path := \"shell:appsFolder\\Microsoft.Office.OneNote_8wekyb3d8bbwe!microsoft.onenoteim\"\n" " mode := 2\n" " OpenWindowByTitle(name, path, mode)\n" "return\n\n" "; #t::\n" "; name := \"Tureng Dictionary\"\n" "; path := \"shell:appsFolder\\24232AlperOzcetin.Tureng_9n2ce2f97t3e6!App\"\n" "; mode := 2\n" "; OpenWindowByTitle(name, path, mode)\n" "; return\n\n" "; --------------------------------- Tray Kısayolları ---------------------------------\n\n" "#w::\n" " ; WARN: 4 tane var exe ile ele alınmalı WhatsApp.exe (bundan değil)\n" " name := \"WhatsApp\"\n" " path := \"shell:appsFolder\\5319275A.WhatsAppDesktop_cv1g1gvanyjgm!WhatsAppDesktop\"\n" " mode := 2\n" " OpenWindowInTray(\"title\", name, path, mode)\n" "return\n\n" "#g::\n" " name := \"GitHub Desktop\"\n" " path := GetEnvPath(\"localappdata\", \"\\GitHubDesktop\\GitHubDesktop.exe\")\n" " mode := 3\n" " OpenWindowInTray(\"title\", name, path, mode)\n" "return\n\n" "#x::\n" " name := \"Google Calendar\"\n" " path := GetEnvPath(\"appdata\", \"\\Microsoft\\Windows\\Start Menu\\Programs\\Chrome Apps\\Google Calendar.lnk\")\n" " mode := 2\n" " OpenWindowInTray(\"title\", name, path, mode)\n" "return\n\n" "#e::\n" " name := \"CabinetWClass\"\n" " path := \"explorer.exe\"\n" " OpenWindowInTray(\"class\", name, path)\n" "return\n\n" "; Dizin kısayolları PgDn ile başlar\n" "PgDn & g::\n" " name := \"GitHub\"\n" " path := GetEnvPath(\"userprofile\", \"\\Documents\\GitHub\")\n" " OpenWindowInTray(\"title\", name, path)\n" "return\n\n" "PgDn & s::\n" " name := \"ShareX\"\n" " path := \"shell:appsFolder\\19568ShareX.ShareX_egrzcvs15399j!ShareX\"\n" " mode := 3\n" " OpenWindowInTray(\"title\", name, path, mode)\n" "return\n\n" "PgDn & Shift::\n" " name := \"Startup\"\n" " path := \"shell:startup\"\n" " mode := 3\n" " OpenWindowInTray(\"title\", name, path, mode)\n" "return\n\n" "PgDn & i::\n" " name := \"Icons\"\n" " path := GetEnvPath(\"userprofile\", \"\\Google Drive\\Pictures\\Icons\")\n" " mode := 3\n" " OpenWindowInTray(\"title\", name, path, mode)\n" "return\n\n" "PgDn & d::\n" " name := \"Downloads\"\n" " path := \"shell:downloads\"\n" " mode := 3\n" " OpenWindowInTray(\"title\", name, path, mode)\n" "return\n\n" "PgDn & u::\n" " name := \"Yunus Emre Ak\"\n" " path := GetEnvPath(\"userprofile\")\n" " mode := 3\n" " OpenWindowInTray(\"title\", name, path, mode)\n" "return\n\n" "; --------------------------------- Buton Kısayolları ---------------------------------\n\n" "; Değiştirilen butonları kurtarma\n" "Control & PgDn::\n" " Send , !{PgDn}\n" "return\n" "Control & PgUp::\n" " Send , !{PgUp}\n" "return\n") 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