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
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
  • Match everything enclosed
    (?:...)
  • Capture everything enclosed
    (...)
  • 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

/
/
gm

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 = "[0-9]{3}[M]"; final String string = "[root@barpbli24149399 log]# ls -alhr | grep -E [\\d\\d\\dM]\n" + "-rw------- 1 root root 2.1K Nov 12 20:39 vmware-vmtoolsd-root.log\n" + "-rw------- 1 root root 719 Mar 11 2021 vmware-network.log\n" + "-rw------- 1 root root 719 Mar 11 2021 vmware-network.2.log\n" + "-rw------- 1 root root 741 Mar 11 2021 vmware-network.1.log\n" + "drwxr-xr-x. 2 root root 4.0K Dec 14 2020 tuned\n" + "-rw-r----- 1 root root 2.2K Mar 11 2021 tsy-install-hpsa.log\n" + "-rw-r--r-- 1 root root 184 Nov 12 20:39 TSM_CAD_-_BARPBLI24149399_FS.dsminfo\n" + "drwxrwxrwx 2 root root 4.0K Nov 28 06:51 tsm\n" + "-rw-------. 1 root root 16M Nov 28 09:10 tallylog\n" + "-rw------- 1 root root 139 Mar 16 2021 sudolog\n" + "drwxr-x---. 2 sssd sssd 4.0K Nov 13 03:08 sssd\n" + "-rw------- 1 root root 220 Mar 11 2021 serverprofile.log\n" + "-rw-r----- 1 root root 157K May 29 2022 seclog.log-20220529\n" + "-rw-r----- 1 root root 158K May 22 2022 seclog.log-20220522\n" + "-rw-r----- 1 root root 161K May 15 2022 seclog.log-20220515\n" + "-rw-r----- 1 root root 158K May 8 2022 seclog.log-20220508\n" + "-rw-r----- 1 root root 157K May 1 2022 seclog.log-20220501\n" + "-rw-r----- 1 root root 156K Mar 27 2022 seclog.log-20220326\n" + "-rw-r----- 1 root root 161K Mar 20 2022 seclog.log-20220319\n" + "-rw-r----- 1 root root 157K Mar 13 2022 seclog.log-20220312\n" + "-rw-r----- 1 root root 158K Mar 6 2022 seclog.log-20220305\n" + "-rw-r----- 1 root root 158K May 30 2021 seclog.log-20210529\n" + "-rw-r----- 1 root root 157K May 23 2021 seclog.log-20210522\n" + "-rw-r----- 1 root root 158K May 16 2021 seclog.log-20210515\n" + "-rw-r----- 1 root root 157K May 9 2021 seclog.log-20210508\n" + "-rw-r----- 1 root root 157K May 2 2021 seclog.log-20210501\n" + "-rw-r----- 1 root root 156K Mar 28 2021 seclog.log-20210326\n" + "-rw------- 1 root root 107K Mar 21 2021 seclog.log-20210319\n" + "drwx------. 3 root root 4.0K May 10 2022 samba\n" + "drwxr-xr-x. 2 root root 4.0K Nov 28 00:00 sa\n" + "drwxr-xr-x. 2 root root 4.0K Nov 27 03:19 rhsm\n" + "drwxr-xr-x 2 root root 4.0K Nov 12 20:31 rear\n" + "drwxr-xr-x 4 root root 4.0K Apr 16 2022 puppetlabs\n" + "drwxr-xr-x. 2 root root 4.0K Aug 17 2016 prelink\n" + "drwxr-xr-x 3 root root 4.0K Mar 11 2021 opsware\n" + "drwxr-xr-x. 2 ntp ntp 4.0K Jun 1 2020 ntpstats\n" + "-rw-r--r-- 1 root root 4.8K Mar 18 2021 ntp\n" + "-rw-r----- 1 root root 1.2M Nov 13 03:15 messages-20221113\n" + "-rw-r----- 1 root root 13M Aug 7 03:15 messages-20220807\n" + "-rw-r----- 1 root root 29M Jul 31 03:35 messages-20220731\n" + "-rw-r----- 1 root root 29M Jul 24 03:15 messages-20220724\n" + "-rw-r----- 1 root root 29M Jul 17 03:20 messages-20220717\n" + "-rw-r----- 1 root root 29M Jul 10 03:50 messages-20220710\n" + "-rw-r----- 1 root root 29M Jul 3 03:14 messages-20220703\n" + "-rw-r----- 1 root root 29M Jun 26 03:24 messages-20220626\n" + "-rw-r----- 1 root root 29M Jun 19 03:20 messages-20220619\n" + "-rw-r----- 1 root root 29M Jun 12 03:24 messages-20220612\n" + "-rw-r----- 1 root root 29M Jun 5 03:20 messages-20220605\n" + "-rw-r----- 1 root root 29M May 29 2022 messages-20220529\n" + "-rw-r----- 1 root root 29M May 22 2022 messages-20220522\n" + "-rw-r----- 1 root root 29M May 15 2022 messages-20220515\n" + "-rw-r----- 1 root root 29M May 8 2022 messages-20220508\n" + "-rw-r----- 1 root root 29M May 1 2022 messages-20220501\n" + "-rw-r----- 1 root root 29M Apr 24 2022 messages-20220424\n" + "-rw-r----- 1 root root 29M Apr 17 2022 messages-20220417\n" + "-rw-r----- 1 root root 29M Apr 10 2022 messages-20220410\n" + "-rw-r----- 1 root root 29M Apr 3 2022 messages-20220403\n" + "-rw-r----- 1 root root 29M Mar 27 2022 messages-20220327\n" + "-rw-r----- 1 root root 29M Mar 20 2022 messages-20220320\n" + "-rw-r----- 1 root root 29M Mar 13 2022 messages-20220313\n" + "-rw-r----- 1 root root 29M Mar 6 2022 messages-20220306\n" + "-rw-r----- 1 root root 29M Feb 27 2022 messages-20220227\n" + "-rw-r----- 1 root root 29M Feb 20 2022 messages-20220220\n" + "-rw-r----- 1 root root 29M Feb 13 2022 messages-20220213\n" + "-rw-r----- 1 root root 29M Feb 6 2022 messages-20220206\n" + "-rw-r----- 1 root root 29M Jan 30 2022 messages-20220130\n" + "-rw-r----- 1 root root 29M Jan 23 2022 messages-20220123\n" + "-rw-r----- 1 root root 29M Jan 16 2022 messages-20220116\n" + "-rw-r----- 1 root root 29M Jan 9 2022 messages-20220109\n" + "-rw-r----- 1 root root 29M Jan 2 2022 messages-20220102\n" + "-rw-r----- 1 root root 29M Dec 26 2021 messages-20211226\n" + "-rw-r----- 1 root root 29M Dec 19 2021 messages-20211219\n" + "-rw-r----- 1 root root 29M Dec 12 2021 messages-20211212\n" + "-rw-r----- 1 root root 29M Dec 5 2021 messages-20211205\n" + "-rw-r----- 1 root root 29M Nov 28 2021 messages-20211128\n" + "-rw-r----- 1 root root 29M Nov 21 2021 messages-20211121\n" + "-rw-r----- 1 root root 29M Nov 14 2021 messages-20211114\n" + "-rw-r----- 1 root root 29M Nov 7 2021 messages-20211107\n" + "-rw-r----- 1 root root 29M Oct 31 2021 messages-20211031\n" + "-rw-r----- 1 root root 29M Oct 24 2021 messages-20211024\n" + "-rw-r----- 1 root root 29M Oct 17 2021 messages-20211017\n" + "-rw-r----- 1 root root 29M Oct 10 2021 messages-20211010\n" + "-rw-r----- 1 root root 30M Oct 3 2021 messages-20211003\n" + "-rw-r----- 1 root root 31M Sep 26 2021 messages-20210926\n" + "-rw-r----- 1 root root 31M Sep 19 2021 messages-20210919\n" + "-rw-r----- 1 root root 30M Sep 12 2021 messages-20210912\n" + "-rw-r----- 1 root root 30M Sep 5 2021 messages-20210905\n" + "-rw-r----- 1 root root 30M Aug 29 2021 messages-20210829\n" + "-rw-r----- 1 root root 34M Aug 22 2021 messages-20210822\n" + "-rw-r----- 1 root root 34M Aug 15 2021 messages-20210815\n" + "-rw-r----- 1 root root 34M Aug 8 2021 messages-20210808\n" + "-rw-r----- 1 root root 34M Aug 1 2021 messages-20210801\n" + "-rw-r----- 1 root root 34M Jul 25 2021 messages-20210725\n" + "-rw-r----- 1 root root 34M Jul 18 2021 messages-20210718\n" + "-rw-r----- 1 root root 34M Jul 11 2021 messages-20210711\n" + "-rw-r----- 1 root root 34M Jul 4 2021 messages-20210704\n" + "-rw-r----- 1 root root 34M Jun 27 2021 messages-20210627\n" + "-rw-r----- 1 root root 34M Jun 20 2021 messages-20210620\n" + "-rw-r----- 1 root root 34M Jun 13 2021 messages-20210613\n" + "-rw-r----- 1 root root 32M Jun 6 2021 messages-20210606\n" + "-rw-r----- 1 root root 34M May 30 2021 messages-20210530\n" + "-rw-r----- 1 root root 34M May 23 2021 messages-20210523\n" + "-rw-r----- 1 root root 34M May 16 2021 messages-20210516\n" + "-rw-r----- 1 root root 34M May 9 2021 messages-20210509\n" + "-rw-r----- 1 root root 34M May 2 2021 messages-20210502\n" + "-rw-r----- 1 root root 34M Apr 25 2021 messages-20210425\n" + "-rw-r----- 1 root root 34M Apr 18 2021 messages-20210418\n" + "-rw-r----- 1 root root 34M Apr 11 2021 messages-20210411\n" + "-rw-r----- 1 root root 34M Apr 4 2021 messages-20210404\n" + "-rw-r----- 1 root root 34M Mar 28 2021 messages-20210328\n" + "-rw-r----- 1 root root 14M Mar 21 2021 messages-20210321\n" + "-rw-------. 1 root root 669K Mar 14 2021 messages-20210314\n" + "-rw-r--r--. 1 root root 140M Nov 28 09:11 lastlog\n" + "-rw------- 1 root root 106K Mar 18 2021 hawkey.log-20210321\n" + "-rw-r--r--. 1 root root 80K Mar 14 2021 hawkey.log-20210314\n" + "-rw------- 1 root root 0 Mar 21 2021 hawkey.log\n" + "-rw-r--r--. 1 root root 193 Dec 18 2020 grubby_prune_debug\n" + "-rw------- 1 root root 6.0K Mar 18 2021 dnf.rpm.log-20210321\n" + "-rw-r--r--. 1 root root 126K Mar 14 2021 dnf.rpm.log-20210314\n" + "-rw------- 1 root root 0 Mar 21 2021 dnf.rpm.log\n" + "-rw------- 1 root root 175K Mar 18 2021 dnf.log-20210321\n" + "-rw-r--r--. 1 root root 418K Mar 14 2021 dnf.log-20210314\n" + "-rw------- 1 root root 0 Mar 21 2021 dnf.log\n" + "-rw------- 1 root root 727K Mar 18 2021 dnf.librepo.log-20210321\n" + "-rw-r--r--. 1 root root 789K Mar 14 2021 dnf.librepo.log-20210314\n" + "-rw------- 1 root root 0 Mar 21 2021 dnf.librepo.log\n" + "-rw-r--r-- 1 root root 116K Nov 12 20:35 dmesg.old\n" + "-rw-r--r-- 1 root root 116K Nov 12 20:39 dmesg\n" + "-rw------- 1 root root 19M Nov 28 09:10 cron\n" + "drwxr-xr-x. 2 chrony chrony 4.0K Jan 10 2019 chrony\n" + "-rw------- 1 root root 21K Mar 19 2021 boot.log-20210319\n" + "-rw------- 1 root root 41K Mar 18 2021 boot.log-20210318\n" + "-rw------- 1 root root 42K Mar 12 2021 boot.log-20210312\n" + "drwx------. 2 root root 4.0K Nov 4 18:00 audit\n" + "drwxr-xr-x. 21 root root 4.0K Mar 11 2021 ..\n"; final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE); 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