gpt4 book ai didi

: 6 digits or 0-6 signs (digits or stars) with at least one star 的正则表达式

转载 作者:行者123 更新时间:2023-12-03 18:23:39 26 4
gpt4 key购买 nike

如何编写正则表达式来验证此模式?

123456 - correct
*1 - correct
1* - correct
124** - correct
*1*2 - correct
* - correct
123456* - incorrect (size 7)
12345 - incorrect (size 5 without stars)

试过:
^[0-9]{6}$|^(([0-9]){1,6}([*]){1,5}){1,6}+$

但它允许有超过 6 个数字,并且不允许在数字之前加星号。
“*”符号没有最小/最大计数(但所有符号的最大计数为 6)。

最佳答案

干得好:

^(?:\d{6}|(?=.*\*)[\d*]{1,6}|)$

这是它的作用:
^            <-- Start of the string (we don't want to capture more than that)
(?: <-- Start a non captured group (it will be used to do the "or" part)
\d{6} <-- 6 digits, nothing more
| <-- OR
(?=.*\*) <-- Look ahead for a '*' (you could replace the first * with {0,5})
[\d*] <-- digits or '*'
{1,6} <-- repeated one to six times (we know from the look ahead that there will be at least one '*'
| <-- OR (nothing)
) <-- End the non capturing group
$ <-- End of the string

我不太确定你是否想要空 shell (但你说的是 0 到 6),如果你真的想要 1 到 6,只需删除最后一个 |

关于: 6 digits or 0-6 signs (digits or stars) with at least one star 的正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10107425/

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