-2

I need to fetch text between parenthesis { text } in python. here is my sample string,

my_txt = "/home/admin/test_dir/SAM_8860-fg_frame_{001,002,003,004,005,007}.png"

I need numbers between {}.

I tried,

>>> re.search(r'{.*}',my_txt).group()
'{001,002,003,004,005,007}'

but it returns string along with curly braces.

expected output is, '001,002,003,004,005,007'

how to omit curly braces to fetch string in python regex?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Mohideen bin Mohammed
  • 18,813
  • 10
  • 112
  • 118

2 Answers2

0

Get only the matched group:

>>> re.search(r'{(.*?)}',my_txt).group(1)
'001,002,003,004,005,007'
  • group(0) is the entire match
  • group(1) is the first match
  • ...
Maroun
  • 94,125
  • 30
  • 188
  • 241
0

Try this. it creates group and select 1st group

re.search(r'{(.*?)}',my_txt).group(1)
Binit Amin
  • 481
  • 2
  • 18