简体   繁体   English

在 PCRE(正则表达式)中 $ 和 * 是什么意思?

[英]what does $ and * means in PCRE (Regular Expression)?

i am using a Regular Expression that validates an email address here is the regular expression i am using.我正在使用验证 email 地址的正则表达式,这是我正在使用的正则表达式。

preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/", $email)

most of the above code are self explanatory like上面的大部分代码都是不言自明的

a) ^ represents NOT. a) ^ 表示非。

b) the start of the string should be either _ az 0-9 b) 字符串的开头应该是_ az 0-9

c) match the next character which starts with dot c) 匹配下一个以点开头的字符

d) now what does *@ means here, couldn't it be just @ which means the next character should be @ d) 现在 *@ 在这里是什么意思,难道只是 @ 意味着下一个字符应该是 @

e) next again it will try and find dot, the first dot is optional and the second is compulsory. e) 接下来它会再次尝试查找点,第一个点是可选的,第二个是强制性的。

f) in the end what does $ means? f) 到底 $ 是什么意思?

Your assumption a) is not true您的假设 a) 不正确

^ is the start of the string in this case.在这种情况下, ^是字符串的开头。 At the beginning of a character class its a NOT.在字符 class 的开头是一个 NOT。

[_a-z0-9-]+ will match any of the chars in [] one or more times (because of the + ) [_a-z0-9-]+将匹配[]中的任何字符一次或多次(因为+

(\.[_a-z0-9-]+)* then there is a dot the same pattern than before and the * means this complete part can be repeated 0 or more times (\.[_a-z0-9-]+)*则有一个点与以前的模式相同, *表示这个完整部分可以重复 0 次或更多次

Then there has to be the character @然后必须有字符@

Then the part from before the @ repeats然后@之前的部分重复

(\.[az]{2,3})$ the string has to end (defined by the $ ) with a . (\.[az]{2,3})$字符串必须以 . 结尾(由$定义) . and 2 or 3 lowercase letters和 2 或 3 个小写字母

* means the preceding rule 0 or multiple times * 表示前面的规则 0 次或多次

while $ in this case means the end of the string而在这种情况下 $ 表示字符串的结尾

(\.[_a-z0-9-]+)* // these characters can appear 0 or multiple times

(\.[a-z]{2,3})$ // the string ends with 2 letters in lowercase alphabet

lots of information about regexp can be found at http://www.regular-expression.info很多关于正则表达式的信息可以在http://www.regular-expression.info找到

For example: f) see § Anchor at http://www.regular-expressions.info/quickstart.html例如:f) 参见http://www.regular-expressions.info/quickstart.html处的§锚

Since the existing answers didn't cover this...由于现有的答案没有涵盖这个......

Yes, * means "zero or more times", but it is also "greedy" by default, so it will match as many times as possible, even if part of the matched string would have caused the next part of the pattern to match.是的, *表示“零次或多次”,但它默认也是“贪婪”的,所以它会尽可能多地匹配,即使部分匹配的字符串会导致模式的下一部分匹配。 * can be made "lazy" (allowing the pattern to "backtrack" to allow further matches in the pattern) by appending a ? *可以通过附加一个?懒惰”(允许模式“回溯”以允许模式中的进一步匹配) : *? : *? . .

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM