java正则表达式

/**
     *  校验发票号码
     *  2019年3月27日14:02:56
     *  @param invoiceNo  获取pdf定义的发票信息,即发票号
     */
    public boolean checkoutInvoiceNo(String invoiceNo) {
        //定义一个检测发票成功与否的标志位
        boolean checkResult = true;
        // 将发票号码字符串去掉所有空格
        invoiceNo = replaceBlank(invoiceNo);
        // 将发票号码中的所有横杠转化为英字横线
        invoiceNo = line(invoiceNo);
        // 全角字符->半角字符
        invoiceNo = BCConvertUtils.qj2bj(invoiceNo);
        // 发票号码只能以数字、英字逗号、英字横线
        String regex="^([-,0-9]+)$";
        Pattern p=Pattern.compile(regex);
        Matcher m=p.matcher(invoiceNo);
        // 与正则表达式不匹配时
        if(!m.matches()){
            // 记录校验不通过
            checkResult = false;
        } else {
            // 先以","来分割字符串
            String[] pdfInvoiceArr = invoiceNo.split(",");
            for (int k = 0; k < pdfInvoiceArr.length; k++) {
                // 判断是否包含"-"
                if (pdfInvoiceArr[k].contains("-")) {
                    // 包含"-"时,再以"-"来分割字符串
                    String[] invoiceNoLine = pdfInvoiceArr[k].split("-");
                    if (invoiceNoLine.length > 1) {
                        int numberOne = 0;
                        int numberEnd = 0;
                        // 将含有"-"的发票号顺番
                        // 发票号码长度不为8时格式不正确
                        if (!(invoiceNoLine[0].length() == 8 && (8 == invoiceNoLine[1].length()))) {
                            // 数据校验不通过
                            checkResult = false;
                            break;
                        } else {
                            numberOne = Integer.parseInt(invoiceNoLine[0]);
                            numberEnd = Integer.parseInt(invoiceNoLine[1]);
                            // '-'两边的数据,前值大于后值时,格式错误
                            if (numberOne > numberEnd) {
                                checkResult = false;
                                break;
                            } else {
                                String stringOne = String.valueOf(invoiceNoLine[0]).substring(0, 3);
                                String stringEnd = String.valueOf(invoiceNoLine[1]).substring(0, 3);
                                // '-'两边的数据,前三位数字不一致时,格式错误
                                if (!stringOne.equals(stringEnd)) {
                                    checkResult = false;
                                    break;
                                }
                            }
                        }
                    }
                } else {
                    // 不包含"-"时,发票号码长度不为8时格式不正确
                    if (pdfInvoiceArr[k].length() != 8) {
                        checkResult = false;
                        break;
                    }
                }
            }
        }
        return checkResult;
    }

 

posted on 2019-08-20 16:39  小白菜好吃  阅读(496)  评论(0编辑  收藏  举报

导航