Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular Expression to match numbers in square brackets in python

Tags:

python

regex

text

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.

like image 890
Soumyajit Avatar asked Apr 28 '26 08:04

Soumyajit


1 Answers

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

like image 71
Yehia Awad Avatar answered Apr 30 '26 20:04

Yehia Awad



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!