Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

public class Main {

    public static String regLargerThanInt(int num) {
        return regLargerThanInt(num, false);
    }

    public static String regLargerThanInt(int num, boolean include) {
        String front = "";
        String back = "";
        if (include) {
            front = "(";
            back = ")";
        } else {
            front = "(?<=\\D|\\b|0)(";
            back = ")(?=\\D|\\b)";
        }
        int cou = String.valueOf(num).length();
        String reg = "";
        String longer = "[1-9]\\d{" + cou + ",}";
        reg += longer + "|";

        if (cou > 1) {
            int k1 = (int) ((num % Math.pow(10, cou)) / Math.pow(10, cou - 1));
            String largerThanK = null;
            if (k1 == 8) {
                largerThanK = "9" + "\\d{" + (cou - 1) + "}";
            } else if (k1 < 8) {
                largerThanK = "[" + (k1 + 1) + "-9]" + "\\d{" + (cou - 1) + "}";
            }

            if (largerThanK != null) {
                reg += largerThanK + "|";
            }
            for (int i = cou - 1; i > 0; i--) {
                int head = (int) (num / Math.pow(10, i));
                int tail = (int) ((num % Math.pow(10, i)) / Math.pow(10, i - 1));
                String top = null;
                if (tail == 8) {
                    top = head + "9" + (i == 1 ? "" : "\\d{" + (i - 1) + "}");
                } else if (tail < 8) {
                    top = head + "[" + (tail + 1) + "-9]" + (i == 1 ? "" : "\\d{" + (i - 1) + "}");
                }
                if (top != null) {
                    reg += top + "|";
                }
            }
        } else {
            String lar = "";
            if (num == 8) {
                lar = "9";
            } else if (num < 8) {
                lar = "[" + (num + 1) + "-9]";
            }
            reg += lar;
        }

        if (reg.lastIndexOf("|") == reg.length() - 1) {
            reg = reg.substring(0, reg.length() - 1);
        }
        reg = front + reg + back;
        return reg;
    }
}