From 3cfc2b3ba628338b49e5f032312595699dde4b3c Mon Sep 17 00:00:00 2001 From: KaustubhPatange Date: Sun, 8 Oct 2023 15:42:27 +0530 Subject: [PATCH] Fix: Phone number matching pattern --- .../xclipper/extensions/utils/ClipUtils.kt | 31 +++++++++++++------ .../xclipper/ui/helpers/SpecialHelper.kt | 6 ++-- 2 files changed, 24 insertions(+), 13 deletions(-) diff --git a/XClipper.Android/modules/core/src/main/java/com/kpstv/xclipper/extensions/utils/ClipUtils.kt b/XClipper.Android/modules/core/src/main/java/com/kpstv/xclipper/extensions/utils/ClipUtils.kt index 43c43e8b..1a8890c5 100644 --- a/XClipper.Android/modules/core/src/main/java/com/kpstv/xclipper/extensions/utils/ClipUtils.kt +++ b/XClipper.Android/modules/core/src/main/java/com/kpstv/xclipper/extensions/utils/ClipUtils.kt @@ -7,8 +7,12 @@ import com.kpstv.xclipper.extensions.small class ClipUtils { companion object { - private const val PHONE_PATTERN_REGEX = "(\\+\\d{1,2}\\s)?\\(?\\d{3}\\)?[\\s.-]?\\d{3}[\\s.-]?\\d{4}" // matches international numbers - private const val PHONE_PATTERN_REGEX1 = "(\\+[\\d-]{1,4})[\\s\\.]?(\\d{5})[\\s\\.]?(\\d{5})" // matches some specific number patterns + private const val PHONE_PATTERN_REGEX = + "(\\+\\d{1,2}\\s)?\\(?\\d{3}\\)?[\\s.-]?\\d{3}[\\s.-]?\\d{4}" // matches international numbers + private const val PHONE_PATTERN_REGEX1 = + "(\\+[\\d-]{1,4})[\\s\\.]?(\\d{5})[\\s\\.]?(\\d{5})" // matches some specific number patterns + private const val PHONE_PATTERN_REGEX2 = + "(\\+[\\d-]{1,4})?((\\d{3})\\s?(\\d{3})\\s?(\\d{4})|((\\d{5})\\s?(\\d{5})))" // should match all private const val EMAIL_PATTERN_REGEX = "([a-zA-Z0-9_\\-\\.]+)@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([a-zA-Z0-9\\-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)" private const val URL_PATTERN_REGEX = @@ -19,8 +23,13 @@ class ClipUtils { "(?:(?:31(\\/|-|\\.)(?:0?[13578]|1[02]))\\1|(?:(?:29|30)(\\/|-|\\.)(?:0?[13-9]|1[0-2])\\2))(?:(?:1[6-9]|[2-9]\\d)?\\d{2})\$|^(?:29(\\/|-|\\.)0?2\\3(?:(?:(?:1[6-9]|[2-9]\\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))\$|^(?:0?[1-9]|1\\d|2[0-8])(\\/|-|\\.)(?:(?:0?[1-9])|(?:1[0-2]))\\4(?:(?:1[6-9]|[2-9]\\d)?\\d{2})" private const val MARKDOWN_IMAGE_ONLY_REGEX = "^(!\\[)(.*?)(])(\\((https?://.*?)\\))$" - fun isMarkdownImage(data: String) : Boolean = (MARKDOWN_IMAGE_ONLY_REGEX.toRegex().matches(data)) - fun getMarkdownImageUrl(data: String) : String? { + private val PHONE_PATTERN_REGEX_LIST = + listOf(PHONE_PATTERN_REGEX2, PHONE_PATTERN_REGEX, PHONE_PATTERN_REGEX1) + + fun isMarkdownImage(data: String): Boolean = + (MARKDOWN_IMAGE_ONLY_REGEX.toRegex().matches(data)) + + fun getMarkdownImageUrl(data: String): String? { return MARKDOWN_IMAGE_ONLY_REGEX.toRegex().matchEntire(data)?.groupValues?.get(5) } @@ -37,10 +46,9 @@ class ClipUtils { return dictList /** Matches the phone number pattern. */ - if (!patternAdder(PHONE_PATTERN_REGEX, data, ClipTag.PHONE, dictList) - ) { - /** If not matched by first pattern we will try second one. */ - patternAdder(PHONE_PATTERN_REGEX1, data, ClipTag.PHONE, dictList) + PHONE_PATTERN_REGEX_LIST.forEach { pattern -> + if (patternAdder(pattern, data, ClipTag.PHONE, dictList)) + return@forEach } /** Date pattern matcher */ @@ -55,7 +63,12 @@ class ClipUtils { return dictList } - private fun patternAdder(pattern: String, data: String, tag: ClipTag, dictList: ArrayList): Boolean { + private fun patternAdder( + pattern: String, + data: String, + tag: ClipTag, + dictList: ArrayList + ): Boolean { val regex: Regex = pattern.toRegex() if (regex.containsMatchIn(data)) { val results: Sequence = regex.findAll(data) diff --git a/XClipper.Android/modules/feature-special/src/main/java/com/kpstv/xclipper/ui/helpers/SpecialHelper.kt b/XClipper.Android/modules/feature-special/src/main/java/com/kpstv/xclipper/ui/helpers/SpecialHelper.kt index 73a49dfa..5d967308 100644 --- a/XClipper.Android/modules/feature-special/src/main/java/com/kpstv/xclipper/ui/helpers/SpecialHelper.kt +++ b/XClipper.Android/modules/feature-special/src/main/java/com/kpstv/xclipper/ui/helpers/SpecialHelper.kt @@ -294,10 +294,8 @@ internal class SpecialHelper( val whatsAppTextMenu = SingleMenuItem(image = R.drawable.sp_ic_whatsapp, title = context.getString(R.string.sp_send_whatsapp)) { createChooser(ClipTag.PHONE.small()) { data -> - val numberToWhatsApp = when (data.length) { - 10 -> "+${getCountryDialCode(context)} $data" - else -> data - } + val numberToWhatsApp = if (data.startsWith("+")) data + else "+${getCountryDialCode(context)} $data" val intent = Intent(Intent.ACTION_VIEW).apply { setData(Uri.parse("https://wa.me/$numberToWhatsApp")) flags = Intent.FLAG_ACTIVITY_NEW_TASK