Why is this Regex not working in .NET?

I want the regex to check that the string starts with a letter followed by some letters, numbers or underscores. According to my regex EditPadPro parser, the next test should pass. But this is not so.

Regex.IsMatch("Class1_1", @"^\w[\w|\d|_]*$").ShouldBeTrue();

What am I missing?

+3
source share
2 answers

Your regular expression works, but does not do what you think.

You have to use

Regex.IsMatch("Class1_1", @"^[A-Za-z]\w*$")

(tested)

+5
source

\ w includes \ d and underscore - even if your test passes, Regex will not check what you want!

+3
source

Source: https://habr.com/ru/post/1742843/


All Articles