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

/
/
gmiuJ

Test String

Code Generator

Generated Code

# coding=utf8 # the above tag defines encoding for this document and is for Python 2.x compatibility import re regex = r"(class+)=[\"']?((?:.(?![\"']?\s+(?:\S+)=|[>\"']))+.)[\"']?" test_str = ("<div>\n" " @if($beforeTableSlot)\n" " <div class=\"mt-8\">\n" " @include($beforeTableSlot)\n" " </div>\n" " @endif\n" " <div class=\"relative\">\n" " <div class=\"flex justify-between items-center mb-1\">\n" " <div class=\"flex-grow h-10 flex items-center\">\n" " @if($this->searchableColumns()->count())\n" " <div class=\"w-96 flex rounded-lg shadow-sm\">\n" " <div class=\"relative flex-grow focus-within:z-10\">\n" " <div class=\"absolute inset-y-0 left-0 pl-3 flex items-center pointer-events-none\">\n" " <svg class=\"h-5 w-5 text-gray-400\" viewBox=\"0 0 20 20\" stroke=\"currentColor\" fill=\"none\">\n" " <path stroke-linecap=\"round\" stroke-linejoin=\"round\" stroke-width=\"2\" d=\"M21 21l-6-6m2-5a7 7 0 11-14 0 7 7 0 0114 0z\" />\n" " </svg>\n" " </div>\n" " <input wire:model.debounce.500ms=\"search\" class=\"w-full pl-10 py-3 text-sm leading-4 block rounded-md border-gray-300 shadow-sm focus:border-blue-300 focus:ring focus:ring-blue-200 focus:ring-opacity-50 focus:outline-none\" placeholder=\"{{__('Search in')}} {{ $this->searchableColumns()->map->label->join(', ') }}\" type=\"text\" />\n" " <div class=\"absolute inset-y-0 right-0 pr-2 flex items-center\">\n" " <button wire:click=\"$set('search', null)\" class=\"text-gray-300 hover:text-red-600 focus:outline-none\">\n" " <x-icons.x-circle class=\"h-5 w-5 stroke-current\" />\n" " </button>\n" " </div>\n" " </div>\n" " </div>\n" " @endif\n" " </div>\n\n" " <div class=\"flex items-center space-x-1\">\n" " <x-icons.cog wire:loading class=\"h-9 w-9 animate-spin text-gray-400\" />\n\n" " @if($exportable)\n" " <div x-data=\"{ init() {\n" " window.livewire.on('startDownload', link => window.open(link,'_blank'))\n" " } }\" x-init=\"init\">\n" " <button wire:click=\"export\" class=\"flex items-center space-x-2 px-3 border border-green-400 rounded-md bg-white text-green-500 text-xs leading-4 font-medium uppercase tracking-wider hover:bg-green-200 focus:outline-none\"><span>{{ __('Export') }}</span>\n" " <x-icons.excel class=\"m-2\" /></button>\n" " </div>\n" " @endif\n\n" " @if($hideable === 'select')\n" " @include('datatables::hide-column-multiselect')\n" " @endif\n" " </div>\n" " </div>\n\n" " @if($hideable === 'buttons')\n" " <div class=\"p-2 grid grid-cols-8 gap-2\">\n" " @foreach($this->columns as $index => $column)\n" " <button wire:click.prefetch=\"toggle('{{ $index }}')\" class=\"px-3 py-2 rounded text-white text-xs focus:outline-none\n" " {{ $column['hidden'] ? 'bg-blue-100 hover:bg-blue-300 text-blue-600' : 'bg-blue-500 hover:bg-blue-800' }}\">\n" " {{ $column['label'] }}\n" " </button>\n" " @endforeach\n" " </div>\n" " @endif\n\n" " <div class=\"rounded-lg shadow-lg bg-white max-w-screen overflow-auto\">\n" " <div class=\"rounded-lg @unless($this->hidePagination) rounded-b-none @endif\">\n" " <div class=\"table align-middle min-w-full\">\n" " @unless($this->hideHeader)\n" " <div class=\"table-row divide-x divide-gray-200\">\n" " @foreach($this->columns as $index => $column)\n" " @if($hideable === 'inline')\n" " @include('datatables::header-inline-hide', ['column' => $column, 'sort' => $sort])\n" " @elseif($column['type'] === 'checkbox')\n" " @unless($column['hidden'])\n" " <div class=\"w-32 py-4 flex justify-center overflow-hidden align-top px-6 border-b border-gray-200 bg-gray-50 text-left text-xs leading-4 font-medium text-gray-500 uppercase tracking-wider focus:outline-none\">\n" " <div class=\"px-3 py-1 rounded @if(count($selected)) bg-orange-400 @else bg-gray-200 @endif text-white text-center\">\n" " {{ count($selected) }}\n" " </div>\n" " </div>\n" " @endunless\n" " @else\n" " @include('datatables::header-no-hide', ['column' => $column, 'sort' => $sort])\n" " @endif\n" " @endforeach\n" " </div>\n\n" " <div class=\"table-row divide-x divide-blue-200 bg-blue-100\">\n" " @foreach($this->columns as $index => $column)\n" " @if($column['hidden'])\n" " @if($hideable === 'inline')\n" " <div class=\"table-cell w-5 overflow-hidden align-top bg-blue-100\"></div>\n" " @endif\n" " @elseif($column['type'] === 'checkbox')\n" " <div class=\"overflow-hidden align-top bg-blue-100 px-6 py-5 border-b border-gray-200 text-left text-xs leading-4 font-medium text-gray-500 uppercase tracking-wider flex h-full flex-col items-center space-y-2 focus:outline-none\">\n" " <div>SELECT ALL</div>\n" " <div>\n" " <input type=\"checkbox\" wire:click=\"toggleSelectAll\" @if(count($selected) === $this->results->total()) checked @endif class=\"form-checkbox mt-1 h-4 w-4 text-blue-600 transition duration-150 ease-in-out\" />\n" " </div>\n" " </div>\n" " @else\n" " <div class=\"table-cell overflow-hidden align-top\">\n" " @isset($column['filterable'])\n" " @if( is_iterable($column['filterable']) )\n" " <div wire:key=\"{{ $index }}\">\n" " @include('datatables::filters.select', ['index' => $index, 'name' => $column['label'], 'options' => $column['filterable']])\n" " </div>\n" " @else\n" " <div wire:key=\"{{ $index }}\">\n" " @include('datatables::filters.' . ($column['filterView'] ?? $column['type']), ['index' => $index, 'name' => $column['label']])\n" " </div>\n" " @endif\n" " @endisset\n" " </div>\n" " @endif\n" " @endforeach\n" " </div>\n" " @endif\n" " @forelse($this->results as $result)\n" " <div class=\"table-row p-1 divide-x divide-gray-100 {{ isset($result->checkbox_attribute) && in_array($result->checkbox_attribute, $selected) ? 'bg-orange-100' : ($loop->even ? 'bg-gray-100' : 'bg-gray-50') }}\">\n" " @foreach($this->columns as $column)\n" " @if($column['hidden'])\n" " @if($hideable === 'inline')\n" " <div class=\"table-cell w-5 overflow-hidden align-top\"></div>\n" " @endif\n" " @elseif($column['type'] === 'checkbox')\n" " @include('datatables::checkbox', ['value' => $result->checkbox_attribute])\n" " @else\n" " <div class=\"px-6 py-2 whitespace-no-wrap text-sm leading-5 text-gray-900 table-cell @if($column['align'] === 'right') text-right @elseif($column['align'] === 'center') text-center @else text-left @endif\">\n" " {!! $result->{$column['name']} !!}\n" " </div>\n" " @endif\n" " @endforeach\n" " </div>\n" " @empty\n" " <p class=\"p-3 text-lg text-teal-600\">\n" " {{ __(\"There's Nothing to show at the moment\") }}\n" " </p>\n" " @endforelse\n" " </div>\n" " </div>\n" " @unless($this->hidePagination)\n" " <div class=\"rounded-lg rounded-t-none max-w-screen border-b border-gray-200 bg-white\">\n" " <div class=\"p-2 sm:flex items-center justify-between\">\n" " {{-- check if there is any data --}}\n" " @if(count($this->results))\n" " <div class=\"px-8 my-2 sm:my-0 flex items-center\">\n" " <select name=\"perPage\" class=\"custom-select custom-select-sm form-control form-control-sm form-select block w-full leading-6 focus:outline-none\" wire:model=\"perPage\">\n" " @foreach(config('livewire-datatables.per_page_options', [ 10, 25, 50, 100 ]) as $per_page_option)\n" " <option value=\"{{ $per_page_option }}\">{{ $per_page_option }}</option>\n" " @endforeach\n" " <option value=\"99999999\">{{__('All')}}</option>\n" " </select>\n" " </div>\n\n" " <div class=\"my-4 sm:my-0\">\n" " <div class=\"lg:hidden\">\n" " <span class=\"space-x-2\">{{ $this->results->links('datatables::tailwind-simple-pagination') }}</span>\n" " </div>\n" " <div class=\"hidden lg:flex justify-center\">\n" " <span>{{ $this->results->links('datatables::tailwind-pagination') }}</span>\n" " </div>\n" " </div>\n\n" " <div class=\"flex justify-end text-gray-600\">\n" " {{__('Results')}} {{ $this->results->firstItem() }} - {{ $this->results->lastItem() }} {{__('of')}}\n" " {{ $this->results->total() }}\n" " </div>\n" " @endif\n" " </div>\n" " </div>\n" " @endif\n" " </div>\n" " </div>\n" " @if($afterTableSlot)\n" " <div class=\"mt-8\">\n" " @include($afterTableSlot)\n" " </div>\n" " @endif\n" "</div>\n") matches = re.finditer(regex, test_str, re.MULTILINE | re.IGNORECASE | re.UNICODE) for matchNum, match in enumerate(matches, start=1): print ("Match {matchNum} was found at {start}-{end}: {match}".format(matchNum = matchNum, start = match.start(), end = match.end(), match = match.group())) for groupNum in range(0, len(match.groups())): groupNum = groupNum + 1 print ("Group {groupNum} found at {start}-{end}: {group}".format(groupNum = groupNum, start = match.start(groupNum), end = match.end(groupNum), group = match.group(groupNum))) # Note: for Python 2.7 compatibility, use ur"" to prefix the regex and u"" to prefix the test string and substitution.

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