35 lines
1.1 KiB
Kotlin
35 lines
1.1 KiB
Kotlin
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")
|
||
}
|
||
}
|
||
}
|
||
|