title: How to extract a "registration" URL from a mail content
body: <p>I am successful in reading the content of the gmail-email using "JAVAMail" and I am able to store it in a string. Now I want to get a specific registration URL from the content (String). How can I do this, The String contains plenty of tags and href but I want to extract only the URL that is provided in a hyper link on a word " click here" that exist in the below mentioned statement</p>
  
  <pre><code>"Please &lt;a class="h5" href="https://newstaging.mobilous.com/en/user-register/******" target="_blank"&gt;click here&lt;/a&gt; to complete your registration".
  </code></pre>
  
  <p>on the hyper link "click here" the url </p>
  
  <p>href="https://newstaging.mobilous.com/en/user-register/******" target="_blank"</p>
  
  <p>I  have tried this by using the following code </p>
  
  <pre><code>package email;
  
  import java.util.ArrayList;
  import java.util.Properties;
  import java.util.regex.Matcher;
  import java.util.regex.Pattern;
  
  import javax.mail.Folder;
  import javax.mail.Message;
  import javax.mail.MessagingException;
  import javax.mail.NoSuchProviderException;
  import javax.mail.Session;
  import javax.mail.Store;
  
  public class emailAccess {
  
      public static void check(String host, String storeType, String user,
               String password) 
            {
               try {
  
               //create properties field
               Properties properties = new Properties();
  
               properties.put("mail.imap.host",host);
               properties.put("mail.imap.port", "993");
               properties.put("mail.imap.starttls.enable", "true");
               properties.setProperty("mail.imap.socketFactory.class","javax.net.ssl.SSLSocketFactory");
                 properties.setProperty("mail.imap.socketFactory.fallback", "false");
                 properties.setProperty("mail.imap.socketFactory.port",String.valueOf(993));
               Session emailSession = Session.getDefaultInstance(properties);
  
               //create the POP3 store object and connect with the pop server
               Store store = emailSession.getStore("imap");
  
               store.connect(host, user, password);
  
               //create the folder object and open it
               Folder emailFolder = store.getFolder("INBOX");
               emailFolder.open(Folder.READ_ONLY);
  
               // retrieve the messages from the folder in an array and print it
               Message[] messages = emailFolder.getMessages();
               System.out.println("messages.length---" + messages.length);
                  int n=messages.length;
                  for (int i = 0; i&lt;n; i++) {
                  Message message = messages[i];
                  ArrayList&lt;String&gt; links = new ArrayList&lt;String&gt;();
                  if(message.getSubject().contains("Thank you for signing up for AppExe")){
                  String desc=message.getContent().toString();
  
                // System.out.println(desc);
                Pattern linkPattern = Pattern.compile(" &lt;a\\b[^&gt;]*href=\"[^&gt;]*&gt;(.*?)&lt;/a&gt;",  Pattern.CASE_INSENSITIVE|Pattern.DOTALL);
                 Matcher pageMatcher = linkPattern.matcher(desc);
  
                 while(pageMatcher.find()){
                     links.add(pageMatcher.group());
                 } 
                  }else{
                  System.out.println("Email:"+ i + " is not a wanted email");
                  }
                  for(String temp:links){
                  if(temp.contains("user-register")){
                      System.out.println(temp);
                  }
                  }
  
                  /*System.out.println("---------------------------------");
                  System.out.println("Email Number " + (i + 1));
                  System.out.println("Subject: " + message.getSubject());
                  System.out.println("From: " + message.getFrom()[0]);
                  System.out.println("Text: " + message.getContent().toString());*/
  
               }
               //close the store and folder objects
               emailFolder.close(false);
               store.close();
  
               } catch (NoSuchProviderException e) {
                  e.printStackTrace();
               } catch (MessagingException e) {
                  e.printStackTrace();
               } catch (Exception e) {
                  e.printStackTrace();
               }
            }
  
  
      public static void main(String[] args) {
          // TODO Auto-generated method stub
  
          String host = "imap.gmail.com";
           String mailStoreType = "imap";
           String username = "rameshakur@gmail.com";
           String password = "*****";
  
           check(host, mailStoreType, username, password);
  
  
      }
  
  }
  </code></pre>
  
  <p>On executing I got the out put as </p>
  
  <p>&lt; class="h5" href="https://newstaging.mobilous.com/en/user-register/******" target="_blank"></p>
  
  <p>How can I extract only the href value i.e. <a href="https://newstaging.mobilous.com/en/user-register/" rel="nofollow">https://newstaging.mobilous.com/en/user-register/</a>******</p>
  
  <p>Please suggest, thanks.</p>