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

/
/
gs

Test String

Code Generator

Generated Code

$re = '/{.+|(?<=var)\s+([^\s]+)\s*=\s*[\'"]([^\'"]+?)[\'"];/s'; $str = '//stuff var RootJsURL = "/trunk/js/portal"; //The root of the OpenMap Application var RootRestUrl = "/trunk/rest"; //root of the restful services directory var stripePublicKey = \'pk_test_rAYTGcpiFtrXvXpYic4C0Mt0\';//the public key for your stripe account //global functions for any tool to use var showErrorPopup = function(title, message) { Ext.MessageBox.show({ title: title, msg: "Error: " + message, buttons: Ext.MessageBox.OK, icon: Ext.Msg.ERROR }); }; /** * checks if there is an error, if so display it * @param obj the response json to check for an error * @param title the title of the error pop up box * @returns {boolean} true if there is an error, false otherwise */ var checkResponseForError = function(obj, title) { if (obj.error) { if (obj.loggedIn) { showErrorPopup(title, obj.error); return true; } else { countdown.modal.show(); } } return false; }; //The configure object that puts it all together var Configure = { LBAR:[], TBAR:[], BBAR:[], RBAR:[], centerbar:null, /** * List of window resize functions */ ResizeEvents:[], /** * Gets the working size of the main panel * @returns {number} */ GetClientHeight:function(){ //var body = document.body; //var html = document.documentElement; var bannerHeight = document.getElementById("bannerimg").height; //return Math.max(body.scrollHeight, body.offsetHeight,html.clientHeight, html.scrollHeight, html.offsetHeight)-bannerHeight-5;//Buffer of 5px --> need to fix and make it universal return Ext.getBody().getViewSize().height -bannerHeight-5; }, /** * Configure all global variables */ Globals:function(){ //Create the dynamic content panel this.centerbar = Ext.create(\'Ext.panel.Panel\',{ region:\'center\', autoScroll:true }); }, /** * Assembles all tools into the main panel before drawing */ MainPanel:function(){ //If LBar and BBar are empty, don\'t bother rendering them --> set to null var topArray = []; var bottomArray = []; if(this.TBAR.length >0){ topArray = topArray.concat(this.TBAR); } if(this.BBAR.length >0){ bottomArray = bottomArray.concat(this.BBAR); } //Top bar --> Persistant tools var TopBar = Ext.create(\'Ext.panel.Panel\',{ region: \'north\', width:\'100%\', layout:{ type:\'hbox\', align: \'left\', pack: \'end\' }, margin: \'0 0 0 0\', items: topArray }); //Bottom bar --> usually pertinant info var BottomBar = Ext.create(\'Ext.panel.Panel\',{ region: \'south\', width:\'100%\', layout:\'hbox\', margin: \'0 0 0 0\', items: bottomArray }); //Side tool panel --> Extensible tools var toolbar = Ext.create(\'Ext.panel.Panel\',{ region: \'west\', width: 200, minWidth: 200, maxWidth: 400, collapsible: false, animCollapse:true, hideCollapseTool: true, floatable: true, split: false, margin: \'0 0 0 0\', layout:{ type: \'vbox\' }, items:this.LBAR }); //Right tool panel --> Extensible tools var Rightbar = Ext.create(\'Ext.panel.Panel\',{ region: \'east\', title:\'Portal Information\', width: 200, minWidth: 200, maxWidth: 400, collapsible: true, animCollapse:true, hideCollapseTool: true, floatable: true, split: false, margin: \'0 0 0 0\', layout:{ type: \'fit\' }, items:this.RBAR }); if(!Rightbar.getCollapsed()){ Rightbar.toggleCollapse(); } var cb = this.centerbar; //Overall Panel --> Contains everything MainPanel = Ext.create(\'Ext.panel.Panel\',{ layout: { type:\'border\', padding: \'0 0 0 0\', margin: \'0 0 0 0\' }, width: \'100%\', height:Configure.GetClientHeight(),//Buffer of 3px --> need to fix and make it universal items:[toolbar,cb,TopBar,BottomBar,Rightbar], renderTo: Ext.getBody() }); //Window resize event Ext.EventManager.onWindowResize(function () { var width = \'100%\'; var height = Configure.GetClientHeight(); MainPanel.setSize(width, height); for(var i = 0; i< Configure.ResizeEvents.length;i++){ Configure.ResizeEvents[i](); } }); }, /** * Adds an element to the left hand side of the page * @param {Array} elements * @return void */ AddToLeftbar:function(elements){ this.LBAR = this.LBAR.concat(elements); }, /** * Adds an element to the top of the page * @param {Array} elements * @return void */ AddToTopbar:function(elements){ this.TBAR = this.TBAR.concat(elements); }, /** * Adds an element to the bottom of the page * @param {Array} elements * @return void */ AddToBottombar:function(elements){ this.BBAR = this.BBAR.concat(elements); }, /** * Adds an element to the right hand side of the page * @params {Array} elements * @return void */ AddToRightbar:function(elements){ this.RBAR = this.RBAR.concat(elements); }, /** * Replaces the center panel content * @params {Ext.Component} element * @return void */ UpdateContent:function(element){ WindowManager.Clear(); this.centerbar.removeAll(false); this.centerbar.add(element); }, /** * Adds a window resize event * @params {function} func * @return void */ AddResizeEvent:function(func){ this.ResizeEvents = this.ResizeEvents.concat(func); } } '; preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0); // Print the entire match result var_dump($matches);

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 PHP, please visit: http://php.net/manual/en/ref.pcre.php