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

/
/
gm

Test String

Substitution

Processing...

Code Generator

Generated Code

re = /^(?<artist>[\w ]+)\/(?<album>[\w ]+)\/(( - )|(\d+)|\k<artist>|\k<album>|[_])*(?<name>.+)(?<ext>\.(flac|mp3))$/m str = 'Infected Mushroom/ Infected Mushroom/Vicious Delicious Infected Mushroom/Vicious Delicious/01 - Infected Mushroom - Becoming Insane.flac Infected Mushroom/Vicious Delicious/02 - Infected Mushroom - Artillery.flac Infected Mushroom/Vicious Delicious/03 - Infected Mushroom - Vicious Delicious.flac Infected Mushroom/Vicious Delicious/04 - Infected Mushroom - Heavyweight.flac Infected Mushroom/Vicious Delicious/05 - Infected Mushroom - Suliman.flac Infected Mushroom/Vicious Delicious/06 - Infected Mushroom - Forgive Me.flac Infected Mushroom/Vicious Delicious/07 - Infected Mushroom - Special Place.flac Infected Mushroom/Vicious Delicious/08 - Infected Mushroom - In Front Of Me.flac Infected Mushroom/Vicious Delicious/09 - Infected Mushroom - Eat It Raw.flach Mushroom - Before.flac Infected Mushroom/Army of Mushrooms Infected Mushroom/Army of Mushrooms/01 - Never Mind.flac Infected Mushroom/Army of Mushrooms/02 - Nothing to Say.flac Infected Mushroom/Army of Mushrooms/03 - Send Me an Angel.flac Infected Mushroom/Army of Mushrooms/04 - U R So Fucked.flac Infected Mushroom/Army of Mushrooms/05 - The Rat.flac Infected Mushroom/Army of Mushrooms/06 - Nation of Wusses.flac Infected Mushroom/Army of Mushrooms/07 - Wanted To.flac Infected Mushroom/Army of Mushrooms/08 - Serve My Thirst.flac Infected Mushroom/Army of Mushrooms/09 - I Shine.flac Infected Mushroom/Army of Mushrooms/10 - Drum n\' Bassa.flac Infected Mushroom/Army of Mushrooms/11 - The Pretender.flac Infected Mushroom/Army of Mushrooms/12 - The Messenger 2012.flac Infected Mushroom/Converting Vegetarians II Infected Mushroom/Converting Vegetarians II/01 - She Zoremet.flac Infected Mushroom/Converting Vegetarians II/02 - Yamakas in Space.flac Infected Mushroom/Converting Vegetarians II/03 - Sense of Direction.flac Infected Mushroom/Converting Vegetarians II/04 - Animatronica.flac Infected Mushroom/Converting Vegetarians II/05 - Feelings.flac Infected Mushroom/Converting Vegetarians II/06 - Pink Froid.flac Infected Mushroom/Converting Vegetarians II/07 - Demons of Pain.flac Infected Mushroom/Converting Vegetarians II/08 - Zoan Zound.flac Infected Mushroom/Converting Vegetarians II/09 - Blue Swan 5.flac Infected Mushroom/Converting Vegetarians II/10 - Fields of Grey (feat. Sasha Grey).flac Infected Mushroom/Converting Vegetarians II/11 - Leopold.flac Infected Mushroom/Converting Vegetarians II/12 - On the Road Again.flac Infected Mushroom/Converting Vegetarians II/13 - Stuck in a Loop.flac Infected Mushroom/Converting Vegetarians II/14 - Mexicali.flac Infected Mushroom/Converting Vegetarians II/15 - The Surgeon.flac Infected Mushroom/Legend of the Black Shawarma Infected Mushroom/Legend of the Black Shawarma/Infected Mushroom - Legend of the Black Shawarma - 01 - Poquito Mas.flac Infected Mushroom/Legend of the Black Shawarma/Infected Mushroom - Legend of the Black Shawarma - 02 - Sa\'eed.flac Infected Mushroom/Legend of the Black Shawarma/Infected Mushroom - Legend of the Black Shawarma - 03 - End of the Road.flac Infected Mushroom/Legend of the Black Shawarma/Infected Mushroom - Legend of the Black Shawarma - 04 - Smashing the Opponent (feat. Jonathan Davis).flac Infected Mushroom/Legend of the Black Shawarma/Infected Mushroom - Legend of the Black Shawarma - 05 - Can\'t Stop.flac Infected Mushroom/Legend of the Black Shawarma/Infected Mushroom - Legend of the Black Shawarma - 06 - Herbert the Pervert.flac Infected Mushroom/Legend of the Black Shawarma/Infected Mushroom - Legend of the Black Shawarma - 07 - Killing Time (feat. Perry Ferrell).flac Infected Mushroom/Legend of the Black Shawarma/Infected Mushroom - Legend of the Black Shawarma - 08 - Project 100.flac Infected Mushroom/Legend of the Black Shawarma/Infected Mushroom - Legend of the Black Shawarma - 09 - Franks.flac Infected Mushroom/Legend of the Black Shawarma/Infected Mushroom - Legend of the Black Shawarma - 10 - Slowly.flac Infected Mushroom/Legend of the Black Shawarma/Infected Mushroom - Legend of the Black Shawarma - 11 - The Legend of the Black Shawarma.flac Infected Mushroom/Legend of the Black Shawarma/Infected Mushroom - Legend of the Black Shawarma - 12 - Riders on the Storm (Infected Mushroom Remix).flac Infected Mushroom/Pink Nightmares Infected Mushroom/Pink Nightmares/01_Pink_Nightmares_Album_Mix.flac ' subst = '\\g<name>\\g<ext>' result = str.gsub(re, subst) # Print the result of the substitution puts 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 Ruby, please visit: http://ruby-doc.org/core-2.2.0/Regexp.html