Loading
Java Regular Expression API Example

Java Quick Notes

Refresh Your Java - Before Java Interview

We are Agile, believe in less Documentation - Only Quick notes (Java Interview Questions) of Java/J2ee Read more....


Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet

Go to SiteMap

Q)  Java Regular Expression API Example RegexUtility


Ans)

Java Regular Expression API Utility "extractFirstString", "extractUniqueStringSet"
"extractUniqueStringSet",  "extractStringArray"

Regular Expression API Example.

  RegexUtility is meant for providing the Some special resuable
  Utility methods for string manupulation by using the Java
  Regular Expression API.
    Methods such as
    i) extractFirstString
   ii) extractUniqueStringSet
   iii) removeHypenAndChangeToLowerCase
   iv) extractUniqueStringSet
   v)extractStringArray
 

import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegexUtility {
    private static Pattern PATTERN_firstLetterInEveryHypenDeliminatedWord =
            Pattern.compile("(_|^)\\w");
   
    /** Creates a new instance of RegexUtility */
    private RegexUtility() {
    }
   
   
    public static StringBuffer replaceString( StringBuffer sb,
      Pattern pattern, String newString ){
        Matcher matcher = pattern.matcher(sb);
        while (matcher.find()){
            sb.replace(matcher.start(), matcher.end(), newString );
        }
        return sb;
    }
   
    public static StringBuffer replaceString( StringBuffer sb,
     String pattern, String newString ){
        return  replaceString(sb, Pattern.compile(pattern), newString);
    }
   
    public static String extractString(CharSequence input, Pattern pattern ) {
        Matcher matcher = pattern.matcher(input);
        while (matcher.find()){
            return matcher.group();
        }
        return "";
    }
   
   
    public static String[] extractStringArray(CharSequence input, Pattern pattern ) {
        List retList = new java.util.ArrayList();
        Matcher matcher = pattern.matcher(input);
        while (matcher.find()){
            retList.add( matcher.group() );
        }
        int n = retList.size();
        String retArray[] = null;
        if (n>0){
            retArray = new String[n];
            retList.toArray(retArray);
        }
        return retArray;
    }
   
    public static String extractFirstString(CharSequence input, Pattern pattern ) {
       
        Matcher matcher = pattern.matcher(input);
        while (matcher.find()){
            return matcher.group();
        }
        return null;
    }
   
    public static Set extractUniqueStringSet(CharSequence input, Pattern pattern ) {
        Set retSet = new HashSet();
        Matcher matcher = pattern.matcher(input);
        while (matcher.find()){
            retSet.add( matcher.group() );
        }
        return retSet;
    }
   
    public static boolean containPattern(CharSequence input, Pattern pattern ) {
        Matcher matcher = pattern.matcher(input);
        while (matcher.find()){
            return true;
        }
        return false;
    }
   
    public static String removeHypenAndChangeToLowerCaseExceptForFirstWordLetter(String input){
       
        Matcher matcher = PATTERN_firstLetterInEveryHypenDeliminatedWord.matcher(input.toLowerCase());
        StringBuffer newSb = new StringBuffer();
        while (matcher.find()){
            String newString = matcher.group().replaceAll("_", "").toUpperCase();
            matcher.appendReplacement(newSb, newString );
        }
        matcher.appendTail(newSb);
        return newSb.toString();
    }
}



Back to top

------------------------- We hope you got necessary Info On -----------------------------------------

Java Regular Expression API Example


Java Regular Expression API Example

-------------------------------------------------------------------------------------------------------



Face Book
Request for a Mock Interview/Training

Get a PDF

Face Book
Same look (Read) on any device, this is Ads free