Source Code : Reg Exp Example

Java Is Open Source Programming Language You Can Download From Java and Java Libraries From http://www.oracle.com. Click Here to download
We provide this code related to title for you to solve your developing problem easily. Libraries which is import in this program you can download from http://www.oracle.com. Click Here or search from google with Libraries Name you get jar file related it

Reg Exp Example

 

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegExpExample {

  public static void main(String args[]) {
    String fileName = "RETestSource.java";

    String unadornedClassRE = "^\s*class (\w+)";
    String doubleIdentifierRE = "\b(\w+)\s+\1\b";

    Pattern classPattern = Pattern.compile(unadornedClassRE);
    Pattern doublePattern = Pattern.compile(doubleIdentifierRE);
    Matcher classMatcher, doubleMatcher;

    int lineNumber = 0;

    try {
      BufferedReader br = new BufferedReader(new FileReader(fileName));
      String line;

      while ((line = br.readLine()) != null) {
        lineNumber++;

        classMatcher = classPattern.matcher(line);
        doubleMatcher = doublePattern.matcher(line);

        if (classMatcher.find()) {
          System.out.println("The class [" + classMatcher.group(1)
              + "] is not public");
        }

        while (doubleMatcher.find()) {
          System.out.println("The word "" + doubleMatcher.group(1)
              + "" occurs twice at position "
              + doubleMatcher.start() + " on line " + lineNumber);
        }
      }
    } catch (IOException ioe) {
      System.out.println("IOException: " + ioe);
      ioe.printStackTrace();
    }
  }
}




           
         
  

Thank with us