Donovan Tan
Donovan Tan

Reputation: 326

Android: Passing text through Regex method, and displaying on EditText

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:

Genos Taper
Assitant Manager

90606968
[email protected]

I want to pass the above text output into the regex method and extract out "[email protected]" 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)

Where did it went wrong? The passing of the text output, or the regex method? Any ideas please provide. Thanks~~

Upvotes: 0

Views: 630

Answers (1)

Sreehari
Sreehari

Reputation: 5655

you are not assigning the returning value to textToUse

textToUse= EmailValidator();

Upvotes: 1

Related Questions