yunzesms/SMS/app/src/main/java/com/yunzer/sms/SmsParser.kt
2026-03-25 16:56:45 +08:00

35 lines
1.1 KiB
Kotlin
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package com.yunzer.sms
data class ParsedCode(
val code: String?,
val status: String // matched/unmatched/error
)
object SmsParser {
private val regexByKeyword = Regex("""验证码[:\s]*([0-9]{4,8})""")
private val regex4 = Regex("""\b([0-9]{4})\b""")
private val regex6 = Regex("""\b([0-9]{6})\b""")
private val regex8 = Regex("""\b([0-9]{8})\b""")
fun parse(content: String): ParsedCode {
return try {
val c1 = regexByKeyword.find(content)?.groupValues?.getOrNull(1)
if (!c1.isNullOrEmpty()) return ParsedCode(c1, "matched")
val c2 = regex6.find(content)?.groupValues?.getOrNull(1)
if (!c2.isNullOrEmpty()) return ParsedCode(c2, "matched")
val c3 = regex8.find(content)?.groupValues?.getOrNull(1)
if (!c3.isNullOrEmpty()) return ParsedCode(c3, "matched")
val c4 = regex4.find(content)?.groupValues?.getOrNull(1)
if (!c4.isNullOrEmpty()) return ParsedCode(c4, "matched")
ParsedCode(code = null, status = "unmatched")
} catch (_: Exception) {
ParsedCode(code = null, status = "error")
}
}
}