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"require\('(.+?)'\)") test_str = ("'use strict';\n\n" "var path = require('path');\n" "var _ = require('lodash');\n" "var gulp = require('gulp');\n" "require('gulp-help')(gulp);\n" "var corejs = require('gulp-corejs');\n\n" "//region Build\n\n" "gulp.task('_bower_clean', function () {\n" " var bower = require('bower');\n" " return bower.commands.cache.clean(['corejs', 'pnotify', 'ui-bootstrap', 'emply-font', 'modernizr']);\n" "});\n\n" "gulp.task('bower', ['_bower_clean'], function () {\n" " var bower = require('gulp-bower');\n" " return bower({ cmd: 'update' })\n" " .pipe(gulp.dest('bower_components'));\n" "});\n\n" "gulp.task('compile', function (cb) {\n" " var buildConfig = {\n" " appDir: 'src',\n" " baseUrl: '.',\n" " paths: {\n" " text: '../bower_components/requirejs-text/text',\n" " html: '../bower_components/requirejs-html/html',\n\n" " jquery: 'empty:',\n" " angular: 'empty:',\n" " 'angular-cache': 'empty:',\n" " ngSanitize: 'empty:',\n" " pnotify: 'empty:',\n" " 'pnotify.modal': 'empty:',\n" " 'pnotify.confirm': 'empty:',\n" " 'pnotify.callbacks': 'empty:',\n" " 'pnotify.nonblock': 'empty:',\n" " 'ui.bootstrap': 'empty:',\n" " infiniteScroll: 'empty:',\n" " 'jquery.tabbable': 'empty:',\n" " cfpHotkeys: 'empty:',\n" " textile: 'empty:',\n" " Modernizr: 'empty:',\n" " 'tmh.dynamicLocale': 'empty:'\n" " },\n" " config: {\n" " html: {\n" " comments: 'strip',\n" " whitespaceBetweenTags: 'collapse',\n" " whitespaceBetweenTagsAndText: 'collapse',\n" " whitespaceWithinTags: 'collapse'\n" " }\n" " },\n" " dir: 'compiled',\n" " removeCombined: true,\n" " optimize: 'none',\n" " skipDirOptimize: true,\n" " fileExclusionRegExp: /\\.spec\\.js/,\n" " modules: [\n" " { name: 'cmHelpers/module', exclude: ['text', 'html'] },\n" " { name: 'cmMessages/module', exclude: ['text', 'html'] },\n" " { name: 'cmYaMetrika/module' },\n" " { name: 'cmFormPrimitives/module', exclude: ['text', 'html', 'cmHelpers/module'] },\n" " { name: 'cmGoogleAnalytics/module', exclude: ['text', 'html', 'cmMessages/module'] },\n" " {\n" " name: 'cmWikiHelp/module',\n" " exclude: ['html', 'cmHelpers/module', 'cmMessages/module', 'cmTranslate/module']\n" " },\n" " { name: 'cmMultiselect/module', exclude: ['text', 'html', 'cmFormPrimitives/module', 'cmHelpers/module'] },\n" " {\n" " name: 'cmSelectControls/module',\n" " exclude: [\n" " 'text',\n" " 'html',\n" " 'cmHelpers/module',\n" " 'cmFormPrimitives/module',\n" " 'cmWikiHelp/module',\n" " 'cmMessages/module',\n" " 'cmTranslate/module'\n" " ]\n" " },\n" " { name: 'cmTranslate/module', exclude: ['text', 'html', 'cmHelpers/module'] }\n" " ]\n" " };\n" " corejs.build.compile(buildConfig, cb);\n" " gulp.src('bower.json')\n" " .pipe(gulp.dest('./compiled'));\n" "});\n\n" "gulp.task('clean', function (cb) {\n" " require('del')(['compiled/**/*.html', 'dist'], function () {\n" " require('remove-empty-directories')('compiled');\n" " cb();\n" " });\n" "});\n\n" "gulp.task('zip', function () {\n" " var zip = require('gulp-zip');\n" " var bowerCfg = require(path.resolve('./bower.json'));\n" " var files = [\n" " 'compiled/**/*',\n" " '!./**/*.txt'\n" " ];\n\n" " gulp.src(files)\n" " .pipe(zip('corejs-' + bowerCfg.version + '.zip'))\n" " .pipe(gulp.dest('./dist'));\n\n" " gulp.src(files)\n" " .pipe(zip('corejs-latest.zip'))\n" " .pipe(gulp.dest('./dist'));\n" "});\n\n" "function build (cb, excluded) {\n" " var tasks = [\n" " 'bower',\n" " 'compile',\n" " 'clean',\n" " 'zip'\n" " ];\n" " tasks = tasks.concat([cb]);\n\n" " _.remove(tasks, function (task) {\n" " return _.includes(excluded, task);\n" " });\n" " require('run-sequence').apply(undefined, tasks);\n" "}\n\n" "gulp.task('build', 'Build production version (./dist folder)', function (cb) {\n" " build(cb);\n" "});\n\n" "gulp.task('fastBuild', 'Build production version (./dist folder)', function (cb) {\n" " build(cb, ['bower', 'compress']);\n" "});\n\n" "//endregion\n\n" "//region Developer tools\n\n" "gulp.task('bump', 'Increment version', function () {\n" " return corejs.bump(['package.json', 'bower.json'])\n" " .pipe(gulp.dest('./'));\n" "});\n\n" "gulp.task('styles', 'Compile CSS', function () {\n" " return corejs.styles(['demo/styles/index.less'])\n" " .pipe(gulp.dest('demo/styles'));\n" "});\n\n" "gulp.task('browsersync', function () {\n" " require('browser-sync').create().init({\n" " files: [\n" " '{src,demo}/**/*',\n" " '*.{json,js}'\n" " ],\n" " watchOptions: {\n" " ignoreInitial: true,\n" " ignored: '{src,demo}/**/*.less'\n" " },\n" " open: false,\n" " proxy: 'localhost:2000',\n" " port: 2015,\n" " logPrefix: 'BrowserSync',\n" " logConnections: true\n" " });\n" "});\n\n" "gulp.task('assemble', ['styles', 'i18n', 'packSvg']);\n\n" "gulp.task('watch', ['assemble'], function () {\n" " // styles\n" " corejs.watch(['src/**/*.less', 'demo/**/*.less'], ['styles']);\n\n" " // translates\n" " corejs.watch(['src/**/*.json', 'demo/**/*.json'], ['i18n']);\n\n" " // svg packs\n" " corejs.watch(['src/**/*.svg'], ['packSvg']);\n" "});\n\n" "gulp.task('live', ['browsersync', 'watch']);\n\n" "gulp.task('i18n', function () {\n" " var extend = require('gulp-extend');\n" " var locales = ['ru', 'en'];\n\n" " _.forEach(locales, function (locale) {\n" " var jsonName = locale + '.json';\n" " gulp.src(['src/**/' + jsonName, 'demo/**/' + jsonName])\n" " .pipe(extend(jsonName))\n" " .pipe(gulp.dest('i18n'));\n" " });\n" "});\n\n" "gulp.task('packSvg', function () {\n" " return corejs.packSvg(['src/**/*.svg'], 'pack.svg')\n" " .pipe(gulp.dest('demo/images/'));\n" "});\n" "//endregion\n\n" "//region Karma tests\n\n" "gulp.task('test', 'Run tests once and exit', function (done) {\n" " corejs.karma.test(done, 'karma.conf.js');\n" "});\n\n" "gulp.task('tdd', 'Run tests and start watching', function (done) {\n" " corejs.karma.tdd(done, 'karma.conf.js');\n" "});\n\n" "gulp.task('test:jenkins', 'Run tests once and build reports', function (done) {\n" " corejs.karma.jenkins(done, 'karma.conf.js');\n" "});\n\n" "//endregion\n\n" "//region Code style and lint\n\n" "var JS_FILES = ['src/**/*.js'];\n\n" "gulp.task('lint', 'Check JS syntax and show errors', function () {\n" " return corejs.lint.check(JS_FILES, 'jshint-stylish');\n" "});\n\n" "gulp.task('lint:storm', 'Check JS syntax and show errors', function () {\n" " return corejs.lint.check(JS_FILES, 'storm');\n" "});\n\n" "gulp.task('lint:jenkins', 'Check JS syntax and generate reports for jenkins', function () {\n" " return corejs.lint.jenkins(JS_FILES, 'reports/corejs-lint.xml');\n" "});\n\n" "gulp.task('jscs', 'Check JS code style', function () {\n" " return corejs.jscs.check(JS_FILES);\n" "});\n\n" "gulp.task('jscs:storm', 'Check JS code style', function () {\n" " return corejs.jscs.check(JS_FILES, 'storm');\n" "});\n\n" "gulp.task('jscs:jenkins', 'Check JS code style and generate report for jenkins', function () {\n" " return corejs.jscs.jenkins(JS_FILES, 'reports/corejs-codestyle.xml');\n" "});\n\n" "gulp.task('storm', 'Run JS check style and syntax and show short report', ['lint:storm', 'jscs:storm']);\n\n" "//endregion\n\n" "gulp.task('jenkins', 'Run all jenkins-specific tasks', ['lint:jenkins', 'test:jenkins', 'jscs:jenkins']);\n") matches = regex.finditer(test_str) for match_num, match in enumerate(matches, start=1): print(f"Match {match_num} 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