WebHosting

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

How to add "Copy to" and "Move to" options to the right-click context menu in Windows:

Method 1: Registry Editor (Manual) Note: Back up your registry first or create a system restore point. Add "Copy to" Option:...