WebHosting

Showing posts with label string to regular expression. Show all posts
Showing posts with label string to regular expression. Show all posts

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(); 
    }

Featured Posts

How to Auto-Terminate Frozen and Hung Apps in Windows

We have all been there. You are in the middle of an important project or a gaming session, and suddenly, your screen freezes. The dreaded ...