开发者

i want upgrade my regex to let user write . or number but not alone and not write . in last of word

I want to upgrade my regex to let user write . or number but not alone and not write . in last of word

Example: I want to write

hello to web 2.0

or hello farah.开发者_如何学Pythonnor

but not hello word.

and not ..................... or 213123213123123

Here is my regex

preg_match('#^[a-z0-9. ]+$#i',$text)


You're nearly there:

preg_match('#^[a-z0-9. ]+(?<!\.)$#i',$text)

The negative lookbehind makes sure that the string doesn't end in . (which it also does if it consists of . alone, so that condition is caught, too).

EDIT:

Now that you also want to disallow "digits only", you also need a lookahead assertion:

preg_match('#^(?![0-9]*$)[a-z0-9. ]+(?<!\.)$#i',$text)
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜