开发者

Regex not working correctly

I have this regex. It's supposed to match all one character, and also all one character along with a space.

For example:

wwwwwwww - MATCH
www wwwwww - MATCH
@@@@@ - MATCH
wwwwqq - NOT MATCH
wwww q开发者_StackOverflow社区qqq - NOT MATCH

But it's not matching things like:

@@@@@@@
.......

What's wrong with it? Here it is below:

var match = Regex.Match(message, @"^\s*(\w)(?:\1|\s)*$");


Because @ and . are not "word characters". Couldn't you just match with (.) ?

var match = Regex.Match(message, @"^\s*(.)(?:\1|\s)*$");

You could also try with \S (non white-space character).


\w is shorthand for a "word character," which does not include punctuation like @ or ..

Dot (.) signifies any character; \S signifies non-whitespace.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜