简体   繁体   中英

Python regular expression to extract the parenthesis

I have the following unwieldy code to extract out 'ABC' and '(XYZ)' from a string 'ABC(XYZ)'

import re

test_str = 'ABC(XYZ)'
partone = re.sub(r'\([^)]*\)', '', test_str)
parttwo_temp = re.match('.*\((.+)\)', test_str)
parttwo = '(' + parttwo_temp.group(1) + ')'

I was wondering if someone can think of a better regular expression to split up the string. Thanks.

You may use re.findall

>>> import re
>>> test_str = 'ABC(XYZ)'
>>> re.findall(r'\([^()]*\)|[^()]+', test_str)
['ABC', '(XYZ)']
>>> [i for i in re.findall(r'(.*)(\([^()]*\))', test_str)[0]]
['ABC', '(XYZ)']
[i for i in re.split(r'(.*?)(\(.*?\))', test_str) if i]

For this kind of input data, we can replace the ( with space+ ( and split by space:

>>> s = 'ABC(XYZ)'
>>> s.replace("(", " (").split()
['ABC', '(XYZ)']

This way we are artificially creating a delimiter before every opening parenthesis.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM