الفرق بين المراجعتين ل"Kotlin/kotlin.text/MatchResult/destructured"

من موسوعة حسوب
اذهب إلى التنقل اذهب إلى البحث
سطر 30: سطر 30:
 
==أنظر أيضًا==
 
==أنظر أيضًا==
 
* صفحة الصنف <code>[[Kotlin/kotlin.text/MatchResult/Destructured/index|Destructured]]</code>.
 
* صفحة الصنف <code>[[Kotlin/kotlin.text/MatchResult/Destructured/index|Destructured]]</code>.
 +
* الخاصية <code>[[Kotlin/kotlin.text/MatchResult/groups|MatchResult.groups]]</code>: تعيد الخاصية <code>groups</code>  كائنًا من النوع <code>[[Kotlin/kotlin.text/MatchGroupCollection/index|MatchGroupCollection]]</code> يضم مجموعة المُطابقات الموجودة في  [[Kotlin/kotlin.text/Regex/index|التعبير النمطي]]. حجم تلك المجموعة يساوي <code>groupCount + 1،</code> حيث<code>groupCount</code> هو عدد المجموعات المطابَقة في  <nowiki/>[[Kotlin/kotlin.text/Regex/index|التعبير النمطي]]. تلك المجموعات مُفهرسة من 1 حتى <code>groupCount،</code> والمجموعة ذات الفهرس <code>0</code> توافق المطابقة الكلية.
 +
* الخاصية <code>[[Kotlin/kotlin.text/MatchResult/groupValues|MatchResult.groupValues]]</code>: تعيد  [[Kotlin/List|لائحة]] تضم مجموعة المُطابقات  <nowiki/>[[Kotlin/kotlin.text/Regex/index|للتعبير النمطي]]. حجم تلك [[Kotlin/List|اللائحة]] يساوي <code>groupCount + 1،</code> حيث<code>groupCount</code> هو عدد المجموعات المطابَقة في  <nowiki/>[[Kotlin/kotlin.text/Regex/index|التعبير النمطي]]. تلك المجموعات مُفهرسة من 1 حتى <code>groupCount،</code> والمجموعة ذات الفهرس <code>0</code> توافق المطابقة الكلية.
  
 
==مصادر==
 
==مصادر==

مراجعة 22:36، 6 يوليو 2018

الخاصية destructured‎ تعيد نسخة من الصنف MatchResult.Destructured، والتي توفّر مركبات لتفكيك قيم المجموعة المُطابقة. المركبة الأولى تقابل قيمة المجموعة المُطابقَة الأولى، والمركبة الثانية تقابل الثانية، وهكذا دواليك.

البنية العامة

open val destructured: Destructured

القيمة المُعادة

نسخة من الصنف MatchResult.Destructured.

أمثلة

استخدام الخاصية destructured‎

ينشِئ التابع Regex()‎ في الشيفرة الآتية تعبيرًا نمطيًا باسم regex لتفكيك مسارات الملفات إلى ثلاث مُركّبات: المجلد (directory) واسم الملف (fileName) والامتداد (extension)، ثم نعرّف سلسلة نصية باسم fullPath تحتوي مسار أحد الملفات، ثم نستخرج من fullPath مجموعات النتائج عبر استدعاء الدالةmatchEntire()‎  عبر regex وتمرير fullPath إليها كوسيط، ثم نستخدم الخاصيةdestructured‎ لاستخراج مجموعات النتائج، ثم نطبع الناتج:

package com.kotlination.filename

fun main(args: Array<String>) {

val fullPath = "Kotlination/Kotlin/Practice/getFileNameExample.kt"

val regex = """(.+)/(.+)\.(.+)""".toRegex()
val matchResult = regex.matchEntire(fullPath)

if (matchResult != null) {
  val (directory, fileName, extension) = matchResult.destructured
  println("dir: $directory | fileName: $fileName | extension: $extension")
  // => dir: Kotlination/Kotlin/Practice | fileName: getFileNameExample | extension: kt
}

}

أنظر أيضًا

مصادر