-2

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;
}
Community
  • 1
  • 1
Cole W
  • 15,123
  • 6
  • 51
  • 85
  • Would help if you showed the c# you have so far and what the syntax error you get is. – Wobbles May 17 '16 at 16:01
  • 2
    Are you at least using `RegexOptions.IgnoreCase` if you removed `/i`? You also should use a verbatim string literal - `@"pattern"`, and replace ``\/`` with `/` – Wiktor Stribiżew May 17 '16 at 16:23
  • The pattern should work well in C#. If you write it correctly. – Wiktor Stribiżew May 17 '16 at 16:39
  • @Wobbles, Honestly the c# was so simple I didn't think it was necessary or essential to post this. I have posted it anyway. It wasn't a syntax error. It's the fact the regex doesn't recognize the inputs it says it should in the original stack overflow post – Cole W May 17 '16 at 16:53
  • Yes, but we need to check for syntax, typos, any any number of other things that could cause the expression to fail in c#. Also you do not indicate what the actual issue is, does it toss an exception, does it not find a match? if the latter can you provide a sample string to test it against as is suggested by [MCVE](http://stackoverflow.com/help/mcve) – Wobbles May 17 '16 at 16:56
  • @WiktorStribiżew After changing `\/` with `/` that seemed to resolve the issue. If you paste the full converted expression below as the answer I will accept it. Thanks for the help. – Cole W May 17 '16 at 17:08

1 Answers1

1

Since there is an /i modifier, you need to use RegexOptions.IgnoreCase. All \/ should be written as / as the forward slash is not a special regex character and not a regex delimiter (these do not exist in C# regex). Youl also need to use a verbatim string literal (i.e. @"pattern") form when converting a JS regex literal to C# pattern.

So, use

string PoBoxRegex = @"(?i)^ *((#\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+)";

The (?i) is an inline case insensitive flag.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563