-1

I need to use Python 2 with my server. But the code below I use in my computer using Python 3, and is not working in version 2. Anyone knows the conversion of the command below to Python 2? I need to return a value in parentheses.

Python 3:

    try:
        new = re.search(r'\([^)]*\)', value)
        new = str(new[0])
    except:
        return value

Python 2

????
  • You need to provide a [mre]. I don't see anything that would make this code behave differently between 2 and 3, but `value` is undefined, so I can't say for sure. However, part of the problem might be the [bare `except`, which is bad practice](https://stackoverflow.com/a/54948581/4518341). Try making it as specific as possible, e.g. `except TypeError`. – wjandrea Sep 17 '20 at 04:01

1 Answers1

-2

you can add , in the end like

import re

value = '5.2 (37.2)'
regex = '\([\d.]+\)'
ans = re.search(regex, value)
print(value[ans.start()+1:ans.end()-1] )

it will give

37.2

gulbaz khan
  • 175
  • 1
  • 9