开发者

What regex can I use to match only letters, numbers, and one space between each word?

How can I create a regex expression that will match only letters and numbers, and one spa开发者_如何学运维ce between each word?

Good Examples:

Amazing

Hello World

I am 500 years old

Bad Examples:

Hello  world

I am 500 years      old.

I    am   Chuck Norris


Most regex implementations support named character classes:

^[[:alnum:]]+( [[:alnum:]]+)*$

You could be clever though a little less clear and simplify this to:

^([[:alnum:]]+ ?)*$

FYI, the second one allows a spurious space character at the end of the string. If you don't want that stick with the first regex.

Also as other posters said, if [[:alnum:]] doesn't work for you then you can use [A-Za-z0-9] instead.


([a-zA-Z0-9]+ ?)+?


^([a-zA-Z0-9]+\s?)*$

its works


^[a-zA-Z]+([\s][a-zA-Z]+)*$


(?:[a-zA-Z0-9]+[ ])+[a-zA-Z0-9]+

If I understand you correctly the above regex should work. See screenshot below:

screenshot http://img136.imageshack.us/img136/6871/screenshotkiki056.png


This would match a word

'[a-zA-Z0-9]+\ ?'
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜