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

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

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 Java, please visit: https://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html