diff --git a/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeByRegex.kt b/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeByRegex.kt index 923afe143..2d7b6c306 100644 --- a/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeByRegex.kt +++ b/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeByRegex.kt @@ -1,12 +1,64 @@ package io.legado.app.model.analyzeRule -object AnalyzeByRegex { +import java.util.* +import java.util.regex.Pattern +import kotlin.collections.HashMap +import kotlin.collections.List +import kotlin.collections.Map +import kotlin.collections.arrayListOf +import kotlin.collections.set - fun getElement() { +object AnalyzeByRegex { + fun getElement(res: String, regs: Array, index: Int = 0): Map? { + var vIndex = index + val resM = Pattern.compile(regs[vIndex]).matcher(res) + if (!resM.find()) { + return null + } + // 判断索引的规则是最后一个规则 + return if (vIndex + 1 == regs.size) { + // 新建容器 + val info = HashMap() + for (groupIndex in 1 until resM.groupCount()) { + info["$$groupIndex"] = resM.group(groupIndex) + } + info + } else { + val result = StringBuilder() + do { + result.append(resM.group()) + } while (resM.find()) + getElement(result.toString(), regs, ++vIndex) + } } - fun getElements() { - + fun getElements(res: String, regs: Array, index: Int = 0): List> { + var vIndex = index + val resM = Pattern.compile(regs[vIndex]).matcher(res) + if (!resM.find()) { + return arrayListOf() + } + // 判断索引的规则是最后一个规则 + if (vIndex + 1 == regs.size) { + // 创建书息缓存数组 + val books = ArrayList>() + // 提取书籍列表 + do { + // 新建容器 + val info = HashMap() + for (groupIndex in 1 until resM.groupCount()) { + info["$$groupIndex"] = resM.group(groupIndex) + } + books.add(info) + } while (resM.find()) + return books + } else { + val result = StringBuilder() + do { + result.append(resM.group()) + } while (resM.find()) + return getElements(result.toString(), regs, ++vIndex) + } } } \ No newline at end of file