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

/
/
gi

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 = "(Port of Registry\\s*:\\s*[a-z]{1,}\\s*)"; final String string = "ATTN : SNP TEAM\n" + " \n" + "Kindly be noted that recent deal was just failed due to late sub lifting by the sellers. Therefore still we can\n" + "develop for sale the following panamax bulk carrier from very close bank:-\n" + "(sellers are ‘Shinhan Capital’ and I have done many deals with them since last 10 years.)\n" + " \n" + "M/V DAEBO TRADER\n" + "==============\n" + "Type : Bulk Carrier\n" + "IMO NO. : 9230153\n" + "Flag : KOREA\n" + "Port of Registry : JEJU\n" + "Class : KR\n" + "Built : 2002 / Namura Shipbuilding, Japan\n" + "DWT : 73,870 mt on 13.962 m Draft\n" + "GRT / NRT : 39,056 / 25,246\n" + "LOA : 224.89 m\n" + "LBP : 215.00 m\n" + "Draught : 13.95 m\n" + "Beam : 32.20 m\n" + "M/E : MAN B&W 7S50MC-C8.1\n" + "Grain Capacity : 89,000 cu.m\n" + "Side-rolling hatches, Hydraulically operated, 7HO/7HA\n" + " \n" + "(All details are given in good faith but without guarantee.)\n" + " \n" + "She is laid up in Yeosu/Korea.\n" + "Holding fuller details and it is at your disposal.\n" + "Inviting best offers.\n" + " \n" + "Thanks & Best Regards\n" + "Danny, Park\n" + "설명: 설명: 설명: 훼어브릿지로고(영문)가로\n" + "FAIRBRIDGE GLOBAL INC.\n" + "Tel : +82 2 553 5190\n" + "Dir : +82 2 722 5191\n" + "Mob: +82 10 5299 1126\n" + "MSN: dannypark78@hotmail.com\n" + "Mail: snp@fairbridge.co.kr"; final Pattern pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE); 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