Reading:  

Quick introduction to Java Programming language - Part 1


Understanding Regular Expressions

Java provide java.util.regex package for pattern matching with regular expressions. 

A regular expression is special sequence of characters helps to match or find other strings or sets of strings using specialized syntax in pattern. They can be used to edit, search or data.

The java.util.regex package mainly consists of following three classes: 

  • Pattern Class: A Pattern object is compiled representation of regular expression. The Pattern class provides no public constructors. To create a pattern, you must first invoke one of its public static compile methods, which will then return a Pattern object. These methods accept a regular expression as the first argument.
  • Matcher Class: A Matcher object is the engine that interprets the pattern and performs match operations against an input string. Like the Pattern class, Matcher defines no public constructors. You obtain a Matcher object by invoking the matcher method on a Pattern object.
  • PatternSyntaxException: A PatternSyntaxException object is an unchecked exception that indicates a syntax error in a regular expression pattern.

Capturing Groups:

Capturing groups is way to treat multiple characters as single unit. They are created by placing characters to be grouped inside a set of parentheses. For example, regular expression (cat) creates a single group containing the letters "c" "a" and "t".

Capturing groups is numbered by counting their opening parentheses from left to right. In expression ((A)(B(C))), for example, there are four such groups:

  • ((A)(B(C)))
  • (A)
  • (B(C))
  • (C)

Example

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

public class RegexampleMatche
{
    public static void main( String args[] ){
    
      String line_1 = "The order number is KD1034!  ";
      String pattern_1 = "(.*)(\\d+)(.*)";
     
      Pattern r = Pattern_1.compile(pattern);

      // Now create matcher object.
      Matcher m_1 = r.matcher(line_1);
      if (m_1.find( )) {
         System.out.println("Value Found: " + m_1.group(0) );
         System.out.println("Value Found: " + m_1.group(1) );
         System.out.println("Value Found: " + m_1.group(2) );
      } else {
         System.out.println("NO MATCH");
      }
   }
}

OutPut:

Value Found: The order number is KD1034!

Value Found: The order number is KD1034

Value Found: 0

 

Methods of the Matcher Class:

The list of useful instance methods:

Index Methods:

Index methods provide useful index values which show precisely where match was found in input string:

Methods with Description

Public int start()

Returns start index of previous match.

public int start(int group)

Returns start index of subsequence captured by given group during previous match operation.

public int end()

Returns offset after last character matched.

public int end(int group)

Returns offset after last character of subsequence captured by given group during previous match operation.

Study Methods:

Study methods review input string and return a Boolean indicating whether or not pattern is matched:

Methods with Description

public boolean lookingAt()

Attempts to match input sequence starting at beginning of the region, against pattern.

public boolean find()

Attempts to find next subsequence of input sequence that matches pattern.

public boolean matches()

Attempts to match entire region against pattern.

public boolean find(int start)

Resets matcher and attempts to find next subsequence of input sequence that matches pattern, starting at specified index.

 

Replacement Methods:

A replacement method is useful methods for replacing text in input string:

Methods with Description

public Matcher appendReplacement(StringBuffer str, String rep)

Implements non-terminal replace and append step.

public StringBuffer appendTail(StringBuffer str)

Implements terminal replace and append step.

public String replaceAll(String rep)

Replaces every subsequence of input sequence that matches pattern with given replacement string.

public String replaceFirst(String rep)

Replaces first subsequence of input sequence that matches pattern with given replacement string.

The start and end Methods:

Following is example that counts number of times word "dog" appears in the input string:

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

public class RegexampleMatches
{
    private static final String REGEX = "\\bdog\\b";
    private static final String INPUTSTR =
                                    "dog doggie";

    public static void main( String args[] ){
       Pattern p = Pattern.compile(REGEX);
       Matcher m = p.matcher(INPUTSTR); 
       int count = 0;

       while(m.find()) {
         count++;
         System.out.println("Match number "+count);
         System.out.println("start(): "+m.start());
         System.out.println("end(): "+m.end());
      }
   }
}

Output :

Match number 1
start(): 0
end(): 3
Match number 2
start(): 4
end(): 7

The matches and lookingAt Methods:

The matches and lookingAt methods both attempt to match an input sequence against a pattern. The difference, however is that matches requires entire input sequence to be matched while lookingAt does not.

Example 

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

public class RegexMatches
{
    private static final String REGEX = "doo";
    private static final String INPUTSTR = "dooooooooooooooooo";
    private static Pattern pattern;
    private static Matcher matcher;

    public static void main( String args[] ){
       pattern = Pattern.compile(REGEX);
       matcher = pattern.matcher(INPUTSTR);

       System.out.println("REGEX is: "+REGEX);
       System.out.println("INPUT is: "+INPUT);

       System.out.println(" Value lookingAt(): "+matcher.lookingAt());
       System.out.println(" value matches(): "+matcher.matches());
   }
}

OutPut:

Current REGEX is: doo
Current INPUT is: dooooooooooooooooo
Value lookingAt(): true
Value matches(): false

PatternSyntaxException Class Methods:

A PatternSyntaxException is unchecked exception that indicates a syntax error in regular expression pattern.

Methods with Description

public String getDescription()

Retrieves description of error.

public int getIndex()

Retrieves error index.

Public String getPattern()

Retrieves erroneous regular expression pattern.

 

Our first part of quick overview of Java Language ends here. In next part we will learn object oriented programming in java and we will cover topics listed below

  1. Inheritance
  2. Overriding
  3. Polymorphism
  4. Abstraction
  5. Encapsulation
  6. Interfaces
  7. Packages

Description

This tutorial is targetted for beginers seeking a quick get on with guide on Java programming language. 3 parts include 

  1. Java basics
  2. Object oriented programming
  3. Advance concepts

Each part is subdivided in multiple chapters.

Java basics has the following chapters

  1. Introduction
  2. Environment Setup
  3. Basic Syntax
  4. Objects and Classes
  5. Basic Data Types
  6. Variable Types
  7. Modifier Types
  8. Basic Operators
  9. Loops
  10. Decision Making
  11. Numbers Class
  12. Character and String Class
  13. Arrays
  14. Date and Time
  15. Regular Expression
  16. Methods
  17. Streams, Files and I/O
  18. Exceptions Handling

Thanks for reading and as always, your feedback is very important to us. Let us know how we can improve and if you found any issues with this write up, send us a correction.



Audience

Beginners or students seeking a refresher on Java language

Author: Subject Coach
Added on: 9th Mar 2015

You must be logged in as Student to ask a Question.

None just yet!