Webcam Chat QuickBooks Advice international calling cards international phone cards
JavaBeat Java Books Certifications Certifications Kits Articles Tutorials Tips QNA Book Store Interview Questions SCJP 1.5 SCJP 1.6 SCWCD 5.0 SCBCD 5.0 SCEA SCJA Feeds

Regular Expressions in Java

Author : Christy
Topic : java  
Pages :
Submit Your Blog Feedback Request Article Print Email

1) Introduction

Regular Expressions are basically patterns of characters which are used to perform certain useful operations on the given input. The operations include finding particular text, replacing the text with some other text, or validating the given text. For example, we can use Regular Expression to check whether the user input is valid for a field like Email Id or a telephone number.

2) java.util.regex Package

The java.util.regex package provides the necessary classes for using Regular Expressions in a java application. This package has been introduced in Java 1.4. It consists of three main classes namely,

  • Pattern
  • Matcher
  • PatternSyntaxException

3) Pattern Class

A regular expression which is specified as a string, should be first compiled into an instance of Pattern class. The resulting pattern can then be used to create an instance of Matcher class which contains various in-built methods that helps in performing a match against the regular expression. Many Matcher objects can share the same Pattern object.

Let us now discuss about the important methods in Pattern class.

a) compile() method

Before working with, we need a compiled form of regular expression pattern, by calling the Pattern.compile() method which returns a new Pattern object. Note that compile() is a static method, so we dont an instance of the Pattern class.

There are two forms of compile() method,

  • compile(String regex)
  • compile(String regex, int flags)

In the first form of compile() method, we pass the regular expression that would be compiled. In the second form of this method, we have an additional parameter which is used to specify the match flags that has to be applied. The flags can be either CASE_INSENSITIVE, MULTILINE, DOTALL, UNICODE_CASE or CANON_EQ based on which matching would be done.

b) matcher() method

The matcher() method is used to create new Matcher object for an input for a given pattern, which can be used to perform matching operations. The syntax is as follows,

matcher(String input)

c) matches() method

The Pattern class provides a matches() method, which is a static method. This method returns true only if the entire input text matches the pattern. This method internally depends on the compile() and matcher() methods of the Pattern object. The syntax for this static matches() method is,

Pattern.matches(pattern, inputSequence);

Let us see a simple example,

RegExpTest.java


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

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

        String inputStr = "Computer";
        String pattern = "Computer";
        boolean patternMatched = Pattern.matches(pattern, inputStr);	
        System.out.println(patternMatched);

    }
}

The matches() method returns true in this case and hence we get the output as true. If in case, we had given the input string as "ComputerScience", then the matches() method would have returned false.

In the static matches() method, when we specify an input string and a pattern to the Pattern.matches() method, the pattern gets compiled into a Pattern object which is used for matching operation. This is inefficient because every time we specify an input string and pattern, compilation of the pattern is done. Hence, its better to use the non static matches() method in Matcher class. (This matches() method would be discussed when dealing with Matcher class in the forth-coming section).

d) pattern() method

Returns the regular expression as a string from which this pattern was compiled.

e) split() method

This method is used to split the given input text based on the given pattern. It returns a String array.

There are two forms of split() method,

  • split(String input)
  • split(String input, int limit)

In the second form, we have an argument called limit which is used to specify the limit i.e the number of resultant strings that have to be obtained by split() method.

Let us see a simple example for the split() method,


Pattern pattern = Pattern.compile("ing");
Matcher matcher = pattern.matcher("playingrowinglaughingsleepingweeping");
String[] str = pattern.split(input, 4);
for(String st : str)
{
    System.out.println(st);
}

In the above code, we had specified the limit of number of Strings to be returned as 4. Hence we would get 4 strings as the result.

The output for the above code is, play row laugh sleepingweeping

f) flag() method

This method returns this pattern's match flags which would have been specified when the pattern was compiled.

Submit Your Blog Feedback Request Article Print Email

Related Articles


JavaBeat Website (2004-2011), India
javabeat | advertise | about us | contact | useful resources
Copyright (2004 - 2011), JavaBeat


Technology Blogs
Technology blogs Technology Blogs
blog log