如何验证字符串仅包含小写字母? [英] How to validate that a string only contain lowercase letters?

查看:187
本文介绍了如何验证字符串仅包含小写字母?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试验证我的字符串是否与模式匹配.也就是说,完整的字符串可以写为该模式. 但是,如果任何子字符串与该模式匹配,则preg_match返回true. (例如preg_match("#[a-z]*#, "333k")返回1,我不想这样做. 在这个示例中,我宁愿验证一下,整个字符串仅包含小的拉丁字母.)

I'm trying to verify that my string matches a pattern. That is, the full string can be written as that pattern. However preg_match returns true, if any substring matches that pattern. (E.g. preg_match("#[a-z]*#, "333k") returns 1, which I don't want to. In this example I'd rather verify, the whole string contains only small Latin letters.)

推荐答案

您分别使用 start end 标记(^$)来表示正则表达式模式中字符串的开头和结尾.这样,您可以使表达式仅匹配整个字符串,而不匹配任何子字符串.在您的情况下,它将如下所示:

You use the start and end markers, ^ and $ respectively, to indicate beginning and end of the string in your regular expression pattern. That way you can make the expression match only the whole string, not any kind of substring. In your case it would then look like this:

preg_match("#^[a-z]*$#", "333k");

您还可以使用这些标记之一来指定模式必须仅与字符串的开头或结尾匹配.

You can also, with one these markers, specify that the pattern must only match the beginning or the end of the string.

这篇关于如何验证字符串仅包含小写字母?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆