Python regular expressions တွင် findall method ကိုအသုံးပြုခြင်း
Python တွင် regular expression နှင့်ကိုက်ညီမှုအားလုံးရှာဖွေရန် findall method ကိုအသုံးပြုသည်။
method ၏ပထမသတ်မှတ်ချက်တွင်ရှာမည့် regex ကိုထည့်ပါ၊ ဒုတိယသတ်မှတ်ချက်တွင်ရှာမည့် string ကိုထည့်ပါ။
method သည်ဘယ်မှညာသို့ရှာဖွေပြီးကိုက်ညီမှုအားလုံးကိုစစ်ဆေးသည်။
ထို့အပြင်အဖွဲ့များမှရှာဖွေမှုများဖော်ပြပါကကိုက်ညီမှုများပါ string list သို့မဟုတ် tuple list ပြန်ပေးသည်။
ကိုက်ညီမှုမတွေ့ပါက ဗလာစာရင်းပြန်ပေးသည်။
findall method syntax သည်ဤသို့ပုံစံရှိသည်:
re.findall(what to search, where to search)
String ထဲတွင် regex နှင့်ကိုက်ညီမှုအားလုံးကိုရှာကြည့်ပါ:
txt = '12 43 56 ab'
res = re.findall('\d+', txt)
print(res)
ကုဒ် run ပြီးနောက်ရလဒ်:
['12', '43', '56']
string ပေးထားသည်:
txt = '123 aaa 456 bbb 789'
နံပါတ်များပါဝင်သော substring များ list ကိုထုတ်ပြပါ။
string ပေးထားသည်:
txt = 'abc 123 def 456 789'
အက္ခရာများပါဝင်သော substring များ list ကိုထုတ်ပြပါ။