diff --git a/app/src/main/java/io/legado/app/help/BookHelp.kt b/app/src/main/java/io/legado/app/help/BookHelp.kt index dba76f97f..0f62fb235 100644 --- a/app/src/main/java/io/legado/app/help/BookHelp.kt +++ b/app/src/main/java/io/legado/app/help/BookHelp.kt @@ -327,18 +327,28 @@ object BookHelp { } } - private val chapterNamePattern by lazy { - Pattern.compile("^(.*?第([\\d零〇一二两三四五六七八九十百千万壹贰叁肆伍陆柒捌玖拾佰仟0-9\\s]+)[章节篇回集])[、,。 ::.\\s]*") + private val chapterNamePattern1 by lazy { + Pattern.compile(".*?第([\\d零〇一二两三四五六七八九十百千万壹贰叁肆伍陆柒捌玖拾佰仟]+)[章节篇回集话]") } - + + private val chapterNamePattern2 by lazy { + Pattern.compile("^(?:[\\d零〇一二两三四五六七八九十百千万壹贰叁肆伍陆柒捌玖拾佰仟]+[,:、])*([\\d零〇一二两三四五六七八九十百千万壹贰叁肆伍陆柒捌玖拾佰仟]+)(?:[,:、]|\\.[^\\d])") + } + + private val regexA by lazy { + return@lazy "\\s".toRegex() + } + private fun getChapterNum(chapterName: String?): Int { - if (chapterName != null) { - val matcher: Matcher = chapterNamePattern.matcher(chapterName) - if (matcher.find()) { - return StringUtils.stringToInt(matcher.group(2)) - } - } - return -1 + chapterName ?: return -1 + chapterName = StringUtils.fullToHalf(chapterName).replace(regexA, "") + return StringUtils.stringToInt( + ( + chapterNamePattern1.matcher(chapterName).takeIf{it.find()} + ?:chapterNamePattern2.matcher(chapterName).takeIf{it.find()} + )?.group(1) + ?:"-1" + ) } @Suppress("SpellCheckingInspection") @@ -347,19 +357,22 @@ object BookHelp { return@lazy "[^\\w\\u4E00-\\u9FEF〇\\u3400-\\u4DBF\\u20000-\\u2A6DF\\u2A700-\\u2EBEF]".toRegex() } - private val regexA by lazy { - return@lazy "\\s".toRegex() - } - private val regexB by lazy { - return@lazy "^第.*?章|[(\\[][^()\\[\\]]{2,}[)\\]]$".toRegex() + //章节序号,排除处于结尾的状况,避免将章节名替换为空字串 + return@lazy "^.*?第(?:[\\d零〇一二两三四五六七八九十百千万壹贰叁肆伍陆柒捌玖拾佰仟]+)[章节篇回集话](?!$)|^(?:[\\d零〇一二两三四五六七八九十百千万壹贰叁肆伍陆柒捌玖拾佰仟]+[,:、])*(?:[\\d零〇一二两三四五六七八九十百千万壹贰叁肆伍陆柒捌玖拾佰仟]+)(?:[,:、](?!$)|\\.(?=[^\\d]))".toRegex() } + private val regexC by lazy { + //前后附加内容,整个章节名都在括号中时只剔除首尾括号,避免将章节名替换为空字串 + return@lazy "(?!^)(?:[〖【《〔\\[{(][^〖【《〔\\[{()〕》】〗\\]}]+)?[)〕》】〗\\]}]$|^[〖【《〔\\[{(](?:[^〖【《〔\\[{()〕》】〗\\]}]+[〕》】〗\\]})])?(?!$)".toRegex() + } + private fun getPureChapterName(chapterName: String?): String { return if (chapterName == null) "" else StringUtils.fullToHalf(chapterName) .replace(regexA, "") .replace(regexB, "") + .replace(regexC, "") .replace(regexOther, "") } -} \ No newline at end of file +}