0

I have to validate a string A123456(T) using System.Text.RegularExpressions.Regex.IsMatch in C#. i am using below reg. expression.

"^[A-Za-z]\d{6}\([A-Za-z0-9]\)$"

i have tested it online it is working as expected. but when i am using it with System.Text.RegularExpressions.Regex.IsMatch("A123456(T)","^[A-Za-z]\d{6}\([A-Za-z0-9]\)$") it always returns false. my problem is this i cant use any other method. i have to use only this method.

Rahul
  • 269
  • 2
  • 10
  • 25
  • It should be `@"^[A-Za-z]\d{6}\([A-Za-z0-9]\)$"`. Don't you get any errors? – Wiktor Stribiżew Jun 15 '17 at 14:06
  • 1
    `Console.WriteLine($"{Regex.IsMatch("A123456(T)", "^[A-Za-z]\\d{6}\\([A-Za-z0-9]\\)$")}");` works fine for me. – ThePerplexedOne Jun 15 '17 at 14:06
  • Either use a verbatim string as @WiktorStribiżew mentions or delimit the backslashes. – juharr Jun 15 '17 at 14:06
  • Regex101 assumes any backslash characters are supposed to be regex escape characters. If you're working in C#, though, it'll first assume they're _string_ escape characters. `\d` in a string is a meaningless character, so a few of us are surprised it's giving you `false` rather than outright throwing an exception, but however you seem to be doing it, `\d` in your string is _not_ converting to `\d` in your regex. Same with the other escaped characters, unless you include the `@` literal as Wiktor suggested. – Harris Jun 15 '17 at 14:23
  • 1
    Moreover, there is little sense relying on Web regex tester testing. Always use the target environment, or at least those regex testers that are compatible with your regex flavor. Regex101 does not support .NET regex syntax, and does not generate code snippets for C#. I can recommend Ultrapico Expresso (no affiliation, a regular user for > 7 years) that works similarly to regex101 and can generate C# code snippets for you. – Wiktor Stribiżew Jun 15 '17 at 15:00

0 Answers0