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 = "\"PrivKey\": \"([a-zA-Z0-9_=\\/-\\\\+]+)"; final String string = " {\n" + " \"API\": {\n" + " \"HTTPHeaders\": {}\n" + " },\n" + " \"Addresses\": {\n" + " \"API\": \"/ip4/0.0.0.0/tcp/5001\",\n" + " \"Announce\": [],\n" + " \"Gateway\": \"/ip4/0.0.0.0/tcp/8080\",\n" + " \"NoAnnounce\": [],\n" + " \"Swarm\": [\n" + " \"/ip4/0.0.0.0/tcp/4001\",\n" + " \"/ip6/::/tcp/4001\"\n" + " ]\n" + " },\n" + " \"Bootstrap\": [\n" + " \"/dnsaddr/bootstrap.libp2p.io/ipfs/QmNnooDu7bfjPFoTZYxMNLWUQJyrVwtbZg5gBMjTezGAJN\",\n" + " \"/dnsaddr/bootstrap.libp2p.io/ipfs/QmQCU2EcMqAqQPR2i9bChDtGNJchTbq5TbXJJ16u19uLTa\",\n" + " \"/dnsaddr/bootstrap.libp2p.io/ipfs/QmbLHAnMoJPWSCR5Zhtx6BHJX9KiKNN6tpvbUcqanj75Nb\",\n" + " \"/dnsaddr/bootstrap.libp2p.io/ipfs/QmcZf59bWwK5XFi76CZX8cbJ4BhTzzA3gU1ZjYZcYW3dwt\",\n" + " \"/ip4/104.131.131.82/tcp/4001/ipfs/QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ\",\n" + " \"/ip4/104.236.179.241/tcp/4001/ipfs/QmSoLPppuBtQSGwKDZT2M73ULpjvfd3aZ6ha4oFGL1KrGM\",\n" + " \"/ip4/128.199.219.111/tcp/4001/ipfs/QmSoLSafTMBsPKadTEgaXctDQVcqN88CNLHXMkTNwMKPnu\",\n" + " \"/ip4/104.236.76.40/tcp/4001/ipfs/QmSoLV4Bbm51jM9C4gDYZQ9Cy3U6aXMJDAbzgu2fzaDs64\",\n" + " \"/ip4/178.62.158.247/tcp/4001/ipfs/QmSoLer265NRgSp2LA3dPaeykiS1J6DifTC88f5uVQKNAd\",\n" + " \"/ip6/2604:a880:1:20::203:d001/tcp/4001/ipfs/QmSoLPppuBtQSGwKDZT2M73ULpjvfd3aZ6ha4oFGL1KrGM\",\n" + " \"/ip6/2400:6180:0:d0::151:6001/tcp/4001/ipfs/QmSoLSafTMBsPKadTEgaXctDQVcqN88CNLHXMkTNwMKPnu\",\n" + " \"/ip6/2604:a880:800:10::4a:5001/tcp/4001/ipfs/QmSoLV4Bbm51jM9C4gDYZQ9Cy3U6aXMJDAbzgu2fzaDs64\",\n" + " \"/ip6/2a03:b0c0:0:1010::23:1001/tcp/4001/ipfs/QmSoLer265NRgSp2LA3dPaeykiS1J6DifTC88f5uVQKNAd\"\n" + " ],\n" + " \"Datastore\": {\n" + " \"BloomFilterSize\": 1048576,\n" + " \"GCPeriod\": \"1h\",\n" + " \"HashOnRead\": false,\n" + " \"Spec\": {\n" + " \"child\": {\n" + " \"path\": \"badgerds\",\n" + " \"syncWrites\": true,\n" + " \"truncate\": true,\n" + " \"type\": \"badgerds\"\n" + " },\n" + " \"prefix\": \"badger.datastore\",\n" + " \"type\": \"measure\"\n" + " },\n" + " \"StorageGCWatermark\": 90,\n" + " \"StorageMax\": \"100GB\"\n" + " },\n" + " \"Discovery\": {\n" + " \"MDNS\": {\n" + " \"Enabled\": true,\n" + " \"Interval\": 10\n" + " }\n" + " },\n" + " \"Experimental\": {\n" + " \"FilestoreEnabled\": false,\n" + " \"Libp2pStreamMounting\": false,\n" + " \"P2pHttpProxy\": false,\n" + " \"PreferTLS\": false,\n" + " \"QUIC\": false,\n" + " \"ShardingEnabled\": false,\n" + " \"UrlstoreEnabled\": false\n" + " },\n" + " \"Gateway\": {\n" + " \"APICommands\": [],\n" + " \"HTTPHeaders\": {\n" + " \"Access-Control-Allow-Headers\": [\n" + " \"X-Requested-With\",\n" + " \"Range\",\n" + " \"User-Agent\"\n" + " ],\n" + " \"Access-Control-Allow-Methods\": [\n" + " \"GET\"\n" + " ],\n" + " \"Access-Control-Allow-Origin\": [\n" + " \"*\"\n" + " ]\n" + " },\n" + " \"NoFetch\": false,\n" + " \"PathPrefixes\": [],\n" + " \"RootRedirect\": \"\",\n" + " \"Writable\": false\n" + " },\n" + " \"Identity\": {\n" + " \"PeerID\": \"Qme2joV44qiG38Q7YaEti6N1QMiM6XujXairxgJqJB9DeT\",\n" + " \"PrivKey\": \"CAASqQkwggSlAgEAAoIBAQDXaph7x+ZcxSXIFxHZ9JYHakDP/8mD33UPBY1KBJ7eLrhr8UA3Pc6gMB7sFUe7wVinij7rupXjprZ/eIMn5PpYaYeC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B6whNIWSAw+rcLndgL/N2PYxGw4B9hKdeJ1E++/IcCgYEA5kmxlgb6Z3VcZy3pCNDP6Jq5j16JC15GEwQVfuBxBYWclS+c5g+ciF0GDW6CG55cC77TaFAIN1Pu2yLOf7YRDET0Bsa23utK50C6a1Oh5FhYSA/bINxS5CynHeHed43DOlM09zEnI+P0tDGiexLvQVwI2e39gLmu2Awe7DFsYk0CgYAWH7ghRPFQyQGFkSNSBZKPIy0hUqLJnEF4ACmQaqpU5xFnH5ZF4euBQiM/nn73AtSVQtjKpI02rOTR9FQ51ySw57fYTzejGjJOM1+UHlKLRSJIMnOhuzdRrO+2WGYmPVvr/k1htOoSEE8e+N6ZxNZERs3i/f8igfcD6v51k5SkQA==\"\n" + " },\n" + " \"Ipns\": {\n" + " \"RecordLifetime\": \"\",\n" + " \"RepublishPeriod\": \"\",\n" + " \"ResolveCacheSize\": 128\n" + " },\n" + " \"Mounts\": {\n" + " \"FuseAllowOther\": false,\n" + " \"IPFS\": \"/ipfs\",\n" + " \"IPNS\": \"/ipns\"\n" + " },\n" + " \"Pubsub\": {\n" + " \"DisableSigning\": false,\n" + " \"Router\": \"\",\n" + " \"StrictSignatureVerification\": false\n" + " },\n" + " \"Reprovider\": {\n" + " \"Interval\": \"12h\",\n" + " \"Strategy\": \"all\"\n" + " },\n" + " \"Routing\": {\n" + " \"Type\": \"dht\"\n" + " },\n" + " \"Swarm\": {\n" + " \"AddrFilters\": null,\n" + " \"ConnMgr\": {\n" + " \"GracePeriod\": \"20s\",\n" + " \"HighWater\": 2000,\n" + " \"LowWater\": 600,\n" + " \"Type\": \"basic\"\n" + " },\n" + " \"DisableBandwidthMetrics\": false,\n" + " \"DisableNatPortMap\": false,\n" + " \"DisableRelay\": false,\n" + " \"EnableAutoNATService\": false,\n" + " \"EnableAutoRelay\": false,\n" + " \"EnableRelayHop\": false\n" + " }\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