简体   繁体   English

正则表达式 python 单步,匹配包含两个以上分隔数字的纯文本括号

[英]regex python single step, match in a text only brackets that contains more than two separated numbers

This is to get combo references (eg [23,24,28-30], this bracket contain four numbers) in sci papers with vancouver notation format.这是为了在 vancouver 符号格式的 sci 论文中获得组合引用(例如 [23,24,28-30],这个括号包含四个数字)。 I need to match brackets with more than two numbers (don't confuse with digits).我需要用两个以上的数字匹配括号(不要与数字混淆)。 Min.repr.example:最小代表示例:

Input raw text输入原始文本

blabla [23,24] bleble [23,24,28-30] blibli [40,45-48] bloblo [113]

The regex I look for yields only,我只寻找产量的正则表达式,

>>> ['[23,24,28-30]', '[40,45-48]']

My regex try: r"\[[,\-(?:\d+)]{3,}\]" but I failed.我的正则表达式尝试: r"\[[,\-(?:\d+)]{3,}\]"但我失败了。 I look for a single step regex expression.我寻找单步正则表达式。

Many thanks for your experience.非常感谢您的体验。

You can't put a group inside a character class. The character set should be inside the group that you're quantifying.您不能将组放在字符 class 内。字符集应该在您要量化的组内。

r"\[(?:\d+[-,]){2,}\d+\]"

This matches at least 2 repetitions of a number followed by a separator (either comma or hyphen) followed by another number.这匹配至少 2 次重复的数字后跟分隔符(逗号或连字符)后跟另一个数字。

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

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