看起来我找到了适用于所有情况的解决方案:
Enter your regex: \(.*?\)[0-9]{0,2}|[\w]+
Enter input string to search: Fe2(CH3)2O4(CH2O)(CH3)
I found the text "Fe2" starting at index 0 and ending at index 3.
I found the text "(CH3)2" starting at index 3 and ending at index 9.
I found the text "O4" starting at index 9 and ending at index 11.
I found the text "(CH2O)" starting at index 11 and ending at index 17.
I found the text "(CH3)" starting at index 17 and ending at index 22.
这是代码中要使用的正则表达式:
String regEx = "\\(.*?\\)[0-9]{0,2}|[\\w]+";
简而言之,它会查找以 '(' 开头并以 ')' 和数字结尾的字符串。如果那不存在,那么它会查找 'Fe2' 、 'O4' 等字符串。