Using regex and android to classify different fields

I'm currently trying to make a business card scanner application. The idea here is to photograph the name card and extract the text and classify the text in another EditText.

I have already completed the OCR part, which extracts all text from an image with a name.

Now I am missing the regex method, which can extract all text from OCR and classify the name, email address, phone number into the corresponding fields in EditText.

Through some googling, I already found the regex formulas below:

private static final String EMAIL_PATTERN =
            "[a-zA-Z0-9\\+\\.\\_\\%\\-\\+]{1,256}" +
                    "\\@" +
                    "[a-zA-Z0-9][a-zA-Z0-9\\-]{0,64}" +
                    "(" +
                    "\\." +
                    "[a-zA-Z0-9][a-zA-Z0-9\\-]{0,25}" +
                    ")+";


private static final String PHONE_PATTERN =
            "^[89]\\d{7}$";


private static final String NAME_PATTERN =
            "/^[a-z ,.'-]+$/i";

I am currently just able to extract the email address using the method below:

public String EmailValidator(String email) {

        Pattern pattern = Pattern.compile(EMAIL_PATTERN);
        Matcher matcher = pattern.matcher(email);

        if (matcher.find()) {

            return email.substring(matcher.start(), matcher.end());

        } else {

            // TODO handle condition when input doesn't have an email address

        }

        return email;
    }

, ^ ^, EditText, (, , ).

-------------------------------------------- -------------------------------------------------

@Styx

, "textToUse" , :

enter image description here

. void, . String void, .

enter image description here

+4
1

. . , . , , , . , 1 . , . !

public void validator(String recognizeText) {

    Pattern emailPattern = Pattern.compile(EMAIL_PATTERN);
    Pattern phonePattern = Pattern.compile(PHONE_PATTERN);
    Pattern namePattern = Pattern.compile(NAME_PATTERN);

    String possibleEmail, possiblePhone, possibleName;
    possibleEmail = possiblePhone = possibleName = "";

    Matcher matcher;

    String[] words = recognizeText.split("\\r?\\n");

    for (String word : words) {
        //try to determine is the word an email by running a pattern check.
        matcher = emailPattern.matcher(word);
        if (matcher.find()) {
            possibleEmail = possibleEmail + word + " ";
            continue;
        }

        //try to determine is the word a phone number by running a pattern check.
        matcher = phonePattern.matcher(word);
        if (matcher.find()) {
            possiblePhone = possiblePhone + word + " ";
            continue;
        }

        //try to determine is the word a name by running a pattern check.
        matcher = namePattern.matcher(word);
        if (matcher.find()) {
            possibleName = possibleName + word + " ";
            continue;
        }
    }

    //after the loop then only set possibleEmail, possiblePhone, and possibleName into
    //their respective EditText here.

}
+4

Source: https://habr.com/ru/post/1626944/


All Articles