[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 785
  • Last Modified:

java string and wildcards

How can I check that a string is a valid email address?  Would I use some kind of wildcard to check?

for example lets say we have an email address:  someone@yahoo.com

and i want to make sure the string has some text, then an @ symbol, then some text, then a period, then some text.
The idea of wild card that I was thinking would be something like this:

String email = "someone@yahoo.com";
if(email.compareToIgnoreCase("'%@%.%'") == 0) {
    System.out.println("Email is valid");
}

I haven't tested this just because I don't know if java supports wildcards.

Any suggestions would be greatly appreciated
0
tbboyett
Asked:
tbboyett
1 Solution
 
MilanKMCommented:
yes it supports wildcards. You've to use the java.util.regex package for wildcard chars. try out the following code.

import java.util.regex.*;
class regexSample
{
   public static void main(String args[])
   {
      String email = "xyz@hotmail.com";
      Pattern p = Pattern.compile(".+@.+\\.[a-z]+");
      Matcher m = p.matcher(email);  //Match the given string with the pattern
      boolean matchFound = m.matches(); //check whether match is found
      if (matchFound)
        System.out.println("Valid Email Id.");
      else
        System.out.println("Invalid Email Id.");
   }
}

Also see the following link, where you can find another email validation in Java
http://java.sun.com/developer/technicalArticles/releases/1.4regex/

Thanks
MilanKM
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now