You can use:
regex = re.compile( r '\( (\d+) \)')
The prefix r means that we are working with a raw string. A raw string means that if you write, for example, r'\n' , Python will not interpret this as a string with a new string character. But like a string with two characters: backslash ( '\\' ) and 'n' .
Additional brackets are in the definition of the capture group. In addition, a number is a sequence (one or more) digits. Thus, + guarantees that we will also write (1425) .
Then we can execute a .search() and get a match. Then you can use .start(1) to get the start of the first capture group:
>>> regex.search(data) <_sre.SRE_Match object; span=(4, 7), match='(8)'> >>> regex.search(data).start(1) 5
If you are interested in the contents of the first capture group, you can call .group(1) :
>>> regex.search(data).group(1) '8'
source share