0

I Am trying to get certain numbers from my OCR text detector. So far I have not been able to successfully extract the numbers with a certain format. The number looks similar to this 5225 6128 3265 4455 generally, the format could be like this XXXX XXXX XXXX XXXX

I need a string match regex to get such a number. NOTE: There are other numbers from picture source I need to avoid capturing Here is the method

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();
        }
    }
}                 
Gama11
  • 31,714
  • 9
  • 78
  • 100
Job M
  • 3,331
  • 2
  • 19
  • 26

1 Answers1

0

Something like that? This will match 4 groups of exactly 4 digits each, separated by a whitespace:

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

Output:

true
false
false
false
false
false

Depending on what your input exactly looks like you might need to alter the regular expression a bit, though.

Marvin
  • 13,325
  • 3
  • 51
  • 57