开发者

Why doesn't this Regex work in .NET?

开发者 https://www.devze.com 2022-12-28 00:22 出处:网络
I want a regex that will verify that a string begins with a letter followed by some letters, numbers, or underscore开发者_如何学编程s.According to my EditPadPro regex parser the following test should

I want a regex that will verify that a string begins with a letter followed by some letters, numbers, or underscore开发者_如何学编程s. According to my EditPadPro regex parser the following test should pass. But it does not.

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

What am I missing?


Your regex works, but doesn't do what you think it does.

You should use

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

(Tested)


\w includes \d and underscore - even if your test passes, the Regex won't be testing what you want it to!

0

精彩评论

暂无评论...
验证码 换一张
取 消