Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular expression for address field validation

Tags:

I am trying to write a regular expression that facilitates an address, example 21-big walk way or 21 St.Elizabeth's drive I came up with the following regular expression but I am not too keen to how to incorporate all the characters (alphanumeric, space dash, full stop, apostrophe)

"regexp=^[A-Za-z-0-99999999' 
like image 447
dames Avatar asked Jul 12 '12 16:07

dames


People also ask

What is the regular expression for address?

\s(\b\w*\b\s){1,2}\w*\. This allows 1-5 digits for the house number, a space, a character followed by a period (for N. or S.), 1-2 words for the street name, finished with an abbreviation (like st. or rd.). to allow for a dash but not require one.

Which of the regular expression can validate an email address?

Email Regex – Simple Validation. This example uses a simple regex ^(. +)@(\S+)$ to validate an email address. It checks to ensure the email contains at least one character, an @ symbol, then a non whitespace character.

Can we use below given regular expression to validate an email address?

You should not use regular expressions to validate email addresses.

How do you validate an address in Java?

We can use InetAddressValidator class that provides the following validation methods to validate an IPv4 or IPv6 address. isValid(inetAddress) : Returns true if the specified string is a valid IPv4 or IPv6 address. isValidInet4Address(inet4Address) : Returns true if the specified string is a valid IPv4 address.


2 Answers

See the answer to this question on address validating with regex: regex street address match

The problem is, street addresses vary so much in formatting that it's hard to code against them. If you are trying to validate addresses, finding if one isn't valid based on its format is mighty hard to do. This would return the following address (253 N. Cherry St. ), anything with its same format:

\d{1,5}\s\w.\s(\b\w*\b\s){1,2}\w*\. 

This allows 1-5 digits for the house number, a space, a character followed by a period (for N. or S.), 1-2 words for the street name, finished with an abbreviation (like st. or rd.).

Because regex is used to see if things meet a standard or protocol (which you define), you probably wouldn't want to allow for the addresses provided above, especially the first one with the dash, since they aren't very standard. you can modify my above code to allow for them if you wish--you could add

(-?) 

to allow for a dash but not require one.

In addition, http://rubular.com/ is a quick and interactive way to learn regex. Try it out with the addresses above.

like image 76
Skitterm Avatar answered Sep 18 '22 17:09

Skitterm


In case if you don't have a fixed format for the address as mentioned above, I would use regex expression just to eliminate the symbols which are not used in the address (like specialized sybmols - &(%#$^). Result would be:

[A-Za-z0-9'\.\-\s\,] 
like image 22
Serzas Avatar answered Sep 20 '22 17:09

Serzas