Regular Expressions 101

Save & Manage Regex

  • Current Version: 1
  • Save & Share
  • Community Library

Flavor

  • PCRE2 (PHP)
  • ECMAScript (JavaScript)
  • Python
  • Golang
  • Java
  • .NET 7.0 (C#)
  • Rust
  • PCRE (Legacy)
  • Regex Flavor Guide

Function

  • Match
  • Substitution
  • List
  • Unit Tests
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
  • 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
Processing...

Test String

Code Generator

Generated Code

import re regex = re.compile(r"^General(.*?)^General|[\s]", flags=re.MULTILINE | re.DOTALL) test_str = ("General\n" "Complete name : G:\\VIDEO_TS\\VTS_04_1.VOB\n" "Format : MPEG-PS\n" "File size : 1 024 MiB\n" "Duration : 29 min 25 s\n" "Overall bit rate mode : Variable\n" "Overall bit rate : 4 867 kb/s\n\n" "Video\n" "ID : 224 (0xE0)\n" "Format : MPEG Video\n" "Format version : Version 2\n" "Format profile : Main@Main\n" "Format settings : CustomMatrix / BVOP\n" "Format settings, BVOP : Yes\n" "Format settings, Matrix : Custom\n" "Format settings, GOP : M=3, N=15\n" "Format settings, picture structure : Frame\n" "Duration : 29 min 25 s\n" "Bit rate mode : Variable\n" "Bit rate : 3 650 kb/s\n" "Maximum bit rate : 8 500 kb/s\n" "Width : 720 pixels\n" "Height : 480 pixels\n" "Display aspect ratio : 16:9\n" "Frame rate : 29.970 (30000/1001) FPS\n" "Standard : NTSC\n" "Color space : YUV\n" "Chroma subsampling : 4:2:0\n" "Bit depth : 8 bits\n" "Scan type : Interlaced\n" "Scan order : Top Field First\n" "Compression mode : Lossy\n" "Bits/(Pixel*Frame) : 0.352\n" "Time code of first frame : 00:59:59:15\n" "Time code source : Group of pictures header\n" "GOP, Open/Closed : Closed\n" "Stream size : 768 MiB (75%)\n\n" "Audio #1\n" "ID : 189 (0xBD)-128 (0x80)\n" "Format : AC-3\n" "Format/Info : Audio Coding 3\n" "Commercial name : Dolby Digital\n" "Muxing mode : DVD-Video\n" "Duration : 29 min 24 s\n" "Bit rate mode : Constant\n" "Bit rate : 448 kb/s\n" "Channel(s) : 2 channels\n" "Channel layout : L R\n" "Sampling rate : 48.0 kHz\n" "Frame rate : 31.250 FPS (1536 SPF)\n" "Compression mode : Lossy\n" "Stream size : 94.2 MiB (9%)\n" "Service kind : Complete Main\n\n" "Audio #2\n" "ID : 189 (0xBD)-129 (0x81)\n" "Format : AC-3\n" "Format/Info : Audio Coding 3\n" "Commercial name : Dolby Digital\n" "Muxing mode : DVD-Video\n" "Duration : 29 min 24 s\n" "Bit rate mode : Constant\n" "Bit rate : 448 kb/s\n" "Channel(s) : 6 channels\n" "Channel layout : L R C LFE Ls Rs\n" "Sampling rate : 48.0 kHz\n" "Frame rate : 31.250 FPS (1536 SPF)\n" "Compression mode : Lossy\n" "Stream size : 94.2 MiB (9%)\n" "Service kind : Complete Main\n\n" "Audio #3\n" "ID : 189 (0xBD)-130 (0x82)\n" "Format : AC-3\n" "Format/Info : Audio Coding 3\n" "Commercial name : Dolby Digital\n" "Muxing mode : DVD-Video\n" "Duration : 29 min 24 s\n" "Bit rate mode : Constant\n" "Bit rate : 224 kb/s\n" "Channel(s) : 2 channels\n" "Channel layout : L R\n" "Sampling rate : 48.0 kHz\n" "Frame rate : 31.250 FPS (1536 SPF)\n" "Compression mode : Lossy\n" "Stream size : 47.1 MiB (5%)\n" "Service kind : Complete Main\n\n" "Text\n" "ID : 189 (0xBD)-33 (0x21)\n" "Format : RLE\n" "Format/Info : Run-length encoding\n" "Muxing mode : DVD-Video\n" "Delay relative to video : 7 s 874 ms\n\n" "General\n" "Complete name : G:\\VIDEO_TS\\VTS_04_0.IFO\n" "Format : DVD Video\n" "Format profile : Program\n" "File size : 68.0 KiB\n" "Duration : 1 h 37 min\n" "Overall bit rate mode : Variable\n" "Overall bit rate : 95 b/s\n\n" "Video\n" "ID : 224 (0xE0)\n" "Format : MPEG Video\n" "Format version : Version 2\n" "Duration : 1 h 37 min\n" "Bit rate mode : Variable\n" "Width : 720 pixels\n" "Height : 480 pixels\n" "Display aspect ratio : 16:9\n" "Frame rate : 29.970 (29970/1000) FPS\n" "Standard : NTSC\n" "Compression mode : Lossy\n" "Duration_Source : General_Duration\n\n" "Audio #1\n" "ID : 128 (0x80)\n" "Format : AC-3\n" "Format/Info : Audio Coding 3\n" "Duration : 1 h 37 min\n" "Channel(s) : 2 channels\n" "Sampling rate : 48.0 kHz\n" "Compression mode : Lossy\n" "Language : Chinese\n" "Duration_Source : General_Duration\n\n" "Audio #2\n" "ID : 129 (0x81)\n" "Format : AC-3\n" "Format/Info : Audio Coding 3\n" "Duration : 1 h 37 min\n" "Channel(s) : 6 channels\n" "Sampling rate : 48.0 kHz\n" "Compression mode : Lossy\n" "Language : Chinese\n" "Duration_Source : General_Duration\n\n" "Audio #3\n" "ID : 130 (0x82)\n" "Format : AC-3\n" "Format/Info : Audio Coding 3\n" "Duration : 1 h 37 min\n" "Channel(s) : 2 channels\n" "Sampling rate : 48.0 kHz\n" "Compression mode : Lossy\n" "Language : Portuguese\n" "Duration_Source : General_Duration\n\n" "Text #1\n" "ID : 32 (0x20)\n" "Format : RLE\n" "Format/Info : Run-length encoding\n" "Bit depth : 2 bits\n" "Language : Portuguese\n\n" "Text #2\n" "ID : 33 (0x21)\n" "Format : RLE\n" "Format/Info : Run-length encoding\n" "Bit depth : 2 bits\n\n" "Menu\n" "Duration : 1 h 37 min\n" "00:00:00.000 : Chapter 1\n" "00:07:10.500 : Chapter 2\n" "00:14:26.500 : Chapter 3\n" "00:23:42.000 : Chapter 4\n" "00:31:12.000 : Chapter 5\n" "00:39:38.000 : Chapter 6\n" "00:46:15.500 : Chapter 7\n" "00:55:23.500 : Chapter 8\n" "01:02:27.500 : Chapter 9\n" "01:10:12.500 : Chapter 10\n" "01:19:13.500 : Chapter 11\n" "01:26:13.000 : Chapter 12\n" "01:37:46.200 : Chapter 13\n" "List (Audio) : 0 / 1 / 2\n" "List (Subtitles 4/3) : 0 / 0\n" "List (Subtitles Wide) : 0 / 1\n" "List (Subtitles Letterbox) : 0 / 1\n" "List (Subtitles Pan&Scan) : 0 / 1\n\n" "General\n" "Unique ID : 265651434810034235117589437423295755026 (0xC7DA9AF5666AD1527A66985978717312)\n" "Complete name : Aladdin.1992.2160p.UHD.BluRay.x265-TERMiNAL.DUAL-Lannister.mkv\n" "Format : Matroska\n" "Format version : Version 4\n" "File size : 8.02 GiB\n" "Duration : 1 h 30 min\n" "Overall bit rate mode : Variable\n" "Overall bit rate : 12.7 Mb/s\n" "Encoded date : UTC 2019-09-12 17:17:31\n" "Writing application : mkvmerge v22.0.0 ('At The End Of The World') 64-bit\n" "Writing library : libebml v1.3.5 + libmatroska v1.4.8\n\n" "Video\n" "ID : 1\n" "Format : HEVC\n" "Format/Info : High Efficiency Video Coding\n" "Format profile : Main 10@L5.1@High\n" "HDR format : SMPTE ST 2086, HDR10 compatible\n" "Codec ID : V_MPEGH/ISO/HEVC\n" "Duration : 1 h 30 min\n" "Bit rate : 6 532 kb/s\n" "Width : 3 584 pixels\n" "Height : 2 160 pixels\n" "Display aspect ratio : 5:3\n" "Frame rate mode : Constant\n" "Frame rate : 23.976 (24000/1001) FPS\n" "Color space : YUV\n" "Chroma subsampling : 4:2:0 (Type 2)\n" "Bit depth : 10 bits\n" "Bits/(Pixel*Frame) : 0.035\n" "Stream size : 4.14 GiB (52%)\n" "Writing library : x265 3.1+11-de920e0a3183:[Windows][GCC 6.3.0][64 bit] 10bit\n" "Encoding settings : cpuid=1176575 / frame-threads=6 / numa-pools=36 / wpp / no-pmode / no-pme / no-psnr / no-ssim / log-level=2 / input-csp=1 / input-res=3584x2160 / interlace=0 / total-frames=130417 / level-idc=51 / high-tier=1 / uhd-bd=0 / ref=4 / no-allow-non-conformance / repeat-headers / annexb / aud / hrd / info / hash=0 / no-temporal-layers / no-open-gop / min-keyint=1 / keyint=24 / gop-lookahead=0 / bframes=4 / b-adapt=2 / b-pyramid / bframe-bias=0 / rc-lookahead=25 / lookahead-slices=4 / scenecut=40 / radl=0 / no-splice / no-intra-refresh / ctu=64 / min-cu-size=8 / rect / no-amp / max-tu-size=32 / tu-inter-depth=1 / tu-intra-depth=1 / limit-tu=0 / rdoq-level=2 / dynamic-rd=0.00 / no-ssim-rd / signhide / no-tskip / nr-intra=0 / nr-inter=0 / no-constrained-intra / no-strong-intra-smoothing / max-merge=3 / limit-refs=3 / limit-modes / me=3 / subme=3 / merange=57 / temporal-mvp / no-hme / weightp / no-weightb / no-analyze-src-pics / deblock=-3:-3 / no-sao / no-sao-non-deblock / rd=4 / no-early-skip / rskip / no-fast-intra / no-tskip-fast / no-cu-lossless / no-b-intra / no-splitrd-skip / rdpenalty=0 / psy-rd=2.00 / psy-rdoq=1.00 / no-rd-refine / no-lossless / cbqpoffs=0 / crqpoffs=0 / rc=crf / crf=17.0 / qcomp=0.60 / qpstep=4 / stats-write=0 / stats-read=0 / vbv-maxrate=160000 / vbv-bufsize=160000 / vbv-init=0.9 / crf-max=0.0 / crf-min=0.0 / ipratio=1.40 / pbratio=1.30 / aq-mode=2 / aq-strength=1.00 / cutree / zone-count=0 / no-strict-cbr / qg-size=32 / no-rc-grain / qpmax=69 / qpmin=0 / no-const-vbv / sar=1 / overscan=0 / videoformat=5 / range=0 / colorprim=9 / transfer=16 / colormatrix=9 / chromaloc=1 / chromaloc-top=2 / chromaloc-bottom=2 / display-window=0 / master-display=G(13250,34500)B(7500,3000)R(34000,16000)WP(15635,16450)L(10000000,20)cll=0,0 / min-luma=0 / max-luma=1023 / log2-max-poc-lsb=8 / vui-timing-info / vui-hrd-info / slices=1 / no-opt-qp-pps / no-opt-ref-list-length-pps / no-multi-pass-opt-rps / scenecut-bias=0.05 / no-opt-cu-delta-qp / no-aq-motion / hdr / hdr-opt / no-dhdr10-opt / no-idr-recovery-sei / analysis-reuse-level=5 / scale-factor=0 / refine-intra=0 / refine-inter=0 / refine-mv=0 / refine-ctu-distortion=0 / no-limit-sao / ctu-info=0 / no-lowpass-dct / refine-analysis-type=0 / copy-pic=1 / max-ausize-factor=1.0 / no-dynamic-refine / no-single-sei / no-hevc-aq / no-svt / no-field / qp-adaptation-range=1.00\n" "Default : Yes\n" "Forced : No\n" "Color range : Limited\n" "Color primaries : BT.2020\n" "Transfer characteristics : PQ\n" "Matrix coefficients : BT.2020 non-constant\n" "Mastering display color primaries : Display P3\n" "Mastering display luminance : min: 0.0020 cd/m2, max: 1000 cd/m2\n\n" "Audio #1\n" "ID : 2\n" "Format : AC-3\n" "Format/Info : Audio Coding 3\n" "Commercial name : Dolby Digital\n" "Codec ID : A_AC3\n" "Duration : 1 h 30 min\n" "Bit rate mode : Constant\n" "Bit rate : 640 kb/s\n" "Channel(s) : 6 channels\n" "Channel layout : L R C LFE Ls Rs\n" "Sampling rate : 48.0 kHz\n" "Frame rate : 31.250 FPS (1536 SPF)\n" "Bit depth : 16 bits\n" "Compression mode : Lossy\n" "Stream size : 415 MiB (5%)\n" "Title : by Lannister\n" "Language: Portuguese\n" "Service kind : Complete Main\n" "Default : Yes\n" "Forced : Yes\n\n" "Audio #2\n" "ID : 3\n" "Format : MLP FBA 16-ch\n" "Format/Info : Meridian Lossless Packing FBA with 16-channel presentation\n" "Commercial name : Dolby TrueHD with Dolby Atmos\n" "Codec ID : A_TRUEHD\n" "Duration : 1 h 30 min\n" "Bit rate mode : Variable\n" "Bit rate : 4 768 kb/s\n" "Maximum bit rate : 6 816 kb/s\n" "Channel(s) : 8 channels\n" "Channel layout : L R C LFE Ls Rs Lb Rb\n" "Sampling rate : 48.0 kHz\n" "Frame rate : 1 200.000 FPS (40 SPF)\n" "Compression mode : Lossless\n" "Stream size : 3.02 GiB (38%)\n" "Language : English\n" "Default : No\n" "Forced : No\n" "Number of dynamic objects : 13\n" "Bed channel count : 1 channel\n" "Bed channel configuration : LFE\n\n" "Audio #3\n" "ID : 4\n" "Format : AC-3\n" "Format/Info : Audio Coding 3\n" "Commercial name : Dolby Digital\n" "Codec ID : A_AC3\n" "Duration : 1 h 30 min\n" "Bit rate mode : Constant\n" "Bit rate : 640 kb/s\n" "Channel(s) : 6 channels\n" "Channel layout : L R C LFE Ls Rs\n" "Sampling rate : 48.0 kHz\n" "Frame rate : 31.250 FPS (1536 SPF)\n" "Bit depth : 16 bits\n" "Compression mode : Lossy\n" "Stream size : 415 MiB (5%)\n" "Language : English\n" "Service kind : Complete Main\n" "Default : No\n" "Forced : No\n\n" "Text\n" "ID : 5\n" "Format : PGS\n" "Muxing mode : zlib\n" "Codec ID : S_HDMV/PGS\n" "Codec ID/Info : Picture based subtitle format used on BDs/HD-DVDs\n" "Duration : 1 h 29 min\n" "Bit rate : 35.9 kb/s\n" "Count of elements : 2352\n" "Stream size : 23.0 MiB (0%)\n" "Language : Portuguese\n" "Default : No\n" "Forced : No\n\n" "General\n" "Unique ID : 96295411837011032641642823181481344850 (0x4871D25C0188DBA1F9468FCC41F8FF52)\n" "Complete name : Coração Valente 1995 4k 2160p Dual Audio.mkv\n" "Format : Matroska\n" "Format version : Version 4\n" "File size : 45.7 GiB\n" "Duration : 2 h 57 min\n" "Overall bit rate : 36.9 Mb/s\n" "Movie name : Coração Valente\n" "Encoded date : UTC 2019-09-24 12:13:44\n" "Writing application : mkvmerge v37.0.0 ('Leave It') 64-bit\n" "Writing library : libebml v1.3.9 + libmatroska v1.5.2\n" "Cover : Yes\n" "Attachments : cover3.jpeg\n\n" "Video\n" "ID : 1\n" "Format : HEVC\n" "Format/Info : High Efficiency Video Coding\n" "Format profile : Main 10@L5.1@High\n" "HDR format : SMPTE ST 2086, HDR10 compatible\n" "Codec ID : V_MPEGH/ISO/HEVC\n" "Duration : 2 h 57 min\n" "Bit rate : 36.0 Mb/s\n" "Width : 3 840 pixels\n" "Height : 1 634 pixels\n" "Display aspect ratio : 2.35:1\n" "Frame rate mode : Constant\n" "Frame rate : 23.976 (24000/1001) FPS\n" "Color space : YUV\n" "Chroma subsampling : 4:2:0 (Type 2)\n" "Bit depth : 10 bits\n" "Bits/(Pixel*Frame) : 0.239\n" "Stream size : 44.7 GiB (98%)\n" "Title : DUAL AUDIO\n" "Writing library : x265 2.7+348-0968a46d6ba4:[Windows][GCC 7.3.0][64 bit] 10bit\n" "Encoding settings : cpuid=1176575 / frame-threads=5 / numa-pools=32 / wpp / no-pmode / no-pme / no-psnr / no-ssim / log-level=2 / input-csp=1 / input-res=3840x1634 / interlace=0 / total-frames=255642 / level-idc=51 / high-tier=1 / uhd-bd=0 / ref=4 / no-allow-non-conformance / repeat-headers / annexb / aud / hrd / info / hash=0 / no-temporal-layers / no-open-gop / min-keyint=24 / keyint=240 / gop-lookahead=0 / bframes=4 / b-adapt=2 / b-pyramid / bframe-bias=0 / rc-lookahead=25 / lookahead-slices=4 / scenecut=40 / radl=0 / no-intra-refresh / ctu=64 / min-cu-size=8 / rect / no-amp / max-tu-size=32 / tu-inter-depth=1 / tu-intra-depth=1 / limit-tu=0 / rdoq-level=2 / dynamic-rd=0.00 / no-ssim-rd / signhide / no-tskip / nr-intra=0 / nr-inter=0 / no-constrained-intra / no-strong-intra-smoothing / max-merge=3 / limit-refs=3 / limit-modes / me=3 / subme=3 / merange=57 / temporal-mvp / weightp / no-weightb / no-analyze-src-pics / deblock=-3:-3 / no-sao / no-sao-non-deblock / rd=4 / no-early-skip / rskip / no-fast-intra / no-tskip-fast / no-cu-lossless / no-b-intra / no-splitrd-skip / rdpenalty=0 / psy-rd=2.00 / psy-rdoq=1.00 / no-rd-refine / no-lossless / cbqpoffs=0 / crqpoffs=0 / rc=crf / crf=18.0 / qcomp=0.60 / qpstep=4 / stats-write=0 / stats-read=0 / vbv-maxrate=160000 / vbv-bufsize=160000 / vbv-init=0.9 / crf-max=0.0 / crf-min=0.0 / ipratio=1.40 / pbratio=1.30 / aq-mode=1 / aq-strength=1.00 / cutree / zone-count=0 / no-strict-cbr / qg-size=32 / no-rc-grain / qpmax=69 / qpmin=0 / no-const-vbv / sar=1 / overscan=0 / videoformat=5 / range=0 / colorprim=9 / transfer=16 / colormatrix=9 / chromaloc=1 / chromaloc-top=2 / chromaloc-bottom=2 / display-window=0 / master-display=G(13250,34500)B(7500,3000)R(34000,16000)WP(15635,16450)L(10000000,1) / max-cll=0,0 / min-luma=0 / max-luma=1023 / log2-max-poc-lsb=8 / vui-timing-info / vui-hrd-info / slices=1 / no-opt-qp-pps / no-opt-ref-list-length-pps / no-multi-pass-opt-rps / scenecut-bias=0.05 / no-opt-cu-delta-qp / no-aq-motion / hdr / hdr-opt / no-dhdr10-opt / no-idr-recovery-sei / analysis-reuse-level=5 / scale-factor=0 / refine-intra=0 / refine-inter=0 / refine-mv=0 / no-limit-sao / ctu-info=0 / no-lowpass-dct / refine-mv-type=0 / copy-pic=1 / max-ausize-factor=1.0 / no-dynamic-refine / no-single-sei\n" "Language : Portuguese\n" "Default : Yes\n" "Forced : Yes\n" "Color range : Limited\n" "Color primaries : BT.2020\n" "Transfer characteristics : PQ\n" "Matrix coefficients : BT.2020 non-constant\n" "Mastering display color primaries : Display P3\n" "Mastering display luminance : min: 0.0001 cd/m2, max: 1000 cd/m2\n\n" "Audio #1\n" "ID : 2\n" "Format : AC-3\n" "Format/Info : Audio Coding 3\n" "Commercial name : Dolby Digital\n" "Codec ID : A_AC3\n" "Duration : 2 h 57 min\n" "Bit rate mode : Constant\n" "Bit rate : 192 kb/s\n" "Channel(s) : 2 channels\n" "Channel layout : L R\n" "Sampling rate : 48.0 kHz\n" "Frame rate : 31.250 FPS (1536 SPF)\n" "Bit depth : 16 bits\n" "Compression mode : Lossy\n" "Stream size : 244 MiB (1%)\n" "Title : AC3 2.0\n" "Language : Portuguese\n" "Service kind : Complete Main\n" "Default : Yes\n" "Forced : Yes\n\n" "Audio #2ID : 3\n" "Format : AC-3\n" "Format/Info : Audio Coding 3\n" "Commercial name : Dolby Digital\n" "Codec ID : A_AC3\n" "Duration : 2 h 57 min\n" "Bit rate mode : Constant\n" "Bit rate : 640 kb/s\n" "Channel(s) : 6 channels\n" "Channel layout : L R C LFE Ls Rs\n" "Sampling rate : 48.0 kHz\n" "Frame rate : 31.250 FPS (1536 SPF)\n" "Bit depth : 16 bits\n" "Compression mode : Lossy\n" "Delay relative to video : 2 s 169 ms\n" "Stream size : 813 MiB (2%)\n" "Title : AC-3 5.1\n" "Language : English\n" "Service kind : Complete Main\n" "Default : No\n" "Forced : No\n\n") match = regex.search(test_str) if match: print(f"Match was found at {match.start()}-{match.end()}: {match.group()}") for group_num, group in enumerate(match.groups(), start=1): print(f"Group {group_num} found at {match.start(group_num)}-{match.end(group_num)}: {group}")

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