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:
- Receive the text output from another class (Already done)
- Pass Entire Text Output into
EmailValidator()
(Not sure if I did it correctly) - Take the output from
EmailValidator()
and pass it tosetupUI()
(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.