Tuesday, February 28, 2012

Gentrate Equivalent Regular Expression From String

public static String CreateRegexFromString(String inString) { 
        StringBuilder newString = new StringBuilder(); 
        for(int i=0; i<inString.length(); ++i) { 
            char eachCharacher = inString.charAt(i); 
            if ("\\.^$|?*+[]{}()".indexOf(eachCharacher) != -1) 
                newString.append('\\').append(eachCharacher); 
            else if (Character.isLetter(eachCharacher))
               
                newString.append("["+Character.toLowerCase(eachCharacher)+Character.toUpperCase(eachCharacher)+"]"); 
            else if (Character.isDigit(eachCharacher)) 
                newString.append("\\d"); 
            else 
                newString.append(eachCharacher); 
        } 
        return newString.toString(); 
    }

No comments:

Post a Comment

Thank you for Commenting Will reply soon ......

Featured Posts

#Intel #Core #i9 #Processors: A Breakdown

Intel Core i9 Processors: A Breakdown Intel's 14th Gen Core i9 series offers a range of processors designed for various use cases. Her...