Strange (to me) Regex Error...

Hi -

I'm not the sharpest tool in the shed when it comes to Regex.  Very new to it.  Using Regex to "validate" email field in a servlet.  Getting the following error.

The method compile(ClassGenerator, MethodGenerator) in the type Expression is not applicable for the arguments (String)       The method matcher(String) is undefined for the type Pattern.

Part of my servlet code is below.  Can't figure out what my problem is.  

I've tried using a very simple regex incase there is an error in the expression below but that doesn't work either.  Help!

PS - see line 37-40
package cs320;
 
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
 
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
 
import com.sun.org.apache.xalan.internal.xsltc.compiler.Pattern;
import com.sun.org.apache.xerces.internal.impl.xs.identity.Selector.Matcher;
 
 
public class Login extends HttpServlet {
	private static final long serialVersionUID = 1L;
 
	public void init(){
 
		if(getServletContext().getAttribute("books")==null){
			getServletContext().setAttribute("books",new ArrayList<Books>());
		}
		
	}
 
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		PrintWriter out = response.getWriter();
		HttpSession s = request.getSession(); 
		
		if (request.getParameter("submit")!=null){
			s.setAttribute("fname", request.getParameter("fname").trim().toLowerCase());
			s.setAttribute("lname", request.getParameter("lname").trim().toLowerCase());
				Pattern pattern = Pattern.compile("\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,4}\b"); //took this regex from another program
				String email = request.getParameter("email").trim().toLowerCase();
	            Matcher matcher = pattern.matcher(email);
	    
		}

Open in new window

b_rad_wrightAsked:
Who is Participating?
 
CEHJConnect With a Mentor Commented:
Why are you not using java.util.regex package?
0
 
b_rad_wrightAuthor Commented:
Simple.  Stupid mistake.  I'm actually still tweaking this.  I just found another post to help me but I encountered another little error - my Matcher matcher = pattern.matcher(email) method call is giving me  "Type mismatch - cannot convert from Matcher to Selector.Matcher"

            PrintWriter out = response.getWriter();
            HttpSession s = request.getSession();
            String error = "";
            if (request.getParameter("submit")!=null){
                        java.util.regex.Pattern pattern = java.util.regex.Pattern.compile("\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,4}\b");
                        String email = request.getParameter("email").trim().toLowerCase();
                  Matcher matcher = pattern.matcher(email);
                  boolean validE = matcher.matches();
                  if(validE){
                        s.setAttribute("fname", request.getParameter("fname").trim().toLowerCase());
                            s.setAttribute("lname", request.getParameter("lname").trim().toLowerCase());
                            s.setAttribute("email", email);
                            response.sendRedirect("ViewBooks");
                  }else error = "Please enter a valid Name and Email.";
                        
            }
0
 
b_rad_wrightAuthor Commented:
Problem solved.  I let my ide handle my imports and it messed me up.  

For all rookies looking for help with regex - don't use the ide imports.  
0
 
CEHJCommented:
>>but I encountered another little error

That's the same error really - from not using the java.util.regex package
0
 
CEHJCommented:
:-)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.