3

Is there a better way to do this? I cannot figure out how to add (?i) so I can make the pattern globally case insensitive while still keeping the statement as a negation.

[Required(ErrorMessage = "Address")]
[RegularExpression("^(?!.*(p|P)\\.?(o|O)\\.?\\s+?(box|Box|BOX)).*$", ErrorMessage = "We cannot ship to PO boxes")]
public string CustomerAddress1 { get; set; }
Arin
  • 1,373
  • 2
  • 10
  • 23
Slinky
  • 5,662
  • 14
  • 76
  • 130
  • Hard to understand this. The only alpha's in your regex are in the negative lookahead. If you want to make the whole thing case insensitive, why can't you just add the `(?i)` modifier to the beginning of the regex? If you are just talking about keeping the box part case sensitive, you can scope the sensitivity: `"(?i)^(?!.*(p)\\.?(o)\\.?\\s+(?-i:(box|Box|BOX))).*$"` –  Jun 15 '15 at 18:42

2 Answers2

3

I tested this out, and simply adding that (?i) to the beginning (as @sln says) works fine for me.

Here's my test code, in a console app:

static void Main(string[] args)
{
    TestForPoBox("PO BOX 111");
    TestForPoBox("P.O. Box 222");
    TestForPoBox("p.O. boX 333");
    TestForPoBox("444 Main Street");

    Console.ReadKey();
}

static void TestForPoBox(string streetAddress)
{            
    const string pattern = "(?i)^(?!.*p\\.?o\\.?\\s+?box).*$";
    Match match = Regex.Match(streetAddress, pattern);

    //Write out the matches
    if (match.Length > 0)
        Console.WriteLine("OK. Address is not a P.O. Box: " + streetAddress);
    else
        Console.WriteLine("INVALID. Address contains a P.O. Box: " + streetAddress);
}

and here's the output:

INVALID. Address contains a P.O. Box: PO BOX 111
INVALID. Address contains a P.O. Box: P.O. Box 222
INVALID. Address contains a P.O. Box: p.O. boX 333
OK. Address is not a P.O. Box: 444 Main Street


EDIT: I apologize; I had only tried this out on the purely-C# end. With MVC model validation like it appears you're doing, you'd need a Regex expression that works in both C# and JavaScript. By nature, C# supports (?i)... to signify case-insensitivity, while JavaScript supports /.../i. But neither notation will work in the other. Best you might be able to do is either what you already have (spelling out p|P, o|O, etc.), or else a custom RegularExpressionWithOptions attribute like Jeremy Cook's in the SO answer here.
Community
  • 1
  • 1
Arin
  • 1,373
  • 2
  • 10
  • 23
  • Weird. Causes a JS error for me (SyntaxError: invalid regexp group). Here is the generated output: – Slinky Jun 16 '15 at 13:37
  • I see what you mean; I had only tried it out in a C# console app, but MVC has different requirements. I edited my answer, though I'm afraid it may not be terribly helpful anymore. Still, Jeremy's custom attribute example I linked to could be useful! – Arin Jun 16 '15 at 17:29
  • I am trying it in a RegularExpressionValidator and getting this error: regular expression: /(?i)^(?!.*packstation).*$/: Invalid group at new RegExp () – Dejan Dozet Mar 06 '21 at 11:27
-1

You could use the .tolower() method on the string during comparison.

Blaargon
  • 88
  • 6
  • Welcome to Stack Overflow, if you improve the quality of your answer, you'll have a better change of it being accepted / upvoted. How to answer guide: http://stackoverflow.com/help/how-to-answer – Dan Beaulieu Jun 15 '15 at 18:08