简体   繁体   中英

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. 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. 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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