kirish - Metabelgilar pythonagi maxsus belgilar bo’lib, ular regular ifodalarda maxsus bir ma’noga ega. Ular textdagi ma’lumotlarni moslashtirish uchun ishlatiladi. Ularga ro’yhat – [ ] , backslash - \ , nuqta - . , daraja oshirish - ^ , dollar - $ , ko’paytirish - * , plus - + , so’roq – ? , lug’at – { } , or – | , oddiy qavs – ( ) . Endi bular haqida ma’lumotlarni ko’raylik. Ro’yhat deganim bu belgilardan iborat to’plam ya’ni ko’rinishi [a-z] , [A-Z] yoki [0-9] .
- import re
- txt = “Toshkent – O’zbekistonning poytaxti."
- x = re.findall("[a-m]", txt)
- print(x)
- Output : [‘h’, ‘k’, ‘e’, ‘b’, ‘e’, ‘k’, ‘i’, ‘a’, ‘i’]
- Bu yerda txtdagi a dan m gacha bo’lgan barcha kichkina harflarni list ko’rinishida chiqarib beradi. Yana bunga ko’pgina misollarni keltirsak bo’ladi.
Metabelgilar - Endi esa backslashni ma’nosini ko’raylik. Bunda u maxsus ketma-ketlik ichidan maxsus signal belgilarni list ko’rinishida chiqarib beradi.
- import re
- txt = “Iphone narxi 59 dollars"
- x = re.findall("\d", txt)
- print(x)
- Output: [‘5’, ‘9’]
- Bu yerda textdagi hamma raqmlarni topyapti.
- Yana \d{2} ham bor, u 59 ni listda chiqaradi. Misol tariqasida \d+ ya’ni textdagi hamma sonlarni chiqaradi, \w+ textdagi tablarni tashlab belgilarni chiqaradi. Bularning hammasi list shaklida bo’ladi.
Keling nuqta belgisiga ahamiyat beraylik. - Keling nuqta belgisiga ahamiyat beraylik.
- import re
- txt = "hello planet"
- #Search for a sequence that starts with "he", followed by two (any) characters, and an "o":
- x = re.findall("he..o", txt)
- print(x)
- Bu yerda nuqta textdagi so’zni he bilan boshlanishini va 2ta belgi bo’lishi o bilan o’rtasida. Yana u textdagi hamma belgilarni bittadan list shaklida chiqarib ham beradi.
- Daraja - ^
- U textni boshlanishi qanday boshlangani haqida ma’lumot beradi. Ya’ni,
import re - import re
- txt = "hello planet"
- #Check if the string starts with 'hello':
- x = re.findall("^hello", txt)
- if x:
- print(“Xa, bu yerda text 'hello’ bilan boshlanadi")
- else:
- print(“Yo’q unday emas")
- Ko’rib turibmizki buyerda Xa, bu yerda text 'hello’ bilan boshlanadi deganni ko’rishimiz mumkin.
- Endi esa $ ni ko’raylik. U textni oxiri wunday belgi bilan tugaydimi yo’qmi shunga javob beradi. Ya’ni
import re - import re
- txt = "hello planet"
- #Check if the string ends with 'planet':
- x = re.findall("planet$", txt)
- if x:
- print(“Xa text 'planet’ bilan tugaydi")
- else:
- print(“Yo’q unday emas”)
- Bunda Xa text 'planet’ bilan tugaydi chiqarishimiz mumkin.
- Endi * bunda u textdagi so’zni o ta yoki undan ko’p bo’lgan belgilarii chiqarib beradi. Ya’ni
import re - import re
- txt = "hello planet"
- #Search for a sequence that starts with "he", followed by 0 or more (any) characters, and an "o":
- x = re.findall("he.*o", txt)
- print(x)
- Output: [‘hello’]
- import re
- txt = "hello planet"
- #Search for a sequence that starts with "he", followed by 1 or more (any) characters, and an "o":
- x = re.findall("he.+o", txt)
- print(x)
- Bu yerda ‘he’ dan keyin 1ta yoki undan ko’p bo’lgan so’zlarni textda topadi.
import re - import re
- txt = "hello planet"
- #Search for a sequence that starts with "he", followed by 0 or 1 (any) character, and an "o":
- x = re.findall("he.?o", txt)
- print(x)
- #This time we got no match, because there were not zero, not one, but two characters between "he" and the "o“
- Bu yerda endi he va o o’rtasida ikkita belgi bor bo’lgani uchun [] javobi chiqadi.
- import re
- txt = "The rain in Spain falls mainly in the plain!"
x = re.findall("falls|stays", txt) - print(x)
- if x:
- print("Yes, there is at least one match!")
- else:
- print("No match")
- Bu yerda falls yoki stays textda bormi yo’qligini aniqlaymiz. Ya’ni bor.
Do'stlaringiz bilan baham: |