Regulyar ifoda orqali satrdan izlash
System.Text.RegularExpressions.Regex klassi satrlarni qidirish uchun ishlatilishi mumkin. Ushbu qidiruvlar murakkablik darajasida oddiydan murakkab matn naqshlarigacha bo'lishi mumkin.
Quyidagi kod misoli jumladagi “the” yoki “ularning” so‘zlarini katta-kichik registrga e’tibor bermasdan qidiradi. Regex.IsMatch statik usuli qidiruvni amalga oshiradi. Siz unga qidirish uchun satr va qidiruv namunasini berasiz. Bunday holda, uchinchi argument katta-kichik harflarga sezgir bo'lmagan qidiruvni belgilaydi.
string[] sentences =
{
"Put the water over there.",
"They're quite thirsty.",
"Their water bottles broke."
};
string sPattern = "the(ir)?\\s";
foreach (string s in sentences)
{
Console.Write($"{s,24}");
if (System.Text.RegularExpressions.Regex.IsMatch(s, sPattern, System.Text.RegularExpressions.RegexOptions.IgnoreCase))
{
Console.WriteLine($" (match for '{sPattern}' found)");
}
else
{
Console.WriteLine(); } }
Etaberilganlar va Kvantifikatorlar
Miqdor ko'rsatkichlari ketma-ketlikdagi elementlarning bir qismi yoki barchasi shartni qondiradimi yoki yo'qligini ko'rsatadigan mantiqiy qiymatni qaytaradi .
Metodlar
All-Ketma-ketlikning barcha elementlari shartni qanoatlantirishini aniqlaydi.
Any-Ketma-ketlikning biron bir elementi shartni qondiradimi yoki yo'qligini aniqlaydi.
Contains-Ketma-ketlikda belgilangan element mavjudligini aniqlaydi.
Quyidagi misol Allbarcha satrlarning ma'lum uzunlikda ekanligini tekshirish uchun ishlatiladi.
class Market
{
public string Name { get; set; }
public string[] Items { get; set; }
}
public static void Example()
{
List markets = new List
{
new Market { Name = "Emily's", Items = new string[] { "kiwi", "cheery", "banana" } },
new Market { Name = "Kim's", Items = new string[] { "melon", "mango", "olive" } },
new Market { Name = "Adam's", Items = new string[] { "kiwi", "apple", "orange" } },
};
// Determine which market have all fruit names length equal to 5
IEnumerable names = from market in markets
where market.Items.All(item => item.Length == 5)
select market.Name;
foreach (string name in names)
{
Console.WriteLine($"{name} market");
}
// This code produces the following output:
//
// Kim's market
}
Quyidagi misol Containsmassivda belgilangan element mavjudligini tekshirish uchun ishlatiladi.
class Market
{
public string Name { get; set; }
public string[] Items { get; set; }
}
public static void Example()
{
List markets = new List
{
new Market { Name = "Emily's", Items = new string[] { "kiwi", "cheery", "banana" } },
new Market { Name = "Kim's", Items = new string[] { "melon", "mango", "olive" } },
new Market { Name = "Adam's", Items = new string[] { "kiwi", "apple", "orange" } },
};
// Determine which market contains fruit names equal 'kiwi'
IEnumerable names = from market in markets
where market.Items.Contains("kiwi")
select market.Name;
foreach (string name in names)
{
Console.WriteLine($"{name} market");
}
// This code produces the following output:
//
// Emily's market
// Adam's market
}
|