2017  Kodetalk | Feedback | Privacy Policy | Terms | About
userimage

Java regular expressions for dummies

A Regular Expression is a expression which represents a group of Strings according to a particular pattern.

Example:

  1. We can write a Regular Expression to represent all valid mail ids.
  2. We can write a Regular Expression to represent all valid mobile numbers.

The main important application areas of Regular Expression are:

  1. To implement validation logic.
  2. To develop Pattern matching applications.
  3. To develop translators like compilers, interpreters etc.
  4. To develop digital circuits.
  5. To develop communication protocols like TCP/IP, UDP etc.


Example:

import java.util.regex.*;
class RegularExpressionDemo 
{
   public static void main(String[] args) 
   {
      int count=0;
      Pattern p=Pattern.compile("ab");
      Matcher m=p.matcher("abbbabbaba");
      while(m.find())
      {
         count++;
         System.out.println(m.start()+"------"+m.end()+"------"+m.group());
      }
      System.out.println("The no of occurences :"+count);
   }
}
Output:
0------2------ab
4------6------ab
7------9------ab
The no of occurrences: 3

Pattern class:

  • A Pattern object represents "compiled version of Regular Expression"
  • We can create a Pattern object by using compile() method of Pattern class. public static Pattern compile(String regex);

Example:

Pattern p=Pattern.compile("ab");

Note: if we refer API we will get more information about pattern class.

Matcher:

A Matcher object can be used to match character sequences against a Regular Expression. We can create a Matcher object by using matcher() method of Pattern class.

public Matcher matcher(String target);
Matcher m=p.matcher("abbbabbaba");

Important methods of Matcher class:

1)    boolean find();

  • It attempts to find next match and returns true if it is available otherwise returns false.

2)    int start();

  • Returns the start index of the match.

3)    int end();

  • Returns the offset(equalize) after the last character matched.(or)
  • Returns the end index of the matched.

4)    String group();

  • Returns the matched Pattern.

Note: Pattern and Matcher classes are available in java.util.regex package.

Character classes:

  1. [abc]-------------------Either aaa or aba or aca
  2. [^abc] -----------------Except aaa and aba and aca
  3. [a-z] --------------------Any lower case alphabet symbol
  4. [A-Z] --------------------Any upper case alphabet symbol
  5. [a-zA-Z] ----------------Any alphabet symbol
  6. [0-9] --------------------Any digit from 0 to 9
  7. [a-zA-Z0-9] ------------Any alphanumeric character


Example:

import java.util.regex.*;
class RegularExpressionDemo 
{
           public static void main(String[] args) 
           {
                       Pattern p=Pattern.compile("x");
                       Matcher m=p.matcher("a1b7@z#");
                       while(m.find())
                       {
                                   System.out.println(m.start()+"-------"+m.group());
                       }
           }
}
Output:

Note:

  • String class split() method can take regular expression as argument where as pattern class split() method can take target string as the argument.

StringTokenizer:

  • This class present in java.util package.
  • It is a specially designed class to perform string tokenization.