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",\"(?<date>\d\d\d\d-\d\d-\d\d)\ (?<time>\d\d:\d\d:\d\d).*\[(?<jail>sshd|recidive|mysqld-auth)\]\ (?<action>[a-zA-z]*)\ (?<ip_address>[\d\.]*)", flags=re.MULTILINE) test_str = ("@timestamp,@message\n" "2021-04-30 18:17:08.504,\"2021-04-30 19:17:04,189 fail2ban.filter [100432]: INFO [sshd] Found 221.181.185.223 - 2021-04-30 19:17:03\"\n" "2021-04-30 18:11:24.504,\"2021-04-30 19:11:20,137 fail2ban.filter [100432]: INFO [sshd] Found 221.181.185.198 - 2021-04-30 19:11:19\"\n" "2021-04-30 18:04:24.504,\"2021-04-30 19:04:19,434 fail2ban.filter [100432]: INFO [sshd] Found 221.131.165.56 - 2021-04-30 19:04:19\"\n" "2021-04-30 18:03:04.504,\"2021-04-30 19:02:59,705 fail2ban.filter [100432]: INFO [sshd] Found 213.171.212.141 - 2021-04-30 19:02:59\"\n" "2021-04-30 17:58:11.504,\"2021-04-30 18:58:06,901 fail2ban.filter [100432]: INFO [recidive] Found 205.185.119.236 - 2021-04-30 18:58:06\"\n" "2021-04-30 17:58:07.132,\"2021-04-30 18:58:06,628 fail2ban.actions [100432]: NOTICE [sshd] Ban 205.185.119.236\"\n" "2021-04-30 17:58:06.631,\"2021-04-30 18:58:06,208 fail2ban.filter [100432]: INFO [sshd] Found 205.185.119.236 - 2021-04-30 18:58:05\"\n" "2021-04-30 17:58:06.381,\"2021-04-30 18:58:06,206 fail2ban.filter [100432]: INFO [sshd] Found 205.185.119.236 - 2021-04-30 18:58:05\"\n" "2021-04-30 17:58:06.381,\"2021-04-30 18:58:06,206 fail2ban.filter [100432]: INFO [sshd] Found 205.185.119.236 - 2021-04-30 18:58:05\"\n" "2021-04-30 17:58:06.381,\"2021-04-30 18:58:06,207 fail2ban.filter [100432]: INFO [sshd] Found 205.185.119.236 - 2021-04-30 18:58:05\"\n" "2021-04-30 17:58:06.381,\"2021-04-30 18:58:06,207 fail2ban.filter [100432]: INFO [sshd] Found 205.185.119.236 - 2021-04-30 18:58:05\"\n" "2021-04-30 17:58:06.380,\"2021-04-30 18:58:06,205 fail2ban.filter [100432]: INFO [sshd] Found 205.185.119.236 - 2021-04-30 18:58:05\"\n" "2021-04-30 17:57:40.504,\"2021-04-30 18:57:35,482 fail2ban.filter [100432]: INFO [sshd] Found 221.181.185.143 - 2021-04-30 18:57:35\"\n" "2021-04-30 17:41:27.504,\"2021-04-30 18:41:23,069 fail2ban.filter [100432]: INFO [sshd] Found 221.181.185.135 - 2021-04-30 18:41:22\"\n" "2021-04-30 17:40:09.504,\"2021-04-30 18:40:05,206 fail2ban.filter [100432]: INFO [sshd] Found 222.187.239.107 - 2021-04-30 18:40:04\"\n" "2021-04-30 17:38:16.504,\"2021-04-30 18:38:11,847 fail2ban.filter [100432]: INFO [sshd] Found 221.181.185.151 - 2021-04-30 18:38:11\"") 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