0

I have a dataframe which looks like this:

>>>df
    Str
0   .....(((((................((((.(...).))))(((((....))))).(((((((((...))))))).))))))).................
1   .(((((..(((....(((((((........)))))))..)))..))).)).((((((((((.((..(((....)))....)).)))))))))).......
2   ((((((.(((((.(((...))))))))))....(((.((((.((.(((....))).)).))))..)))))))(..((((...))))..)...........
3   (((((((.((....((.((.((((..((.......(((...))).((((((((...))))))))....))..)))).)).))....))..)))))))...

I want to capture the portion starting from first opening bracket to the last opening bracket. I tried the following code for that:

df["stem"] = df["Str"].str.findall('[(][(.)]+[)]')
df["stem"] = df["stem"].astype("str")

The code does capture the blocks but prints it inside an array starting and ending with square brackets:

['regexblock']

>>>df
        Str stem
0   .....(((((................((((.(...).))))(((((....))))).(((((((((...))))))).))))))).................    ['(((((................((((.(...).))))(((((....))))).(((((((((...))))))).)))))))']
1   .(((((..(((....(((((((........)))))))..)))..))).)).((((((((((.((..(((....)))....)).)))))))))).......    ['(((((..(((....(((((((........)))))))..)))..))).)).((((((((((.((..(((....)))....)).))))))))))']
2   ((((((.(((((.(((...))))))))))....(((.((((.((.(((....))).)).))))..)))))))(..((((...))))..)...........    ['((((((.(((((.(((...))))))))))....(((.((((.((.(((....))).)).))))..)))))))(..((((...))))..)']
3   (((((((.((....((.((.((((..((.......(((...))).((((((((...))))))))....))..)))).)).))....))..)))))))...    ['(((((((.((....((.((.((((..((.......(((...))).((((((((...))))))))....))..)))).)).))....))..)))))))']

I need to find the length of each block, but due to this addition of special characters I get 4 extra counts. Is there anyway to get rid of these characters while handling regex?
Thanks in advance.

sloth14
  • 3
  • 1

1 Answers1

0

This one should do it:

df['Str'].str.extract('(\(.*\))')
zipa
  • 27,316
  • 6
  • 40
  • 58