Regular Expressions


startswith() kabi re.search() dan foydalanish


Download 233.56 Kb.
bet2/5
Sana19.06.2023
Hajmi233.56 Kb.
#1609008
1   2   3   4   5
Bog'liq
Pythonlearn-11-Regex

startswith() kabi re.search() dan foydalanish


import re
hand = open('mbox-short.txt')
for line in hand:
line = line.rstrip()
if re.search('^From:', line) :
print(line)
hand = open('mbox-short.txt')
for line in hand:
line = line.rstrip()
if line.startswith('From:') :
print(line)
Biz satrga maxsus belgilar qo'shish orqali mos keladigan narsani aniqlaymiz

Wild-Card belgilar


X-Sieve: CMU Sieve 2.3
X-DSPAM-Result: Innocent
X-DSPAM-Confidence: 0.8475
X-Content-Type-Message-Body: text/plain
^X.*:
Chiziqning boshini moslang
Har qanday belgini moslashtiring
Ko'p marotaba

Moslikni nozik sozlash

Ma'lumotlaringiz qanchalik "toza" ekanligiga va ilovangizning maqsadiga qarab, moslikni biroz qisqartirishni xohlashingiz mumkin.


X-Sieve: CMU Sieve 2.3
X-DSPAM-Result: Innocent
X-Plane is behind schedule: two weeks
X-: Very short
^X.*:
Chiziqning boshini moslang
Har qanday belgini moslashtiring
Ko'p marotaba

Moslikni nozik sozlash

Ma'lumotlaringiz qanchalik "toza" ekanligiga va ilovangizning maqsadiga qarab, moslikni biroz qisqartirishni xohlashingiz mumkin.


X-Sieve: CMU Sieve 2.3
X-DSPAM-Result: Innocent
X-: Very Short
X-Plane is behind schedule: two weeks
^X-\S+:
Chiziqning boshini moslang
Bo'sh joy bo'lmagan har qanday belgini moslang
Bir yoki bir necha marta

Ma'lumotlarni moslashtirish va chiqarish

  • re.search() satrning oddiy ifodaga mos kelishiga qarab True/False qiymatini qaytaradi
  • Agar biz mos keladigan satrlarni chiqarib olishni istasak, biz re.findall() dan foydalanamiz.

>>> import re
>>> x = 'My 2 favorite numbers are 19 and 42'
>>> y = re.findall('[0-9]+',x)
>>> print(y)
['2', '19', '42']
[0-9]+
Bir yoki bir nechta raqam

Download 233.56 Kb.

Do'stlaringiz bilan baham:
1   2   3   4   5




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling