-1

I need a regex which matches pattern name1\name2 with the restriction that name1 must not contain some special characters such as < , >. name1, name2 can have spaces.

I am using this regex and it seems to work fine in java script :

/^[^ &<>;]+\\./

In my C sharp code, I am using the regex below:

var pattern= @"^[^ &<>;]+\\.";

The C sharp results fail for the input : 8 [ } \ ;
where as it passes for javascript. How can I get similar results ?

6
  • Javascript: regular-expressions.info/javascript.html C#: regular-expressions.info/dotnet.html Commented Jul 19, 2018 at 10:34
  • The patterns are equivalent. They work the same way in JS and .NET. That is, equally wrong. Or, you have provided incorrect requirements. Please clarify. Commented Jul 19, 2018 at 10:36
  • 3
    The @ decorator means the C# regex contains the two characters \\ where only \ was intended. Commented Jul 19, 2018 at 10:37
  • @AlexK. A double backslash matches a single backslash in a string, it is not a problem. @"\\" = /\\/. Commented Jul 19, 2018 at 10:38
  • @AlexK. Your comment is wrong, please remove. @"\." matches a dot, while @"\\." matches a \ and any char. The JS pattern matches the same: /\\./ matches a \ followed with any char. Commented Jul 19, 2018 at 11:43

1 Answer 1

1

Problem with the example that you've given, you ommited space from list of allowed characters. For your example this pattern works:

var pattern = @"^[^&<>;]+\\.";
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you ! didn't notice the space.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.