Java – Use regular expressions and android to classify different fields

Use regular expressions and android to classify different fields… here is a solution to the problem.

Use regular expressions and android to classify different fields

I am currently trying to make a business card scanner app. The idea here is to take a photo of a business card that extracts the text and classifies the text into different EditTexts.

I’ve done the OCR part of extracting all the text from the business card image.

What I’m missing now is making a regular expression method that can extract the entire text from OCR and sort names, email addresses, phone numbers into the corresponding fields in EditText.

By googling, I have found the following regular expression formula:

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

Currently I’m just able to extract email addresses using:

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

I’m not sure how to edit ^above method^ to include using all 3 regular expression patterns at once and displaying them to different EditText fields like (name, email address, phone number).

——————————————– Edit – –

Use @Styx after answering

There is a problem with the parameters, how I used to pass the text “textToUse” to the method as follows:

enter image description here

I also tried passing text to all three parameters. But since the method is void, it cannot be completed. Or, if I change the method to String instead of void, it will require a return value.

enter image description here

Solution

Try this code. The function accepts recognized text and splits it with a broken line symbol. Then run a loop and run the mode check to determine the type of content. Whenever a pattern is determined, the loop will use the continue keyword to enter the next iteration. This code can also handle cases where one or more email and phone numbers appear on a business card. Hope that helps. Cheers!

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.

}

Related Problems and Solutions