Sunday, May 11, 2014
This article provides an introduction to usage of regular expressions in Java with some examples. Regular Expression or Regex in the computing terms refers to a sequence of symbols and characters expressing a string or pattern to be searched within a longer piece of text. There is a different syntax to regular expressions beyond the Java programming language. It is essential to understand the basics of regular expressions clearly to understand or create complex regular expressions. Regular Expression syntax in Java is more similar to the one used with Perl language.

Regex Constructs

In general regular expression include characters, character classes and quantifiers. The table below provides a reference to most common Regex constructs taken from the Java reference material.
xThe character x
\\The backslash character
\tThe tab character
\nThe newline character
\rThe carriage-return character
Character Classes
[abc]a, b, or c (simple class)
[^abc]Any character except a, b, or c (negation)
[a-zA-Z]a through z or A through Z, inclusive (range)
Predefined character classes
.Any character (may or may not match line terminators)
\dA digit: [0-9]
\sA whitespace character: [ \t\n\x0B\f\r]
\SA non-whitespace character: [^\s]
\wA word character: [a-zA-Z_0-9]
\WA non-word character: [^\w]
Boundary Matchers
^The beginning of a line
$The end of a line
\bA word boundary
\BA non-word boundary
X?X, once or not at all
X*X, zero or more times
X+X, one or more times
X{n}X, exactly n times
X{n,}X, at least n times
X{n,m}X, at least n but not more than m times

Regex Java APIs

Java supports java.util.regex for regular expressions processing. 
  1. Primarily there are 2 classes Pattern and Matcher. 
  2. Pattern is used to define a regular expression. Use the Pattern.compile() factory method to create a pattern.
  3. Matcher is used to match the pattern against a input sequence. Use Pattern.matcher() to get create a Matcher.
Following example shows how to use the Pattern and Matcher.
package com.sourcetricks.regex;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegExTest {
 public static void main(String[] args) {

  // Matches the entire region
  String str = "java";
  Pattern pattern = Pattern.compile("java");
  Matcher matcher = pattern.matcher(str);
  if ( matcher.matches() ) {
   System.out.println("Pattern found.");
  // Find if a pattern exists
  str = "This is a java tutorial";
  pattern = Pattern.compile("java");
  matcher = pattern.matcher(str);
  if ( matcher.find() ){
   System.out.println("Pattern found at location = " + matcher.start());
  // Using flags to perform case insensitive match
  str = "This is a Java tutorial";
  pattern = Pattern.compile("java", Pattern.CASE_INSENSITIVE);
  matcher = pattern.matcher(str);
  if ( matcher.find() ){
   System.out.println("Pattern found at location = " + matcher.start());
  // Split a string using a whitespace
  str = "This is a Java tutorial";
  pattern = Pattern.compile("\\s");
  String[] words = pattern.split(str);
  for ( String word : words )
  // Find all occurrences of a pattern
  str = "This is a Java tutorial. Java is programming language.";
  pattern = Pattern.compile("[jJ]ava");
  matcher = pattern.matcher(str);
  while ( matcher.find() ) {
   System.out.println("Pattern found at location = " + matcher.start());
  // Replace all occurrences of a pattern
  String str1 = matcher.replaceAll("C++");

This example produces the following output.
Pattern found.
Pattern found at location = 10
Pattern found at location = 10
Pattern found at location = 10
Pattern found at location = 25
This is a C++ tutorial. C++ is programming language.


  1. Prateek AshtikarMay 16, 2014 at 11:23 PM

    Here I would like to share following snippet, If you want to print logger.trace messages at file level you should used the below code

    # Root logger option
    log4j.rootLogger=TRACE, file, console
    # Direct log messages to stdout
    log4j.appender.console.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c - %m%n

    # File appender
    log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c - %m%n


    Will help you to log logger.trace in a prateek.log file

  2. రాజ శేఖర్May 22, 2014 at 6:06 PM

    visual c++

  3. Prateek AshtikarMay 23, 2014 at 11:48 AM

    Using the "Log to both console and file and with different log levels", I am unable to print log trace messages in a file on linux environment. Could someone please help me?


Contact Form


Email *

Message *

Back to Top