Java Regular Expressions Cheat Sheet: A Handy Reference Guide with Examples

Regular expressions (regex) are powerful tools for pattern matching and manipulating text in Java. They provide a concise and flexible way to search, extract, and manipulate strings. Here, we will provide a Java regular expressions cheat sheet that includes essential regex patterns and their usage examples. Whether you are a beginner or an experienced developer, this cheat sheet will serve as a handy reference to help you master regular expressions in Java.

  1. Matching a Specific Pattern:
  • To match a specific word in a string:
   String regex = "\\bword\\b";
   String input = "This is a word in a sentence.";
   boolean match = input.matches(regex);
   // match = true
  • To match a sequence of digits:
   String regex = "\\d+";
   String input = "There are 123 apples.";
   Pattern pattern = Pattern.compile(regex);
   Matcher matcher = pattern.matcher(input);
   while (matcher.find()) {
       System.out.println(matcher.group());
   }
   // Output: 123
  1. Character Classes and Quantifiers:
  • To match a single digit:
   String regex = "\\d";
   String input = "I am 5 years old.";
   Pattern pattern = Pattern.compile(regex);
   Matcher matcher = pattern.matcher(input);
   while (matcher.find()) {
       System.out.println(matcher.group());
   }
   // Output: 5
  • To match any lowercase letter:
   String regex = "[a-z]";
   String input = "Java is Fun!";
   Pattern pattern = Pattern.compile(regex);
   Matcher matcher = pattern.matcher(input);
   while (matcher.find()) {
       System.out.println(matcher.group());
   }
   // Output: a i s u n
  1. Anchors and Boundary Matchers:
  • To match a word at the beginning of a string:
   String regex = "^Java";
   String input = "Java is a popular programming language.";
   boolean match = input.matches(regex);
   // match = true
  • To match a word at the end of a string:
   String regex = "language\\.$";
   String input = "Java is a popular programming language.";
   boolean match = input.matches(regex);
   // match = true
  1. Grouping and Capturing:
  • To extract a date in the format “dd/mm/yyyy”:
   String regex = "(\\d{2})/(\\d{2})/(\\d{4})";
   String input = "Date: 01/05/2023";
   Pattern pattern = Pattern.compile(regex);
   Matcher matcher = pattern.matcher(input);
   if (matcher.find()) {
       String day = matcher.group(1);
       String month = matcher.group(2);
       String year = matcher.group(3);
       System.out.println("Day: " + day);
       System.out.println("Month: " + month);
       System.out.println("Year: " + year);
   }
   // Output:
   // Day: 01
   // Month: 05
   // Year: 2023
  1. Matching Specific Characters:
  • To match a string containing only uppercase letters:
   String regex = "^[A-Z]+$";
   String input = "HELLO";
   boolean match = input.matches(regex);
   // match = true
  • To match a string containing any special characters:
   String regex = "[!@#$%^&*()]";
   String input = "Password@123";
   Pattern pattern = Pattern.compile(regex);
   Matcher matcher = pattern.matcher(input);
   while (matcher.find()) {
       System.out.println(matcher.group());
   }
   // Output: @
  1. Quantifiers:
  • To match a string with at least 3 lowercase letters:
   String regex = "[a-z]{3,}";
   String input = "The quick brown fox jumps";
   Pattern pattern = Pattern.compile(regex);
   Matcher matcher = pattern.matcher(input);
   while (matcher.find()) {
       System.out.println(matcher.group());
   }
   // Output: the, quick, brown, fox, jumps
  • To match a string with exactly 5 alphanumeric characters:
   String regex = "\\b\\w{5}\\b";
   String input = "Hello world, Java is cool!";
   Pattern pattern = Pattern.compile(regex);
   Matcher matcher = pattern.matcher(input);
   while (matcher.find()) {
       System.out.println(matcher.group());
   }
   // Output: Hello, world, Java, cool
  1. Escaping Special Characters:
  • To match a string containing a dot (.) character:
   String regex = "\\.";
   String input = "The file extension is .txt";
   Pattern pattern = Pattern.compile(regex);
   Matcher matcher = pattern.matcher(input);
   while (matcher.find()) {
       System.out.println(matcher.group());
   }
   // Output: .
  • To match a string containing a dollar ($) sign:
   String regex = "\\$";
   String input = "The price is $19.99";
   Pattern pattern = Pattern.compile(regex);
   Matcher matcher = pattern.matcher(input);
   while (matcher.find()) {
       System.out.println(matcher.group());
   }
   // Output: $
  1. Optional Characters:
  • To match a string with or without a prefix “Mr.” or “Mrs.”:
   String regex = "(Mr\\.|Mrs\\.)?\\s?[A-Za-z]+";
   String input = "Mr. John, Mrs. Smith, Jane";
   Pattern pattern = Pattern.compile(regex);
   Matcher matcher = pattern.matcher(input);
   while (matcher.find()) {
       System.out.println(matcher.group());
   }
   // Output: Mr. John, Mrs. Smith, Jane
  1. Lookahead and Lookbehind:
  • To match a string followed by a specific word:
   String regex = "\\w+(?= is cool)";
   String input = "Java is cool, Python is cool too!";
   Pattern pattern = Pattern.compile(regex);
   Matcher matcher = pattern.matcher(input);
   while (matcher.find()) {
       System.out.println(matcher.group());
   }
   // Output: Java, Python
  • To match a string preceded by a specific word:
   String regex = "(?<=Hello, )\\w+";
   String input = "Hello, John. Hello, Jane.";
   Pattern pattern = Pattern.compile(regex);
   Matcher matcher = pattern.matcher(input);
   while (matcher.find()) {
       System.out.println(matcher.group());
   }
   // Output: John, Jane
  1. Splitting a String:
  • To split a string using commas as the delimiter:
   String regex = ",";
   String input = "Apple,Orange,Mango,Banana";
   String[] splitArray = input.split(regex);
   for (String element : splitArray) {
       System.out.println(element);
   }
   // Output: Apple, Orange, Mango, Banana

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top