在正则表达式中指定 glob 大小范围
如何定义某个模式应重复自身的最小和最大(可能无界)次数?我知道有 ?
和 *
,我可以通过重复一定次数来构建模式,但我知道使用 有一个特殊的表示法{}
,我只是不记得它是怎么回事。
How do I define a minimum and maximum (possibly unbounded) number of times a certain pattern should repeat itself? I know there's ?
and *
, with which I could build the pattern by repeating it a certain amount of times, but I know there's a special notation for it using {}
, I just can't remember how it is.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
对于最小值
m
和最大值n
,您可以使用{m,n}
。如果m
和n
相同,则只需使用{m}
。例如,仅包含三到四个字母,后跟两个数字,后跟六到十二个字母数字的行将是:
如果您希望在高端无限重复(没有最大数量),只需省略
n.对于低端的无限重复,有些实现不支持省略
m
因此您可能只想指定 0 以确保安全)。换句话说,意味着六个或更多小写字母后跟零到四位数字。
您的特殊情况只是其中的版本,如下所示:
For a minimum of
m
and maximum ofn
, you use{m,n}
. Ifm
andn
are the same, just use{m}
.For example, a line consisting only of three to four alphas followed by two numerics followed by six to twelve alphanumerics would be:
Where you want unbounded repetitions on the high side (no maximum number), just leave out the
n
. For unbounded repetitions on the low side, there are some implementations that don't support leaving out them
so you may want to just specify 0 for that to be safe). In other words,means six or more lowercase letters followed by zero to four digits.
Your special cases are just versions of that, as in:
模式后包括 {min,max}
After the pattern include {min,max}
您可以这里找到有关正则表达式重复的教程(以及一堆其他内容)
You can find a tutorial about repetition in Regex (as well as a bunch of other stuff) here