I'm able to remove brackets entirely from a string using the following Python function:
def bracket_remove(smi):
remove_chars = ['[', ']']
return ''.join((filter(lambda i: i not in remove_chars, smi)))
However, there are some cases where I want to keep bracketed sections. Specifically, I only want to remove brackets if the brackets enclose a single character. For example, [N][C@H]([C])[C](=O)[O] would become N[C@H](C)C(=O)O. Regular expressions seem like the way to go here, but I haven't been able to find much that's relevant for this problem. The closest regular expression solution I could find was:
import re
test_str = "[N][C@H]([C])[C](=O)[O]"
m = re.search(r"\[(\w+)\]", test_str)
print(m.group(1))
This returns only the first instance of bracketed characters (N), while I had thought it would identify cases where multiple characters were present between brackets. How would I modify this expression to find only the substrings with multiple characters (alphanumeric or otherwise) between brackets?