简体   繁体   中英

Regular Expression to match numbers in square brackets in python

I need to match and return the string index of numbers which are contained in square brackets. Example string:

Gabrilovich and Markovitch [11, 12] propose a method to use conditional random fields [6] as a training process.....

Here I would like to extract the index of any given number like 11, 12 or 6 in the above case using regular expressions. I am trying

pattern = re.compile(r'[/11/]') # for 11
result =  re.search(pattern, text, flags=0)
print result.start()

However with this I am not getting desired results. Note: I would need a solution for matching the exact number I want, not any given number within brackets.

use this regex expression (\\[[,\\d\\s ]*)11([,\\d\\s ]*\\]) for retrieving all the 11's in the text

have a look at the example I uploaded https://regex101.com/r/lN8mA6/1

Since in Python we cannot use variable-width lookbehinds with standard re module, you can use capturing groups, and then check the index of the group.

Sample code for capturing 11 :

pattern = re.compile(r'(\[[^\]]*)\b(11)\b(?=[^\]]*\])') # for 11
text = 'Gabrilovich and Markovitch [11, 12] propose a method to use conditional random fields [6] as a training process.....'
result =  re.search(pattern, text)
if result:
    print result.start(2)

Result: 28 .

Note that I am using word boundaries around 11 to only match 11 , and not 111 or 112 .

Try this regexp: \\[\\s*(\\d*)(\\s*,\\s*(\\d*)\\s*)?(\\s*,\\s*(\\d*)\\s*)?(\\s*,\\s*(\\d*)\\s*)?(\\s*,\\s*(\\d*)\\s*)?(\\s*,\\s*(\\d*)\\s*)?(\\s*,\\s*(\\d*)\\s*)?\\] (the groups (\\s*,\\s*(\\d)\\s*)? are repeated to allow to get up to 7 numbers in between the square brackets) as this demo shows. If you want to be more general, you can substitute this regexp with \\[\\s*(\\d*)(\\s*,\\s*(\\d*)\\s*)*\\] that allows an indefinite number of numbers in the list (but then you will get the first and the last only in groups \\1 and \\3 )

If you use the first, you will allow spaces around the commas and you'll get the numbers in groups 1, 3, 5, 7, 11, 13 and 15.

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