gpt4 book ai didi

大括号中包含数字的正则表达式

转载 作者:行者123 更新时间:2023-12-05 08:57:36 27 4
gpt4 key购买 nike

我得到了这个正则表达式:

^\+[0-9]{12,12}$

谁能解释一下大括号{12,12}之间的两个值的含义?

最佳答案

{12,12} 是一个量词(参见 Limiting Repetition section ),它告诉正则表达式引擎前面的子模式应该重复最少 12 次和最多 12 次。在 ^+[0-9]{12,12}$ 中表示 0-9 范围内的数字应恰好重复 12 次。字符串应以加号开头,且仅包含加号+12位数字。

The syntax is {min,max}, where min is zero or a positive integer number indicating the minimum number of matches, and max is an integer equal to or greater than min indicating the maximum number of matches. If the comma is present but max is omitted, the maximum number of matches is infinite. So {0,1} is the same as ?, {0,} is the same as *, and {1,} is the same as +. Omitting both the comma and max tells the engine to repeat the token exactly min times.

因此,我们可以使用{12}(它等于{12,12}):

^\+[0-9]{12}$

参见 demo at regex101.com

关于大括号中包含数字的正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30246601/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com