简体   繁体   English

Python正则表达式重复模式匹配

[英]python regular expression repeating pattern match

I would like some thoughts on how to write a regular expression which validates a pattern 我想对如何编写可验证模式的正则表达式进行一些思考

ex. 例如 .??2

one of more characters followed by two question marks followed by one or more numbers and if only if there is another repeating pattern then the seperator will be a semi colon. 一个或多个字符后跟两个问号,后跟一个或多个数字,并且如果仅存在另一种重复模式,则分隔符将为半冒号。

more examples 更多例子

--??9;.??50;,??3 - in this example I have the pattern repeating and that is why the semi colon --??9;.??50;,??3在此示例中,我重复了模式,这就是为什么半冒号

or 要么

*??5 - a * followed by two qnestions marks followed by a number and no semi colon as there are no repeating groups *??5一个*后跟两个qnestions标记,后跟一个数字,没有分号,因为没有重复的组

This is what i currently have 这是我目前拥有的

.+\\?\\?\\d+(;|)+

The basic pattern is .+?\\?\\?\\d+ . 基本模式是.+?\\?\\?\\d+ We have made the first .+ non-greedy so it won't try to match the whole string right away. 我们已经制作了第一个.+非贪婪类,因此它不会尝试立即匹配整个字符串。 Use a repeated group to capture the subsequent patterns: r'(.+?\\?\\?\\d+)(;.+?\\?\\?\\d+)*' 使用重复的组来捕获后续模式: r'(.+?\\?\\?\\d+)(;.+?\\?\\?\\d+)*'

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

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