如何丢弃正则表达式组后的所有字符

发布于 2025-01-10 12:41:25 字数 334 浏览 4 评论 0原文

我只想允许 2 位正双精度数。

我得到了这个,它正在工作,但是在我成功匹配模式后,它会重新开始,而不是丢弃传入的字符。

([1-9]+[\d]*(\.|,)?[\d]{0,2})
Input

12.34  //Pass Filter  
12.340 //Rejected - Zero cannot be the first character
12.345 //Pass Filter - It pass, but it is supposed to be rejected since it has 3 decimal digits

I want to only allow a positive double with 2 digits.

I got this, which is working, but after I successfully match the pattern it starts over again instead of discarding incoming characters.

([1-9]+[\d]*(\.|,)?[\d]{0,2})
Input

12.34  //Pass Filter  
12.340 //Rejected - Zero cannot be the first character
12.345 //Pass Filter - It pass, but it is supposed to be rejected since it has 3 decimal digits

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

怎樣才叫好 2025-01-17 12:41:25

如果您不想部分匹配,可以锚定字符串,并将零和逗号放入字符类中。

如果您想匹配 ., 零次或 1 次,您可以使用 ? 而不是 *

^[1-9]+\d*[.,]*\d{0,2}$

请参阅正则表达式演示

If you don't want partial matches, you can anchor the string, and put the zero and comma in a character class.

If you want to match the . or , zero or 1 times you can use ? instead of *

^[1-9]+\d*[.,]*\d{0,2}$

See a regex demo.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文