开发者

Regex not working correctly

开发者 https://www.devze.com 2023-03-18 01:34 出处:网络
I have this regex. It\'s supposed to match all one character, and also all one character along with a space.

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

精彩评论

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

关注公众号