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
No Match

@"
"
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"(?xim)\n" r"^\s*\n" r"(?<Num1>\d+)\n" r"\s+\n" r"(?<Dots>.*?)\n" r"\s+\n" r"(?<X>.*?)\n" r"\s+\n" r"(?<ExtList>.*?)\n" r"\s{2,}\n" r"(?<Rest>.*)\n" r"$\n\n" r"# //\n" r"# (?<ExtList>.*)\n" r"# \s{2,}\n" r"# (?<BytesOr>(\|.{2}))*?\n" r"#$") test_str = ("7z -i\n" "7-Zip 22.00 (x64) : Copyright (c) 1999-2022 Igor Pavlov : 2022-06-15\n" "Libs:\n" " 0 c:\\program files\\7-zip\\7z.dll\n\n" "Formats:\n" " 0 C...F..........c.a.m+.. w...0 7z 7z 7 z BC AF ' 1C\n" " 0 ...................... APFS apfs img offset=32 N X S B 00\n" " 0 ...................... APM apm E R\n" " 0 ...................... Ar ar a deb udeb lib ! < a r c h > 0A\n" " 0 ...................... Arj arj ` EA\n" " 0 K.....O.....X......... Base64 b64 \n" " 0 ......O............... COFF obj \n" " 0 ...F.................. Cab cab M S C F 00 00 00 00\n" " 0 ...................... Chm chm chi chq chw I T S F 03 00 00 00 ` 00 00 00\n" " 0 ...................... Compound msi msp doc xls ppt D0 CF 11 E0 A1 B1 1A E1\n" " 0 ....M................. Cpio cpio 0 7 0 7 0 || C7 q || q C7\n" " 0 ...................... CramFS cramfs offset=16 C o m p r e s s e d 20 R O M F S\n" " 0 .....G..B............. Dmg dmg k o l y 00 00 00 04 00 00 02 00\n" " 0 .........E............ ELF elf  E L F\n" " 0 ...................... Ext ext ext2 ext3 ext4 img offset=1080 S EF\n" " 0 ...................... FAT fat img offset=510 U AA\n" " 0 ...................... FLV flv F L V 01\n" " 0 ...................... GPT gpt mbr offset=512 E F I 20 P A R T 00 00 01 00\n" " 0 ....M................. HFS hfs hfsx offset=1024 B D || H + 00 04 || H X 00 05\n" " 0 ...F.................. Hxs hxs hxi hxr hxq hxw lit I T O L I T L S 01 00 00 00 ( 00 00 00\n" " 0 ......O............... IHex ihex \n" " 0 ...................... Iso iso img offset=32769 C D 0 0 1\n" " 0 ...................... LP lpimg img offset=4096 g D l a 4 00 00 00\n" " 0 ...................... Lzh lzh lha offset=2 - l h\n" " 0 .......P.............. MBR mbr \n" " 0 ....M....E............ MachO macho CE FA ED FE || CF FA ED FE || FE ED FA CE || FE ED FA CF\n" " 0 ...................... MsLZ mslz S Z D D 88 F0 ' 3 A\n" " 0 ....M................. Mub mub CA FE BA BE 00 00 00 || B9 FA F1 0E\n" " 0 ...................... NTFS ntfs img offset=3 N T F S 20 20 20 20 00\n" " 0 ...F.G................ Nsis nsis offset=4 EF BE AD DE N u l l s o f t I n s t\n" " 0 .........E............ PE exe dll sys M Z\n" " 0 ...................... Ppmd pmd 8F AF AC 84\n" " 0 ...................... QCOW qcow qcow2 qcow2c Q F I FB 00 00 00\n" " 0 ...F.................. Rar rar r00 R a r ! 1A 07 00\n" " 0 ...F.................. Rar5 rar r00 R a r ! 1A 07 01 00\n" " 0 ...................... Rpm rpm ED AB EE DB\n" " 0 K..................... SWF swf F W S\n" " 0 ....M................. SWFc swf (~.swf) C W S || Z W S\n" " 0 ...................... Sparse simg img : FF & ED 01 00\n" " 0 ...................... Split 001 \n" " 0 ....M................. SquashFS squashfs h s q s || s q s h || s h s q || q s h s\n" " 0 .........E............ TE te V Z\n" " 0 ...FM................. UEFIc scap BD 86 f ; v 0D 0 @ B7 0E B5 Q 9E / C5 A0 || 8B A6 < J # w FB H 80 = W 8C C1 FE C4 M || B9 82 91 S B5 AB 91 C B6 9A E3 A9 C F7 / CC\n" " 0 ...FM................. UEFIf uefif offset=16 D9 T 93 z h 04 J D 81 CE 0B F6 17 D8 90 DF || x E5 8C 8C = 8A 1C O 99 5 89 a 85 C3 - D3\n" " 0 ......O............... Udf udf iso img offset=32768 01 C D 0 0 1\n" " 0 ...................... VDI vdi offset=64  10 DA BE\n" " 0 .....G................ VHD vhd c o n e c t i x 00 00\n" " 0 ...................... VHDX vhdx avhdx v h d x f i l e\n" " 0 ...................... VMDK vmdk K D M V\n" " 0 ...................... Xar xar pkg xip x a r ! 00 1C\n" " 0 ...................... Z z taz (.tar) 1F 9D\n" " 0 CK..................... bzip2 bz2 bzip2 tbz2 (.tar) tbz (.tar) B Z h\n" " 0 CK.................m+.. .u..1 gzip gz gzip tgz (.tar) tpz (.tar) apk (.tar) 1F 8B 08\n" " 0 K.....O............... lzma lzma \n" " 0 K..................... lzma86 lzma86 \n" " 0 C......O...LH......m+.. wu.n1 tar tar ova offset=257 u s t a r\n" " 0 C.SN.......LH..c.a.m+.. w...0 wim wim swm esd ppkg M S W I M 00 00 00\n" " 0 CK..................... xz xz txz (.tar) FD 7 z X Z 00\n" " 0 C...FMG........c.a.m+.. wud.0 zip zip z01 zipx jar xpi odt ods docx xlsx epub ipa apk appx P K 03 04 || P K 05 06 || P K 06 06 || P K 07 08 P K || P K 0 0 P K\n" " CK.....O.....XC........ Hash sha256 sha512 sha224 sha384 sha1 sha md5 crc32 crc64 asc cksum \n\n" "Codecs:\n" " 0 4ED 303011B BCJ2\n" " 0 EDF 3030103 BCJ\n" " 0 EDF 3030205 PPC\n" " 0 EDF 3030401 IA64\n" " 0 EDF 3030501 ARM\n" " 0 EDF 3030701 ARMT\n" " 0 EDF 3030805 SPARC\n" " 0 EDF 20302 Swap2\n" " 0 EDF 20304 Swap4\n" " 0 ED 40202 BZip2\n" " 0 ED 0 Copy\n" " 0 ED 40109 Deflate64\n" " 0 ED 40108 Deflate\n" " 0 EDF 3 Delta\n" " 0 ED 21 LZMA2\n" " 0 ED 30101 LZMA\n" " 0 ED 30401 PPMD\n" " 0 D 40301 Rar1\n" " 0 D 40302 Rar2\n" " 0 D 40303 Rar3\n" " 0 D 40305 Rar5\n" " 0 EDF 6F10701 7zAES\n" " 0 EDF 6F00181 AES256CBC\n\n" "Hashers:\n" " 0 4 1 CRC32\n" " 0 20 201 SHA1\n" " 0 32 A SHA256\n" " 0 8 4 CRC64\n" " 0 32 202 BLAKE2sp") 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