繁体   English   中英

Java正则表达式,其中String.match()为整数

[英]Java regex with String.match() for integers

我正在尝试从我的OCR文本检测器中获取某些数字。 到目前为止,我还无法成功提取具有特定格式的数字。 该数字通常看起来与此5225 6128 3265 4455相似,格式可能像这样XXXX XXXX XXXX XXXX

我需要一个字符串匹配正则表达式来获得这样的数字。 注意:图片来源中还有其他编号,我需要避免捕获这是方法

List<? extends Text> textComponents = text.getComponents();
for (Text currentText : textComponents) {
    float left = translateX(currentText.getBoundingBox().left);
    float bottom = translateY(currentText.getBoundingBox().bottom);
    canvas.drawText(currentText.getValue(),left,bottom,sTectPaint);

    // get certain type of text
    if(currentText !=null && currentText.getValue() != null) {
        if (currentText.getValue().matches("^[0-9]+(0-9]+[0-9]+[0-9]") || currentText.getValue().contains("0123456789")) {
            Log.e("number", currentText.getValue());
            myNum = "";
            myNum = currentText.getValue();
        }
    }
}                 

这样的东西? 这将匹配4组,每组各4位数字,并用空格隔开:

public static void main (String[] args) throws java.lang.Exception
{
    System.out.println(matches("1234 5678 1234 9872"));
    System.out.println(matches("1234 5678 1234 9872 3265"));
    System.out.println(matches("134 5678 1234 9872"));
    System.out.println(matches("1234 5678 14 982"));
    System.out.println(matches("1234"));
    System.out.println(matches("1234 5678 12345 9872"));
}

private static boolean matches(String text) {
    return text.matches("^\\d{4} \\d{4} \\d{4} \\d{4}");
}

输出:

true
false
false
false
false
false

但是,根据您输入的内容,您可能需要稍微更改正则表达式。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM