本文已收录于PHP全栈系列专栏:PHP快速入门与实战
正则表达式(Regular Expression)是一种字符序列,用于描述一组字符串匹配某个模式或格式。
它由一些特殊的字符和普通字符组成,可以表示一些特定的文本模式,如手机号码、邮箱、网址等等。正则表达式在很多程序设计语言中都得到了广泛应用,如Python、Java、C++等等。合理的使用正则表达式往往会使我们的效率事半功倍。
常用的正则表达式元字符包括:
例如,使用正则表达式“\d{11}”可以匹配11位的数字串,即手机号码。
正则表达式虽然非常强大,但也有其缺点,即复杂度较高,难以阅读和维护,因此需要慎重使用。
在编程中,正则表达式经常被用来处理文本数据,如验证用户输入、搜索字符串、替换文本等。
以下是使用 PHP 编写的 20 个正则表达式示例:
if (preg_match('/^[a-zA-Z]/', $string)) {
echo "First character is a letter";
} else {
echo "First character is not a letter";
}
if (preg_match('/^[0-9]/', $string)) {
echo "First character is a number";
} else {
echo "First character is not a number";
}
if (preg_match('/[aeiou]/', $string)) {
echo "String contains a vowel";
} else {
echo "String does not contain a vowel";
}
if (preg_match('/[.]$/', $string)) {
echo "String ends with a period";
} else {
echo "String does not end with a period";
}
if (preg_match('/^[0-9]+$/', $string)) {
echo "String is all digits";
} else {
echo "String is not all digits";
}
if (preg_match('/^[a-zA-Z]+$/', $string)) {
echo "String is all letters";
} else {
echo "String is not all letters";
}
if (preg_match('/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/', $string)) {
echo "String is a valid email";
} else {
echo "String is not a valid email";
}
if (preg_match('/^(http|https):\/\/[a-z0-9\-]+\.[a-z0-9\-]+(\/[a-z0-9\-._~:?#\[\]@!$&\'()*+,;=%]*)?$/i', $string)) {
echo "String is a valid URL";
} else {
echo "String is not a valid URL";
}
if (preg_match('/^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/', $string)) {
echo "String is a valid IP address";
} else {
echo "String is not a valid IP address";
}
if (preg_match('/^\d{4}-\d{2}-\d{2}$/', $string)) {
echo "String is a valid date";
} else {
echo "String is not a valid date";
}
if (preg_match('/^(0?[1-9]|1[0-2]):[0-5][0-9] (am|pm)$/i', $string)) {
echo "String is a valid time";
} else {
echo "String is not a valid time";
}
if (preg_match('/^\+?\d{1,3}?[- .]?\(?\d{3}\)?[- .]?\d{3}[- .]?\d{4}$/', $string)) {
echo "String is a valid phone number";
} else {
echo "String is not a valid phone number";
}
if (preg_match('/(?=.*[a-zA-Z])(?=.*[0-9])/', $string)) {
echo "String contains at least one letter and one number";
} else {
echo "String does not contain at least one letter and one number";
}
if (preg_match('/^\d{5}-\d{4}|\d{5}|[A-Z]\d[A-Z] \d[A-Z]\d$/', $string)) {
echo "String is a valid postal code";
} else {
echo "String is not a valid postal code";
}
if (preg_match('/^\$?\d+(\.\d{1,2})?$/', $string)) {
echo "String is a valid currency";
} else {
echo "String is not a valid currency";
}
if (preg_match('/(?=.*[a-z])(?=.*[A-Z])/', $string)) {
echo "String contains at least one lower case letter and one upper case letter";
} else {
echo "String does not contain at least one lower case letter and one upper case letter";
}
if (preg_match('/^(京|津|沪|渝|冀|豫|云|辽|黑|湘|皖|鲁|新|苏|浙|赣|鄂|桂|甘|晋|蒙|陕|吉|闽|贵|粤|青|藏|川|宁|琼)([A-HJ-NP-Z])([A-HJ-NP-Z0-9]{4}[A-HJ-NP-Z0-9挂学警港澳]{1})$/', $string)) {
echo "String is a valid license plate number";
} else {
echo "String is not a valid license plate number";
}
if (preg_match('/^[1-9]\d{5}(18|19|20)\d{2}(0[1-9]|1[012])(0[1-9]|[12][0-9]|3[01])\d{3}[0-9xX]$/', $string)) {
echo "String is a valid ID card number";
} else {
echo "String is not a valid ID card number";
}
if (preg_match('/\W/', $string)) {
echo "String contains at least one non-letter and non-number character";
} else {
echo "String does not contain at least one non-letter and non-number character";
}
if (preg_match('/^<([a-z]+)([^<]+)*(?:>(.*)<\/\1>|\s+\/>)$/', $string)) {
echo "String is a valid HTML tag";
} else {
echo "String is not a valid HTML tag";
}
以上就是关于本篇文章介绍的内容,正则相关,附一定要学会的20个高频使用案例,后续更多内容将收录在专栏PHP快速入门与实战中,感谢大家支持。