Results 1 to 7 of 7
  1. #1
    paul1024's Avatar
    paul1024 is offline Member
    Join Date
    Mar 2011
    Posts
    13
    Rep Power
    0

    Default regex house address

    I want my regex to be able to handle 123 Rock Rd or 444 Blue Bird Ln.

    Here is my attempt:

    Java Code:
            //accept strings that are in the format of 123 Rock Rd or 444 Blue Bird Ln (not case sensitive)
    	public static boolean validateAddr(String address)
    	{
    		return address.matches("\\d+ \\s+ [a-zA-Z]+ | ([a-zA-Z]+ \\s+ [a-zA-Z]+) \\s+ [a-zA-Z]+");
    	}
    The output keeps generating invalid input when I enter 444 Blue Bird Ln:

    Java Code:
    	
                            //ask the user for the address until they enter valid input
    			do
    			{
    				System.out.println("Enter the address: ");
    				addr = in.nextLine();
    				
    				if(ValidateInput.validateAddr(addr))
    					addrbk.setAddress(addr);
    				
    				else
    					System.out.println("Invalid input. \n");
    			}while(!ValidateInput.validateAddr(addr));
    Last edited by paul1024; 05-07-2011 at 10:55 PM.

  2. #2
    Toll's Avatar
    Toll is offline Senior Member
    Join Date
    May 2011
    Location
    Sweden
    Posts
    392
    Rep Power
    4

    Default

    The first question to ask is: How does an address look? Does it have any rules aside from it having to start with a number? Does it have to end in "rd" or "ln"? Once you make that definition, making a regex becomes much easier.

  3. #3
    Toll's Avatar
    Toll is offline Senior Member
    Join Date
    May 2011
    Location
    Sweden
    Posts
    392
    Rep Power
    4

    Default

    Something else to note when dealing with regex is that every character counts. It's not like Java, where you can have any amount of whitespaces without it changing the code. For instance, "X=5" is identical, codewise, to "X = 5". The regex "[A-Za-z] [A-Za-z]" is far from equal to "[A-Za-z] [A-Za-z]".

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,234
    Rep Power
    19

    Default

    The regex "[A-Za-z] [A-Za-z]" is far from equal to "[A-Za-z] [A-Za-z]".
    As posted, the two are equal. Maybe you needed to put the regexes in code tags to preserve a difference.

    db

  5. #5
    Toll's Avatar
    Toll is offline Senior Member
    Join Date
    May 2011
    Location
    Sweden
    Posts
    392
    Rep Power
    4

    Default

    Oops. It stole my spaces :( Ahem.
    The regex
    Java Code:
    [A-Za-z] [A-Za-z]
    is far from equal to
    Java Code:
    [A-Za-z]    [A-Za-z]

  6. #6
    paul1024's Avatar
    paul1024 is offline Member
    Join Date
    Mar 2011
    Posts
    13
    Rep Power
    0

    Default

    Thanks for the tip. Eliminating the gaps (spaces) in my code did the trick. I also had to add more parenthesis. How does Java interpret gaps (spaces) in regex code? Here is my solution:

    Java Code:
    return address.matches("\\d+\\s+(([a-zA-Z])+|([a-zA-Z]+\\s+[a-zA-Z]+))\\s+[a-zA-Z]*");

  7. #7
    Toll's Avatar
    Toll is offline Senior Member
    Join Date
    May 2011
    Location
    Sweden
    Posts
    392
    Rep Power
    4

    Default

    If there's a space in the regex, Java (and all other languages that I can think of) expects a space in the parsed string as well.

Similar Threads

  1. Help with House/Fence Applet.
    By GA Scooby in forum Java Applets
    Replies: 15
    Last Post: 04-11-2011, 09:58 AM
  2. building a house but having problems connecting it
    By youngflames in forum New To Java
    Replies: 9
    Last Post: 01-26-2010, 06:00 PM
  3. build a house with windows and a door
    By youngflames in forum New To Java
    Replies: 3
    Last Post: 01-22-2010, 01:33 PM
  4. Hello gurus' in the house
    By javahsm in forum New To Java
    Replies: 0
    Last Post: 11-29-2008, 06:11 PM
  5. building a house
    By dc2acgsr99 in forum Java Applets
    Replies: 4
    Last Post: 03-07-2008, 11:18 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •