I saw the following javascript regex on a stack overflow article and I would like to convert it to C#:
var poBox = /^ *((#\d+)|((box|bin)[-. \/\\]?\d+)|(.*p[ \.]? ?(o|0)[-. \/\\]? *-?((box|bin)|b|(#|num)?\d+))|(p(ost)? *(o(ff(ice)?)?)? *((box|bin)|b)? *\d+)|(p *-?\/?(o)? *-?box)|post office box|((box|bin)|b) *(number|num|#)? *\d+|(num|number|#) *\d+)/i;
I've tried removing the / at the beginning and end which I believe is used to denote a string literal and I also removed /i at the end which seems to be for case insensitivity but I still seem to be missing other conversions here.
The original stack overflow article is here: PO Box Regular Expression Validation
Can anyone tell me what else I'm missing here? Not super familiar with regular expressions and I'm struggling a bit here.
Edit:
Here is the modified c# code with the above notes:
public const string PoBoxRegex = @"^ *((#\d+)|((box|bin)[-. \/\\]?\d+)|(.*p[ \.]? ?(o|0)[-. \/\\]? *-?((box|bin)|b|(#|num)?\d+))|(p(ost)? *(o(ff(ice)?)?)? *((box|bin)|b)? *\d+)|(p *-?\/?(o)? *-?box)|post office box|((box|bin)|b) *(number|num|#)? *\d+|(num|number|#) *\d+)";
public static bool IsPoBox(string addressToCheck)
{
if (string.IsNullOrWhiteSpace(addressToCheck))
{
return false;
}
if (Regex.IsMatch(addressToCheck, PoBoxRegex, RegexOptions.IgnoreCase))
{
return true;
}
return false;
}