About the Tutorial



Download 2,85 Mb.
Pdf ko'rish
bet228/234
Sana28.05.2022
Hajmi2,85 Mb.
#614232
1   ...   224   225   226   227   228   229   230   231   ...   234
Bog'liq
python3 tutorial 060821094426

search
Function 
This function searches for first occurrence of RE 
pattern
within the 
string,
with 
optional 
flags

Here is the syntax for this function- 
re.search(pattern, string, flags=0)
Here is the description of the parameters- 
Parameter 
Description 
pattern 
This is the regular expression to be matched. 
string 
This is the string, which would be searched to match the 
pattern anywhere in the string. 
flags 
You can specify different flags using bitwise OR (|). These are 
modifiers, which are listed in the table below. 
The 
re.search
function returns a 
match
object on success
none
on failure. We 
use 
group(num) 
or 
groups() 
function of 
match
object to get the matched expression. 
Match Object 
Methods 
Description 
group(num=0) 
This method returns entire match (or specific subgroup num) 


Python 3 
349 
groups() 
This method returns all matching subgroups in a tuple (empty 
if there weren't any) 
Example 
#!/usr/bin/python3 
import re 
line = "Cats are smarter than dogs"; 
searchObj = re.search( r'(.*) are (.*?) .*', line, re.M|re.I) 
if searchObj: 
print ("searchObj.group() : ", searchObj.group()) 
print ("searchObj.group(1) : ", searchObj.group(1)) 
print ("searchObj.group(2) : ", searchObj.group(2)) 
else: 
print ("Nothing found!!")
When the above code is executed, it produces following result- 
matchObj.group() : Cats are smarter than dogs 
matchObj.group(1) : Cats 
matchObj.group(2) : smarter

Download 2,85 Mb.

Do'stlaringiz bilan baham:
1   ...   224   225   226   227   228   229   230   231   ...   234




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

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish