Results 1 to 7 of 7
Thread: regex house address
- 05-07-2011, 08:12 PM #1
regex house address
I want my regex to be able to handle 123 Rock Rd or 444 Blue Bird Ln.
Here is my attempt:
The output keeps generating invalid input when I enter 444 Blue Bird Ln: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]+"); }
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.
- 05-07-2011, 10:45 PM #2
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.
- 05-07-2011, 10:52 PM #3
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]".
- 05-08-2011, 04:13 AM #4
As posted, the two are equal. Maybe you needed to put the regexes in code tags to preserve a difference.The regex "[A-Za-z] [A-Za-z]" is far from equal to "[A-Za-z] [A-Za-z]".
db
- 05-08-2011, 10:50 AM #5
Oops. It stole my spaces :( Ahem.
The regexis far from equal toJava Code:[A-Za-z] [A-Za-z]
Java Code:[A-Za-z] [A-Za-z]
- 05-08-2011, 02:04 PM #6
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]*");
- 05-08-2011, 02:10 PM #7
Similar Threads
-
Help with House/Fence Applet.
By GA Scooby in forum Java AppletsReplies: 15Last Post: 04-11-2011, 09:58 AM -
building a house but having problems connecting it
By youngflames in forum New To JavaReplies: 9Last Post: 01-26-2010, 06:00 PM -
build a house with windows and a door
By youngflames in forum New To JavaReplies: 3Last Post: 01-22-2010, 01:33 PM -
Hello gurus' in the house
By javahsm in forum New To JavaReplies: 0Last Post: 11-29-2008, 06:11 PM -
building a house
By dc2acgsr99 in forum Java AppletsReplies: 4Last Post: 03-07-2008, 11:18 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks