.*(点星)如何工作? [英] How .* (dot star) works?
问题描述
我已经明白 .*
意味着零个或多个任何字符,但是有人能解释一下 .*
在下面的工作中是如何工作的以及它会匹配什么吗?
I already understand that .*
means zero or more of any character, but
Could someone explain how .*
in the following work and what it would match?
.*([a-m/]*).*
.*([a-m/]+).*
.*?([a-m/]*).*
推荐答案
每个案例都不同:
.*([a-m\/]*).*
第一个 .*
可能会匹配整个字符串,因为 [am/]
不需要出现,第一个 *
> 贪婪且优先.
The first .*
will probably match the whole string, because [a-m/]
is not required to be present, and the first *
is greedy and comes first.
.*([a-m\/]+).*
第一个 .*
将匹配整个字符串直到匹配 [am/]
的最后一个字符,因为只需要一个,第一个 *
贪婪且先到.
The first .*
will match the whole string up to the last character that matches [a-m/]
since only one is required, and the first *
is greedy and comes first.
.*?([a-m\/]*).*
第一个 .*?
将匹配字符串直到匹配 [am/]
的第一个字符,因为 *?
不是贪婪,那么 [am/]*
将匹配所有它可以匹配的,因为 *
是贪婪的,然后最后一个 .*
将匹配其余的的字符串.
The first .*?
will match the string up to the FIRST character that matches [a-m/]
, because *?
is not greedy, then [a-m/]*
will match all it can, because *
is greedy, and then the last .*
will match the rest of the string.
这篇关于.*(点星)如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!