开发者

address parsing and validation technique required for Australia zone with regex

We need to implement the validation for the following field

Street Address/ Business Address

Conditions which needs to be taken care are as below

Post office box, private bag NOT acceptable as address. The address field should not start with the following:

NOTE: ^ = space

G^P^O    
GPO^    
G.P.O    
GPO.    
G.P.O. Box

P^O    
P.O    
PO^    
PO.    
P.O.    
P.O.B    
POBOX    
POST BOX    
POST OFFICE BOX    
P / O Box    
P/O Box    
P O Box    开发者_运维知识库
P.O. Box

BOX^    
BOX.    
Private Bag^    
Private Bag.    
Locked Bag

This list needs to be configurable to allow for additional rules to be included at a later date. There is no need to validate for upper/lower case

Can you suggest me this kind of validation can be better implement using Javascript or Java?

  1. if I use Java, is it advisable to use Regex class

  2. If Javascript, what should be my way

Kindly share your sample code if you have any

I am checking if we use of Regex would be helpful in doing this?

ps: Since this project cant use any google or yahoo api or any other paid APIs to parse the street address.


I wouldn't use regexp in that case. Instead I'd put all "stop words" in a file, read it into a set and use a loop to verify:

public static boolean isValid(String address) {
  Set<String> stopWords = getSet();  // some magic to get the loaded set
  for (String stopWord:stopWords) {
    if (address.trim().toLowerCase().startsWith(stopWord.toLowerCase())) {
       return false;
    }
  }
  return true;
}

Big advantage: the stop words are maintained in a file and not compiled into some regular expression that no one will understand next week and later.


Not a regex guru my self, so i my answer will be the long way of doing it.

you can include the list of possible values in a regex:

/(G\sP\sO)|(GPO\s)|(G\.P\.O)|(G\.P\.O\.\sBox)| .... and so one

I don't think it's the most efficient way of solving the problem but it will still work.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜