1

I am trying to pass a text output into a regex method whereby I can take this entire text output and extract out the email address.

For example, I have the text output below:

Taria Joseph
General Manager

96523325
tariajoseph@hotmail.com

I want to pass the above text output into the regex method and extract out "tariajoseph@hotmail.com" and display into an EditText.

Below are my codes:

public class CreateContactActivityOCRtest extends Activity {

private String recognizedText, textToUse;
private EditText mEditText1, mEditText2;
private String mFromLang, mCurrentLang;

private Pattern pattern;
private Matcher matcher;

private static final String EMAIL_PATTERN =
        "^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@"
                + "[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_createcontact);

    // Getting the path of the image to show
    Bundle extras = this.getIntent().getExtras();
    recognizedText = extras.getString("TEXT");
    textToUse = recognizedText;

    // Getting the language used for text recognition
    mFromLang = extras.getString("LANG");
    mCurrentLang = mFromLang;
    //Log.i(TAG, mFromLang);

    textToUse = EmailValidator();
    setupUI();
}

public String EmailValidator() {

    String email = textToUse;

    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;

}

public boolean validate(final String hex) {

    matcher = pattern.matcher(hex);
    return matcher.matches();

}

public void setupUI(){

    // Setting up the textbox

    mEditText1 = (EditText)findViewById(R.id.EmailET);
    mEditText2 = (EditText)findViewById(R.id.role);
    mEditText1.setText(textToUse);
    mEditText2.setText(textToUse);

}

}

What I am trying to do is:

  1. Receive the text output from another class (Already done)
  2. Pass Entire Text Output into EmailValidator() (Not sure if I did it correctly)
  3. Take the output from EmailValidator() and pass it to setupUI() (Not done)

Can someone help me check on which part did it go wrong? Is it the passing of methods, or error in my regex method (EmailValidator())?

Currently when testing on the device, the page straight away display the entire text output to the EditText.

Daniel Louis
  • 63
  • 1
  • 1
  • 6

1 Answers1

0

you can check email validation Here. change your code like as below

protected void onCreate(Bundle savedInstanceState) {    
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_createcontact);   
mEditText1 = (EditText)findViewById(R.id.EmailET);
mEditText2 = (EditText)findViewById(R.id.role);
Bundle extras = this.getIntent().getExtras();
if (extras != null) {
recognizedText = extras.getString("TEXT");
mFromLang = extras.getString("LANG");
textToUse = recognizedText;  
mCurrentLang = mFromLang;   
// textToUse = EmailValidator(textToUse);

if(isValidEmail(recognizedText))
{
textToUse=recognizedText   
 }
else
{
//this is invalid email part
}

setupUI();      
}


public final static boolean isValidEmail(CharSequence target) {
if (TextUtils.isEmpty(target)) {
return false;
} else {
return android.util.Patterns.EMAIL_ADDRESS.matcher(target).matches();
}
}


 public void setupUI(){    
 mEditText1.setText(textToUse);
 mEditText2.setText(textToUse);
 }
Community
  • 1
  • 1
sasikumar
  • 12,540
  • 3
  • 28
  • 48
  • Hi, really appreciate your answer :) As I mentioned in my question, I want the entire text output to go through the method 'EmailValidator()' which will extract out only the email address and display it to 'setupUI()' EditText. However after using your codes, it still displays the entire text output to EditText. Does it mean that my method 'EmailValidator()' does not work? – Daniel Louis Jan 25 '16 at 07:45
  • I have tried your edited codes, and also with the one line solution from @mindriot answer in the link you provided. Additionally also tried to change my email pattern regex from this page: http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/2.3.5_r1/android/util/Patterns.java. However, it still display the entire text output to the EditText. p.s. thanks a lot for your answer – Daniel Louis Jan 25 '16 at 09:47
  • it still does not display just the extracted email address though, hope I can find out what's wrong. But I will accept your answer as it seems to be correct. Thanks a lot :) – Daniel Louis Jan 25 '16 at 10:04