java string and wildcards

Posted on 2006-05-18
Last Modified: 2010-10-05
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:

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 = "";
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
Question by:tbboyett
    1 Comment
    LVL 14

    Accepted Solution

    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 = "";
          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.");
            System.out.println("Invalid Email Id.");

    Also see the following link, where you can find another email validation in Java


    Featured Post

    Find Ransomware Secrets With All-Source Analysis

    Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

    Join & Write a Comment

    Suggested Solutions

    Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
    Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
    In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now