我不确定单个正则表达式能够满足您的需求:这些嵌套子字符串总是会让它混乱不清。
一个解决方案可能是以下算法(用Java编写,但我想将其翻译成C#应该不难):
public static List<String> findAllMatches(String input, String regex, String format) {
if (format == null) {
format = "ééé%dèèè";
}
int counter = 0;
Map<String, String> matches = new LinkedHashMap<String, String>();
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(input);
while (matcher.find()) {
String replace = String.format(format, counter++);
matches.put(replace, matcher.group());
String end = input.substring(matcher.end(), input.length());
String start = input.substring(0, matcher.start());
input = start + replace + end;
matcher = pattern.matcher(input);
}
List<Entry<String, String>> entries = new LinkedList<Entry<String, String>>(matches.entrySet());
for (int i = 0; i < entries.size(); i++) {
Entry<String, String> current = entries.get(i);
for (int j = 0; j < i; j++) {
Entry<String, String> previous = entries.get(j);
if (current.getValue().contains(previous.getKey())) {
current.setValue(current.getValue().replace(previous.getKey(), previous.getValue()));
}
}
}
return new LinkedList<String>(matches.values());
}
因此,在您的情况下:
String input = "tes{}tR{R{abc}aD{mnoR{xyz}}}";
String regex = "[A-Z][{][^{}]+[}]";
findAllMatches(input, regex, null);
返回:
R{abc}
R{xyz}
D{mnoR{xyz}}
R{R{abc}aD{mnoR{xyz}}}