a:3使用正则表达式
Regular expressions provides an easy and precise way of matching certain patterns within a string. There can indeed be very complex regular expressions that can be written, or very simple ones. For example, a very simple regular expression could be (for PHP, specifically):
正则表达式提供了一种简单而精确的方式来匹配字符串中的某些模式。 确实可以存在非常复杂的正则表达式,也可以非常简单。 例如,一个非常简单的正则表达式可以是(特别是对于PHP):
$regex = preg_match(“/^hello$/”,$string);
$ regex = preg_match(“ / ^ hello $ /”,$ string);
In this example, the word hello is required completely because of the start ^ character and the end $ character. The forward slashes at the start and the end are required with the preg_match function and within that, regular expression syntax is added.
在此示例中,由于开始^字符和结束$字符,因此完全需要单词hello 。 preg_match函数在开始和结尾处使用正斜杠,并在其中添加了正则表达式语法。
^ means start of string $ means end of string
^表示字符串的开头 $表示字符串的结尾
So hello needs to be at the start and end of the string exactly. Hence, the word hello is only accepted.
因此, 您好需要准确地位于字符串的开头和结尾 。 因此,“ hello”一词仅被接受。
Here is a more complex regular expression:
这是一个更复杂的正则表达式:
$regex = preg_match(“/^[a-zA-Z]$/”,$string);
$ regex = preg_match(“ / ^ [a-zA-Z] $ /”,$ string);
In this case, any characters between a and z or A and Z (uppercase). No numerical numbers are allowed, because of the end $ symbol. If you had:
在这种情况下,a和z或A和Z之间的任何字符(大写)。 由于末尾的$符号,因此不允许使用数字。 如果你有:
$regex = preg_match(“/^[a-zA-Z]/”,$string);
$ regex = preg_match(“ / ^ [a-zA-Z] /”,$ string);
This would be accepted: eUKhost10
这将被接受:eUKhost10
As it starts with a to z or A to Z but ends in numericals. There are no constraints restricting the end characters within the example above.
从a到z或A到Z开始,但以数字结尾。 在上面的示例中,没有任何限制结束字符的约束。
Another example:
另一个例子:
$regex = preg_match(“/[0-9]$/”,$string);
$ regex = preg_match(“ / [0-9] $ /”,$ string);
This requires the end of the string to be numerical. So: eUKhost10 would be accepted, eUKhost would not.
这要求字符串的结尾是数字。 因此: eUKhost10将被接受, eUKhost将不被接受。
Another example:
另一个例子:
$regex = preg_match(“/[a-zA-Z]{3,}$/”,$string);
$ regex = preg_match(“ / [a-zA-Z] {3,} $ /”,$ string);
This requires at least 3 alphabetic characters at the end. If it was {3,4} it would require precisely between 3 and 4 – and no more than 4.
最后至少需要3个字母字符。 如果它是{3,4 },则需要3到4之间- 不超过4。
这些特殊字符是什么? (What are these special characters?)
Things such as {3,4} are metacharacters and have a specialised meaning in regular expressions. For example:
诸如{3,4 }之类的元素是元字符,并且在正则表达式中具有特殊含义。 例如:
- {3,4} means between 3 and 4 characters of the preceding element {3,4}表示前一个元素的3至4个字符
- [a-z] matches a specific range, also: [0-9]. [az]匹配特定范围,也:[0-9]。
- ^ start of string ^字符串的开头
- $ end of string $字符串结尾
* matches the element zero or more times: hello* – so for example, hell and hello would be accepted.
*匹配元素零次或多次: hello * –例如,将接受hell和hell o 。
? matches the preceding element zero or one time – so hello? – hell and hello are accepted, not hello.
? 匹配前面的元素零个或一个时间-所以喂? –接受hell和hello ,而不是hello 。
+ matches the preceding element one or more times, so: [a-z]+ means there needs to be at least one a-z character.
+一次或多次匹配前一个元素,因此:[az] +表示至少需要一个z字符。
| is the choice metacharacter. For example: hello|goodbye would match either hello or goodbye.
| 是选择元字符。 例如: hello | goodbye将匹配hello或goodbye 。
如果一个字符被当作元字符怎么办? (What if a character is treated as a metacharacter?)
Use a backslash to make it not be treated as a metacharacter. For example: \. for the period character, as the period character is a metacharacter for matching any single character.
使用反斜杠使其不被视为元字符。 例如: \。 对于句点字符,因为句点字符是用于匹配任何单个字符的元字符。
翻译自: https://www.eukhost.com/blog/webhosting/regular-expressions-what-are-they-how-do-i-use-them/
a:3使用正则表达式