[英]Repeat characters X times but do not count Y characters that are in between (Regex)
I need a regex that does the following:我需要一个执行以下操作的正则表达式:
This is the regex I have right now: [0-9]{0,5}
这是我现在拥有的正则表达式:
[0-9]{0,5}
However, it does not cover the following legit strings:但是,它不包括以下合法字符串:
1 2 3 4 5
123/45
1234 5
1 234 4
1/234/5
1/234 5
1 2/3 4/5
1 2345
1 ////23/////45
I tried:我试过了:
[\s*\/*0-9]{0,5} //counts unwanted characters
[0-9\s\/]{0,5} //counts unwanted characters
[0-9-\s+\/]{0,5} //counts unwanted characters
[\s+\/-]{0,}[0-9]{0,5} // does not mix
Is this even possible in regex?这在正则表达式中甚至可能吗?
The other solution, I can do is to remove these characters and then comparing to the pattern.我可以做的另一个解决方案是删除这些字符,然后与模式进行比较。
The regex you can use is您可以使用的正则表达式是
(?:[ \/]*\d){0,5}
See the regex demo .请参阅正则表达式演示。 Details :
详情:
(?:
- start of a non-capturing group: (?:
- 非捕获组的开始:
[ \/]*
- zero or more spaces or slashes [ \/]*
- 零个或多个空格或斜杠\d
- a digit \d
- 一个数字){0,5}
- end of the group, match zero to five times. ){0,5}
- 组的结尾,匹配 0 到 5 次。 If you need to match any spaces or slashes after the last digit, add [ \/]*
at the end:如果您需要匹配最后一位数字后的任何空格或斜杠,请在末尾添加
[ \/]*
:
(?:[ \/]*\d){0,5}[ \/]*
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.