Thursday, 5 September 2013

PO Box RegEx At the Start of a String

PO Box RegEx At the Start of a String

I've looked around this site for a good PO Box regex and didn't find any
that I liked or worked consistently, so I tried my hand at making my
own... I feel pretty good about it, but I'm sure the kind folks here on SO
can poke some holes in it :) So... what problems do you see with this and
what false-positives/false-negatives can you think up that would get
through?
One caveat that I can see is that the PO Box pattern has to be at the
start of the string, but what else is wrong with it?
public bool AddressContainsPOB(string Addr)
{
string input = Addr.Trim().ToLower();
bool Result = false;
Regex regexObj1 = new
Regex(@"^p(ost){0,1}(\.){0,1}(\s){0,2}o(ffice){0,1}(\.){0,1}((\s){1}|b{1}|[1-9]{1})");
Regex regexObj2 = new Regex(@"^pob((\s){1}|[0-9]{1})");
Regex regexObj3 = new Regex(@"^box((\s){1}|[0-9]{1})");
Match match1 = regexObj1.Match(input);
if (match1.Success)
{ Result = true; }
Match match2 = regexObj2.Match(input);
if (match2.Success)
{ Result = true; }
Match match3 = regexObj3.Match(input);
if (match3.Success)
{ Result = true; }
return Result;
}

No comments:

Post a Comment