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

/
/
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 = "^\\[([\\d]{1,2}-[\\w]{3,9}-[\\d]{4}\\s+[\\d]{2}:[\\d]{2}:[\\d]{2}\\s+[A-Z]{3,4})\\]\\s((?s).+?)(?=(?:^\\[.+\\])|\\Z)"; final String string = "[27-May-2021 05:59:39 UTC] Inpsyde\\Idealo\\WooCommerce\\IdealoException: Plugin \"woocommerce\" must be installed and active in C:\\Users\\xedin\\Local Sites\\idealo\\app\\public\\wp-content\\plugins\\idealo-for-woocommerce\\src\\Application.php:172\n" + "[27-May-2021 05:59:39 UTC] PHP Fatal error: Uncaught Error: Class 'ActionScheduler_Lock' not found in C:\\Users\\xedin\\Local Sites\\idealo\\app\\public\\wp-content\\plugins\\idealo-for-woocommerce\\vendor\\woocommerce\\action-scheduler\\classes\\abstracts\\ActionScheduler.php:29\n" + "Stack trace:\n" + "#0 C:\\Users\\xedin\\Local Sites\\idealo\\app\\public\\wp-content\\plugins\\idealo-for-woocommerce\\vendor\\woocommerce\\action-scheduler\\classes\\ActionScheduler_QueueRunner.php(103): ActionScheduler::lock()\n" + "#1 C:\\Users\\xedin\\Local Sites\\idealo\\app\\public\\wp-includes\\class-wp-hook.php(292): ActionScheduler_QueueRunner->maybe_dispatch_async_request('')\n" + "#2 C:\\Users\\xedin\\Local Sites\\idealo\\app\\public\\wp-includes\\class-wp-hook.php(316): WP_Hook->apply_filters(NULL, Array)\n" + "#3 C:\\Users\\xedin\\Local Sites\\idealo\\app\\public\\wp-includes\\plugin.php(484): WP_Hook->do_action(Array)\n" + "#4 C:\\Users\\xedin\\Local Sites\\idealo\\app\\public\\wp-includes\\load.php(1072): do_action('shutdown')\n" + "#5 [internal function]: shutdown_action_hook()\n" + "#6 {main}\n" + " thrown in C:\\Users\\xedin\\Local Sites\\idealo\\app\\public\\wp-content\\plugins\\idealo-for-woocommerce\\vendor\\woocommerce\\action-scheduler\\classes\\abstracts\\ActionScheduler.php on line 29\n" + "[27-May-2021 05:59:39 UTC] PHP Stack trace:\n" + "[27-May-2021 05:59:39 UTC] PHP 1. {main}() C:\\Users\\xedin\\Local Sites\\idealo\\app\\public\\wp-admin\\admin-ajax.php:0\n" + "[27-May-2021 05:59:39 UTC] PHP 2. do_action() C:\\Users\\xedin\\Local Sites\\idealo\\app\\public\\wp-admin\\admin-ajax.php:187\n" + "[27-May-2021 05:59:39 UTC] PHP 3. WP_Hook->do_action() C:\\Users\\xedin\\Local Sites\\idealo\\app\\public\\wp-includes\\plugin.php:484\n" + "[27-May-2021 05:59:39 UTC] PHP 4. WP_Hook->apply_filters() C:\\Users\\xedin\\Local Sites\\idealo\\app\\public\\wp-includes\\class-wp-hook.php:316\n" + "[27-May-2021 05:59:39 UTC] PHP 5. wp_ajax_delete_plugin() C:\\Users\\xedin\\Local Sites\\idealo\\app\\public\\wp-includes\\class-wp-hook.php:292\n" + "[27-May-2021 05:59:39 UTC] PHP 6. wp_send_json_success() C:\\Users\\xedin\\Local Sites\\idealo\\app\\public\\wp-admin\\includes\\ajax-actions.php:4591\n" + "[27-May-2021 05:59:39 UTC] PHP 7. wp_send_json() C:\\Users\\xedin\\Local Sites\\idealo\\app\\public\\wp-includes\\functions.php:4151\n" + "[27-May-2021 05:59:39 UTC] PHP 8. wp_die() C:\\Users\\xedin\\Local Sites\\idealo\\app\\public\\wp-includes\\functions.php:4125\n" + "[27-May-2021 05:59:39 UTC] PHP 9. _ajax_wp_die_handler() C:\\Users\\xedin\\Local Sites\\idealo\\app\\public\\wp-includes\\functions.php:3421\n" + "[27-May-2021 06:56:26 UTC] PHP Warning: mysqli_real_connect(): (HY000/2002): No connection could be made because the target machine actively refused it.\n" + " in C:\\Users\\xedin\\Local Sites\\idealo\\app\\public\\wp-includes\\wp-db.php on line 1653\n" + "[27-May-2021 06:56:27 UTC] PHP Stack trace:\n" + "[27-May-2021 06:56:27 UTC] PHP 1. {main}() C:\\Users\\xedin\\Local Sites\\idealo\\app\\public\\wp-cron.php:0\n" + "[27-May-2021 06:56:27 UTC] PHP 2. require_once() C:\\Users\\xedin\\Local Sites\\idealo\\app\\public\\wp-cron.php:44\n" + "[27-May-2021 06:56:27 UTC] PHP 3. require_once() C:\\Users\\xedin\\Local Sites\\idealo\\app\\public\\wp-load.php:37\n" + "[27-May-2021 06:56:27 UTC] PHP 4. require_once() C:\\Users\\xedin\\Local Sites\\idealo\\app\\public\\wp-config.php:80\n" + "[27-May-2021 06:56:27 UTC] PHP 5. require_wp_db() C:\\Users\\xedin\\Local Sites\\idealo\\app\\public\\wp-settings.php:124\n" + "[27-May-2021 06:56:27 UTC] PHP 6. wpdb->__construct() C:\\Users\\xedin\\Local Sites\\idealo\\app\\public\\wp-includes\\load.php:545\n" + "[27-May-2021 06:56:27 UTC] PHP 7. wpdb->db_connect() C:\\Users\\xedin\\Local Sites\\idealo\\app\\public\\wp-includes\\wp-db.php:632\n" + "[27-May-2021 06:56:27 UTC] PHP 8. mysqli_real_connect() C:\\Users\\xedin\\Local Sites\\idealo\\app\\public\\wp-includes\\wp-db.php:1653\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