?
Solved

Strange (to me) Regex Error...

Posted on 2009-04-29
5
Medium Priority
?
801 Views
Last Modified: 2012-05-06
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

0
Comment
Question by:b_rad_wright
  • 3
  • 2
5 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 2000 total points
ID: 24263231
Why are you not using java.util.regex package?
0
 

Author Comment

by:b_rad_wright
ID: 24263403
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
 

Author Comment

by:b_rad_wright
ID: 24263634
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 24263653
>>but I encountered another little error

That's the same error really - from not using the java.util.regex package
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 24263726
:-)
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
What You Need to Know when Searching for a Webhost Provider
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses
Course of the Month15 days, 12 hours left to enroll

850 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