splitByKeyList static method
Implementation
static List<String> splitByKeyList(List<String> keyList, String text) {
List<String> splitResultByKeyList = [];
if (text == null || text.isEmpty) {
return splitResultByKeyList;
}
if (keyList == null || keyList.isEmpty) {
splitResultByKeyList.add(text);
return splitResultByKeyList;
}
// 使用正则表达式创建模式
String pattern = keyList.map((key) => RegExp.escape(key)).join('|');
RegExp regExp = RegExp('($pattern)');
// 使用正则表达式分割字符串
List<String> result = text.split(regExp);
var matches = regExp.allMatches(text);
var matchList = matches.toList();
// 重新插入关键字
for (int i = 0; i < result.length; i++) {
splitResultByKeyList.add(result[i]);
if (i < matchList.length) {
splitResultByKeyList.add(matchList[i].group(0)!);
}
}
return splitResultByKeyList;
}