title: Using Android Pattern and Matcher class (Regex)
body: <p>I have just picked up Android, but am tasked to help with a project for my internship.</p>
  
  <p>Lets say I have the details below:</p>
  
  <pre><code>Fonia Taylo
  Product Manager
  
  foniataylo@gmail.com
  98706886
  </code></pre>
  
  <p>From the details I have above, I want to pass it into a class whereby I can then filter out the email address using regex, and pass <strong>only</strong> this filtered out email address to an EditText.</p>
  
  <p>I have searched many tutorials on regex, especially on Android Pattern and Matcher classes. </p>
  
  <p>But all the examples I have found are only for validation for the <strong>text entered into an EditText field only.</strong></p>
  
  <p>What I need to do is:</p>
  
  <ol>
  <li>Validate the entire text as shown above</li>
  <li>Filter out the email address using the regex (and delete the rest of the text)</li>
  <li>Pass <strong>only</strong> this email address to an EditText</li>
  </ol>
  
  <p>Currently below is my class:</p>
  
  <pre><code>public class RegexOCR1 extends Activity {
  
      private Pattern pattern;
      private Matcher matcher;
  
      private String recognizedText, textToUse;
  
      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 from another class
          Bundle extras = this.getIntent().getExtras();
          recognizedText = extras.getString("TEXT");
          textToUse = recognizedText;
  
      }
  
      @Override
      public void onConfigurationChanged(Configuration newConfig) {
          super.onConfigurationChanged(newConfig);
          setContentView(R.layout.usetext);
          showText();
          //Log.i(TAG, "onConfigChanged");
      }
  
      private void showText(){
          //Log.i(TAG, "ShowText");
          Intent intent = new Intent(this, CreateContactActivityOCR.class);
          startActivity(intent);
      }
  
      public EmailValidator() {
      Pattern pattern = Pattern.compile(EMAIL_PATTERN);
      Matcher matcher = pattern.matcher(textToUse);
      if (matcher.find())
      {
          String email = textToUse.substring(matcher.start(), matcher.end());
  
  
      } else {
          // TODO handle condition when input doesn't have an email address
      }
      }
  
      public boolean validate(final String hex) {
  
          matcher = pattern.matcher(hex);
          return matcher.matches();
  
      }
  }
  </code></pre>
  
  <p>As you can see, it is pretty much incomplete. I would like to pass "textToUse" into the regex validation, and then continue to perform the function as stated above.</p>
  
  <p>Edit:</p>
  
  <p>After the following method:</p>
  
  <pre><code>public EmailValidator() {
          Pattern pattern = Pattern.compile(EMAIL_PATTERN);
          Matcher matcher = pattern.matcher(textToUse);
          if (matcher.find())
          {
              String email = textToUse.substring(matcher.start(), matcher.end());
  
  
          } else {
              // TODO handle condition when input doesn't have an email address
          }
          }
  </code></pre>
  
  <p>which extract out the email address; How do I then pass this extracted email address to through intent to an <strong>EditText</strong> that is in <strong>another Class</strong>?</p>
  
  <p>Please let me know you how I can change my code if there are any ideas. Thank you!</p>