简体   繁体   English

如何修改正则表达式以限制字符数最少 8 到最多 20

[英]How to modify a regular expression to restrict characters count minimum 8 to maximum 20

I have the following regular expression.我有以下正则表达式。 Now I want to modify it to restrict characters count from 8 to 20.现在我想修改它以限制字符数从 8 到 20。

Minimum characters count should be 8 and maximum 20最小字符数应为 8,最大为 20

^(\+\d{1,2}\s?)?1?\-?\.?\s?\(?\d{2,6}\)?[\s.-]?\d{3,6}[\s.-]?\d{1,4}$

The current expression accepting the following strings but it shouldn't in my case.当前表达式接受以下字符串,但在我的情况下不应该。 The string count should be always greater than 8字符串计数应始终大于 8

123456 123456
1234567 1234567

Here are more test cases:以下是更多测试用例:

https://regex101.com/r/hDNCkT/1 https://regex101.com/r/hDNCkT/1

You can use a positive lookahead to limit the overall length of your match.您可以使用正向前瞻来限制匹配的总长度。 After the carat, insert (?=.{8,20}$) .在克拉之后,插入(?=.{8,20}$) However, if you need to ensure there are 8-20 digits , you could use (?=(\D*\d){8,20}$) instead.但是,如果您需要确保有 8-20 个数字,您可以使用(?=(\D*\d){8,20}$)代替。

Example例子

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM