Saturday, November 23, 2013

java convert regular expression into string

It is easy to check a string with regular expression, but there is no direct method to generate string from regular expression. I hardly need it but found nothing. So I myself generated the following program to generate string using regular expression and check back the string with given regular expression. The program i generated will sufficient for normal expression. It will cover my back. It will not parse high level expression.

Output example using random regex generator


Regex: [a-z][a-z0-9]{8,10}@[a-z0-9]{5,9}.com
Matches: [a-z][a-z0-9]{8,10}@[a-z0-9]{5,9}.com, String: bak65qvkpp@3qosue.com, Length: 21



Regex: [\w][\D]{0,10}[\d\W]{1,10}[abc.][abc09]{1,10}
Matches: [\w][\D]*[\d\W]+[abc.][abc09]+, String: aLchJL,e’‘.c9bca, Length: 16



Regex: [A-Z]{4}-#%[\d]{3}
Matches: [A-Z]{4}-#%[\d]{3}, String: BAKZ-#%114, Length: 10



Regex: www.[a-z][a-z0-9-_]{5,15}[a-z].com
Matches: www.[a-z][a-z0-9-_]{5,15}[a-z].com, String: www.jj829x10cq9ngilk.com, Length: 24



Regex: 01[7856][0-9]{2}-[0-9]{6}
Matches: 01[7856][0-9]{2}-[0-9]{6}, String: 01700-736202, Length: 12

StringRegex.java (main class)


import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 *
 * @author Pritom K Mondal
 * @version 1.0
 */
public class StringRegex {
    public static void main(String[] args) {
        String regex = "[a-z][a-z0-9]{8,10}@[a-z0-9]{5,9}.com";
        StringRegex.stringFromRegex(regex);
        regex = "[\\w][\\D]*[\\d\\W]+[abc.][abc09]+";
        StringRegex.stringFromRegex(regex);
        regex = "[A-Z]{4}-#%[\\d]{3}";
        StringRegex.stringFromRegex(regex);
        regex = "www.[a-z][a-z0-9-_]{5,15}[a-z].com";
        StringRegex.stringFromRegex(regex);
        regex = "01[7856][0-9]{2}-[0-9]{6}";
        StringRegex.stringFromRegex(regex);
    }
    
    public static void stringFromRegex(String regex) {         
        String regexConverted = cvtLineTerminators(regex);
        System.out.println("Regex: " + regexConverted);
        String fullString = "";
        fullString = parse(regexConverted, "");
        if(fullString.matches(regex)) {
            System.out.println("Matches: " + regex + ", String: " + fullString + ", Length: " + fullString.length());
        } else {
            System.err.println("Matches Failed: " + regex + ", String: " + fullString + ", :Length: " + fullString.length());
        }
        System.out.println("\n\n");
    }
    
    private static String parse(String regex, String fullString) {
        Random random = new Random();
        if(regex.trim().length() > 0) {
            Boolean allow = false, processed = false;
            if(regex.startsWith("[") 
                    && (regex.substring(0, regex.indexOf("]") + 1).matches("(.*)[a-z]\\-[a-z](.*)") 
                    || regex.substring(0, regex.indexOf("]") + 1).matches("(.*)[A-Z]\\-[A-Z](.*)") 
                    || regex.substring(0, regex.indexOf("]") + 1).matches("(.*)d(.*)") 
                    || regex.substring(0, regex.indexOf("]") + 1).matches("(.*)D(.*)") 
                    || regex.substring(0, regex.indexOf("]") + 1).matches("(.*)w(.*)") 
                    || regex.substring(0, regex.indexOf("]") + 1).matches("(.*)W(.*)") 
                    || regex.substring(0, regex.indexOf("]") + 1).matches("\\[([a-z]*)([0-9]*).\\]") 
                    || regex.substring(0, regex.indexOf("]") + 1).matches("\\[[A-Z].\\]") 
                    || regex.substring(0, regex.indexOf("]") + 1).matches("(.*)[0-9]\\-[0-9](.*)"))) {
                allow = true;
            }
            if(allow) {
                int start = regex.indexOf("[");
                int end = regex.indexOf("]", start);
                String part = regex.substring(start, end + 1);
                regex = regex.substring(end + 1);
                int pos1 = 1, pos2 = 1;
                if(regex.startsWith("{")) {
                    start = 0;
                    String pos = "";
                    end = regex.indexOf("}", start);
                    pos = regex.substring(start + 1, end);
                    //part = part + regex.substring(start, end + 1);
                    regex = regex.substring(end + 1);
                    
                    if(pos.trim().length() > 0) {
                        if(pos.contains(",")) {
                            String[] poss = pos.split(",");
                            pos1 = Integer.parseInt(poss[0]);
                            pos2 = Integer.parseInt(poss[1]);
                        } else {
                            pos1 = Integer.parseInt(pos);
                            pos2 = Integer.parseInt(pos);
                        }
                    }
                }
                //System.out.println("Pos: " + pos1 + " to " + pos2);
                
                StringBuilder sb = new StringBuilder();                
                String str = "";
                String printInfo = "Processing-1: " + part;
                int tried = 0;
                while(sb.toString().length() == 0 && tried <= 5) {
                    tried++;
                    str = RandomString.nextString(2000);
                    try {
                        Matcher m = Pattern.compile(part).matcher(str);
                        while (m.find()) {
                            sb.append(m.group(0).toString());
                        }
                    } catch (Exception ex) {
                        System.out.println("Exception: " + ex.getMessage());
                    }
                }
                if(sb.toString().length() > 0) {
                    processed = true;
                    str = sb.toString();
                    while(true) {
                        if(str.length() < pos2) {
                            str = str.concat(str);
                        } else {
                            break;
                        }
                    }
                    printInfo += " = (" + pos1 + ", " + pos2 + "): ";
                    String cutString = str.substring(0, pos1);
                    if(pos2 - pos1 > 0) {
                        pos2 = random.nextInt(pos2 - pos1);
                        cutString += str.substring(pos1, pos2 + pos1);
                    }
                    printInfo += cutString;
                    //System.out.println(printInfo);
                    fullString += cutString;
                } else {
                    regex = part + regex;
                }
            } 
            if(regex.startsWith("[") && !processed) {
                processed = true;
                int start = regex.indexOf("[");
                int end = regex.indexOf("]", start);
                String part = regex.substring(start + 1, end);
                regex = regex.substring(end + 1);
                String printInfo = "Processing-2: " + part;
                int pos1 = 1, pos2 = 1;
                if(regex.startsWith("{")) {
                    start = 0;
                    String pos = "";
                    end = regex.indexOf("}", start);
                    pos = regex.substring(start + 1, end);
                    regex = regex.substring(end + 1);
                    
                    if(pos.trim().length() > 0) {
                        if(pos.contains(",")) {
                            String[] poss = pos.split(",");
                            pos1 = Integer.parseInt(poss[0]);
                            pos2 = Integer.parseInt(poss[1]);
                        } else {
                            pos1 = Integer.parseInt(pos);
                            pos2 = Integer.parseInt(pos);
                        }
                    }
                }
                String pushString = "";
                for(int i = 0; i < pos1; i++) {
                    pushString += part;
                }
                if(pos2 - pos1 > 0) {
                    pos2 = random.nextInt(pos2 - pos1);
                    for(int i = 0; i < pos2; i++) {
                        pushString += part;
                    }
                }
                printInfo += "{" + pos1 + "," + pos2 + "} = " + pushString;
                //System.out.println(printInfo);
                fullString += pushString;
            } 
            if(!processed) {
                //System.out.println("Handled: " + regex);
                regex = "[" + regex;
                int fa = regex.indexOf("]"), f2 = regex.indexOf("{"), f3 = regex.indexOf("[", 1);
                if(fa > f3) fa = f3;
                if(fa > f2) fa = f2;
                if(fa < 0) {
                    if(f2 < f3 && f2 >= 0) {
                        fa = f2;
                    } else if(f3 > f2 && f3 >= 0) {
                        fa = f3;
                    } else {
                        fa = regex.length();
                    }
                }
                //regex = regex.substring(0, fa) + "]" + regex.substring(fa);
                regex = regex.substring(0, 2) + "]" + regex.substring(2);
                //System.out.println("Handled: " + regex);
            }
            fullString = parse(regex, fullString);
        }
        return fullString;
    }
    
    private static String cvtLineTerminators (String s) {
        s = s.replaceAll("\\*", "{0,10}");
        s = s.replaceAll("\\+", "{1,10}");
        
        StringBuffer sb = new StringBuffer ();
        int oldindex = 0, newindex;
        while ((newindex = s.indexOf ("\\n", oldindex)) != -1) {
            sb.append (s.substring (oldindex, newindex));
            oldindex = newindex + 2;
            sb.append ('\n');
        }
        sb.append (s.substring (oldindex));

        s = sb.toString ();
        sb = new StringBuffer ();
        oldindex = 0;
        while ((newindex = s.indexOf ("\\r", oldindex)) != -1) {
            sb.append (s.substring (oldindex, newindex));
            oldindex = newindex + 2;
            sb.append ('\r');
        }
        sb.append (s.substring (oldindex));

        s = sb.toString ();
        sb = new StringBuffer ();
        oldindex = 0;
        while ((newindex = s.indexOf ("\\s", oldindex)) != -1) {
            sb.append (s.substring (oldindex, newindex));
            oldindex = newindex + 2;
            sb.append (" ");
        }
        sb.append (s.substring (oldindex));
        
        return sb.toString();
   }
}

RandomString.java


import java.util.Random;

/**
 *
 * @author Pritom K Mondal
 */
public class RandomString {
    private static final char[] symbols = new char[10 + 26 + 26 + 3 + 8];
    private static final char[] chars = new char[52];
    private static final char[] numbers = new char[10];
    
    static {
        for (int idx = 0; idx < 10; ++idx) {
            numbers[idx] = (char) ('0' + idx);
        }
        for (int idx = 10; idx < 36; ++idx) {
            chars[idx - 10] = (char) ('a' + idx - 10);
        }
        for (int idx = 36; idx < 62; ++idx) {
            chars[idx - 10] = (char) ('A' + idx - 36);
        }
        /**
         * String
         */
        int total = 0;
        for(int idx = 48; idx <= 57; idx++, total++) {
            symbols[total] = (char) idx;
        }// 10 (10)
        for(int idx = 65; idx <= 90; idx++, total++) {
            symbols[total] = (char) idx;
        } // 26 (36)
        for(int idx = 97; idx <= 122; idx++, total++) {
            symbols[total] = (char) idx;
        } // 26 (62)
        for(int idx = 44; idx <= 46; idx++, total++) {
            symbols[total] = (char) idx;
        } // 3 (65)
        int[] copyFrom = { 64, 95, 44, 46, 45, 145, 146, 35 };
                         //@   _   ,   .   -   ‘     ’    #
        for(int idx = 0; idx < copyFrom.length; idx++, total++) {
            symbols[total] = (char) copyFrom[idx];
        } // 8 (73)
        //System.out.println(total);
    }
    
    private static Random random = new Random();
    private static char[] buf;
    
    public static String fullString() {
        return new String(symbols);
    }
    
    public static String nextString(int length) {
        if (length < 1) {
            throw new IllegalArgumentException("length < 1: " + length);
        }
        buf = new char[length];
        //System.out.println(symbols);
        for (int idx = 0; idx < buf.length; ++idx) {
            buf[idx] = symbols[random.nextInt(symbols.length)];
        }
        return new String(buf);
    }
    
    public static String nextNumber(int length) {
        if (length < 1) {
            throw new IllegalArgumentException("length < 1: " + length);
        }
        buf = new char[length];
        for (int idx = 0; idx < buf.length; ++idx) {
            buf[idx] = numbers[random.nextInt(numbers.length)];
        }
        return new String(buf);
    }
    
    public static String nextCharacter(int length) {
        if (length < 1) {
            throw new IllegalArgumentException("length < 1: " + length);
        }
        buf = new char[length];
        for (int idx = 0; idx < buf.length; ++idx) {
            buf[idx] = chars[random.nextInt(chars.length)];
        }
        return new String(buf);
    }
}

Friday, November 22, 2013

Java generate sequence of random characters or numbers or strings

import java.util.Random;

/**
 * Created by Pritom K Mondal.
 */
public class RandomString {
    public static String formatAsLength(Long number, Integer minLength = 0) {
        return String.format("%0${minLength}d%n".toString(), number).trim();
    }

    private static final char[] symbols = new char[62];
    private static final char[] chars = new char[52];
    private static final char[] numbers = new char[10];

    static {
        initialize();
    }

    public static void initialize() {
        Integer symIdx = 0, numIdx = 0, charIdx = 0;
        for (int idx = 48; idx <= 57; ++idx) {
            symbols[symIdx++] = (char) (idx);
            numbers[numIdx++] = (char) (idx);
        }
        for (int idx = 97; idx < 26 + 97; ++idx) {
            symbols[symIdx++] = (char) (idx);
            chars[charIdx++] = (char) (idx);
        }
        for (int idx = 65; idx < 26 + 65; ++idx) {
            symbols[symIdx++] = (char) (idx);
            chars[charIdx++] = (char) (idx);
        }
    }

    private static Random random = new Random();

    public static String nextString(int length) {
        if (length < 1) {
            throw new IllegalArgumentException("length < 1: " + length);
        }
        if (symbols[0] == (char) 0) {
            initialize();
        }
        String returnString = "";
        for (int idx = 0; idx < length; ++idx) {
            returnString += symbols[random.nextInt(symbols.length)].toString()
        }
        return returnString;
    }

    public static String nextNumber(int length) {
        if (length < 1) {
            throw new IllegalArgumentException("length < 1: " + length);
        }
        if (symbols[0] == (char) 0) {
            initialize();
        }
        char[] buf = new char[length];
        for (int idx = 0; idx < buf.length; ++idx) {
            buf[idx] = numbers[random.nextInt(numbers.length)];
        }
        return new String(buf);
    }

    public static String nextCharacter(int length) {
        if (length < 1) {
            throw new IllegalArgumentException("length < 1: " + length);
        }
        if (symbols[0] == (char) 0) {
            initialize();
        }
        char[] buf = new char[length];
        for (int idx = 0; idx < buf.length; ++idx) {
            buf[idx] = chars[random.nextInt(chars.length)];
        }
        return new String(buf);
    }
}

Usage:


String mixing = RandomString.nextString(40);
String number = RandomString.nextNumber(40);
String chars = RandomString.nextCharacter(40);
System.out.println("MIXING: " + mixing);
System.out.println("NUMBER: " + number);
System.out.println("CHARS:  " + chars);

Output:


MIXING: ZP8RQCpEF7Kr9veJDlbUWafqNxjqh4xwJr3bvw9J
NUMBER: 5578864807912590082652398553220665093932
CHARS:  nZvejdXyiMEzzbeuJyVTpaGwmGykqNVMQLAckAfP

Wednesday, November 20, 2013

java convert number to word (text)

/**
 *
 * @author Pritom K Mondal
 */
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

public class NumberToWords {
    private static final String[] tensNames = {
        "", " ten", " twenty", " thirty", " forty", " fifty", " sixty",
        " seventy", " eighty", " ninety"
    };

    private static final String[] numNames = {
        "", " one", " two", " three", " four", " five", " six", " seven", " eight",
        " nine", " ten", " eleven", " twelve", " thirteen", " fourteen", " fifteen",
        " sixteen", " seventeen", " eighteen", " nineteen"
    };

    private static String convertLessThanOneThousand(int number) {
        String soFar;
        
        if (number % 100 < 20){
            soFar = numNames[number % 100];
            number /= 100;
        } else {
            soFar = numNames[number % 10];
            number /= 10;
            
            soFar = tensNames[number % 10] + soFar;
            number /= 10;
        }
        if (number == 0) return soFar;
        return numNames[number] + " hundred" + soFar;
    }

    private static String convert(long number) {
        // 0 to 999 999 999 999
        if (number == 0) { return "zero"; }
        
        String snumber = Long.toString(number);
        // pad with "0"
        String mask = "000000000000";
        DecimalFormat df = new DecimalFormat(mask);
        snumber = df.format(number);
        // XXXnnnnnnnnn 
        int billions = Integer.parseInt(snumber.substring(0,3));
        // nnnXXXnnnnnn
        int millions  = Integer.parseInt(snumber.substring(3,6)); 
        // nnnnnnXXXnnn
        int hundredThousands = Integer.parseInt(snumber.substring(6,9)); 
        // nnnnnnnnnXXX
        int thousands = Integer.parseInt(snumber.substring(9,12));    
        
        String tradBillions;
        switch (billions) {
            case 0:
                tradBillions = "";
                break;
            case 1 :
                tradBillions = convertLessThanOneThousand(billions) 
                        + " billion ";
                break;
            default :
                tradBillions = convertLessThanOneThousand(billions) 
                        + " billion ";
        }
        String result =  tradBillions;
        
        String tradMillions;
        switch (millions) {
            case 0:
                tradMillions = "";
                break;
            case 1 :
                tradMillions = convertLessThanOneThousand(millions) + " million ";
                break;
            default :
                tradMillions = convertLessThanOneThousand(millions) + " million ";
        }
        result =  result + tradMillions;
        
        String tradHundredThousands;
        switch (hundredThousands) {
            case 0:
                tradHundredThousands = "";
                break;
            case 1 :
                tradHundredThousands = "one thousand ";
                break;
            default :
                tradHundredThousands = convertLessThanOneThousand(hundredThousands)  + " thousand ";
        }
        result =  result + tradHundredThousands;
        
        String tradThousand;
        tradThousand = convertLessThanOneThousand(thousands);
        result =  result + tradThousand;
        
        // remove extra spaces!
        return result.replaceAll("^\\s+", "").replaceAll("\\b\\s{2,}\\b", " ");
    }
    
    public static String convert(String number, String weight) {
        String[] numbers = number.split("\\.");
        String[] weights = weight.split("\\.");
        String word = "";
        for(Integer position = 0; position < numbers.length; position++) {
            if(numbers[position].trim().length() > 0) {
                if(position == 1) {
                    word = word.trim();
                    if(word.startsWith("zero")) {
                        word = "";
                    } else {
                        word = word.trim() + " and ";
                    }
                }
                word += NumberToWords.convert(new Long("" + numbers[position]));
                if(weights.length >= position) {
                    word = word.trim() + " " + weights[position];
                }
            }
        }
        return word.trim().toUpperCase();
    }
    
    /**
     * testing
     * @param args
     */
    public static void main(String[] args) {
        List numberList = new ArrayList<String>();
        numberList.add(0);
        numberList.add(1);
        numberList.add(7);
        numberList.add(15.98);
        numberList.add("36.06");
        numberList.add(48);
        numberList.add(67.986);
        numberList.add(89.09);
        numberList.add(435);
        numberList.add(.110);
        numberList.add(328.64);
        numberList.add(99999);
        numberList.add(100000);
        numberList.add(1000010);
        numberList.add(1002);
        numberList.add(3567);
        numberList.add(6942);
        numberList.add(9073);
        numberList.add(4583);
        numberList.add(43768);
        numberList.add(35789);
        numberList.add(35483);
        numberList.add(468368);
        numberList.add(9073254);
        numberList.add(73859735);
        numberList.add("5890203983");
        numberList.add(3000000010L);
        
        Object[] objects = numberList.toArray();
        for(Integer position = 0; position < objects.length; position++) {
            System.out.println("" + 
                    NumberToWords.convert("" + objects[position], "DOLLAR.CENT") 
                    + " (" + objects[position] + ")");
        }
    }
}

Output:

ZERO DOLLAR (0)
ONE DOLLAR (1)
SEVEN DOLLAR (7)
FIFTEEN DOLLAR AND NINETY EIGHT CENT (15.98)
THIRTY SIX DOLLAR AND SIX CENT (36.06)
FORTY EIGHT DOLLAR (48)
SIXTY SEVEN DOLLAR AND NINE HUNDRED EIGHTY SIX CENT (67.986)
EIGHTY NINE DOLLAR AND NINE CENT (89.09)
FOUR HUNDRED THIRTY FIVE DOLLAR (435)
ELEVEN CENT (0.11)
THREE HUNDRED TWENTY EIGHT DOLLAR AND SIXTY FOUR CENT (328.64)
NINETY NINE THOUSAND NINE HUNDRED NINETY NINE DOLLAR (99999)
ONE HUNDRED THOUSAND DOLLAR (100000)
ONE MILLION TEN DOLLAR (1000010)
ONE THOUSAND TWO DOLLAR (1002)
THREE THOUSAND FIVE HUNDRED SIXTY SEVEN DOLLAR (3567)
SIX THOUSAND NINE HUNDRED FORTY TWO DOLLAR (6942)
NINE THOUSAND SEVENTY THREE DOLLAR (9073)
FOUR THOUSAND FIVE HUNDRED EIGHTY THREE DOLLAR (4583)
FORTY THREE THOUSAND SEVEN HUNDRED SIXTY EIGHT DOLLAR (43768)
THIRTY FIVE THOUSAND SEVEN HUNDRED EIGHTY NINE DOLLAR (35789)
THIRTY FIVE THOUSAND FOUR HUNDRED EIGHTY THREE DOLLAR (35483)
FOUR HUNDRED SIXTY EIGHT THOUSAND THREE HUNDRED SIXTY EIGHT DOLLAR (468368)
NINE MILLION SEVENTY THREE THOUSAND TWO HUNDRED FIFTY FOUR DOLLAR (9073254)
SEVENTY THREE MILLION EIGHT HUNDRED FIFTY NINE THOUSAND SEVEN HUNDRED THIRTY FIVE DOLLAR (73859735)
FIVE BILLION EIGHT HUNDRED NINETY MILLION TWO HUNDRED THREE THOUSAND NINE HUNDRED EIGHTY THREE DOLLAR (5890203983)
THREE BILLION TEN DOLLAR (3000000010)