-1

I am having this below regex pattern. I want to allow these below characters as special characters.

  • {
  • }
  • [
  • ]

Regex regEx = new Regex(@"^(?=.*[A-Z])(?=.*[!@#$%^&*()+_<>~-])(?=.*[0-9])(?=.*[a-z]).{8,15}$");

Is there a way to add them

sgl
  • 563
  • 1
  • 6
  • 16

1 Answers1

-1

You'll need to escape them within the regex. Within a regular expression, you can refer to "\[" to get a '[' character, for example.

Note that in C, C#, C++, Java, etc, you usually need to double up the '\' character to escape the escape character. So when adding '[' to your regex, you would actually use "\\[". In your case you're using an @"" so you've escaped that trap.

jwismar
  • 12,164
  • 3
  • 32
  • 44
  • 2
    OP is using an `@` prefixed string - no need for doubling up slashes – James Thorpe Oct 30 '18 at 13:10
  • None of the chars OP wants to add requires escaping if placed correctly inside the pattern. If they should be added at all since the current pattern already "allows" any char but line break chars. – Wiktor Stribiżew Oct 30 '18 at 13:11
  • @JamesThorpe - you're right. In the regex hash, I missed that. I'll update response. – jwismar Oct 30 '18 at 13:16
  • 1
    @WiktorStribiżew I think he has a bug or two, but I'm trying to address what I believe is the underlying question. – jwismar Oct 30 '18 at 13:16