Python RegEx
RegEx၊ သို့မဟုတ် ပုံမှန်ဖော်ပြချက်သည် ရှာဖွေမှုပုံစံတစ်ခုအဖြစ် ဖန်တီးထားသည့် ဇာတ်ကောင်များ၏ အတွဲတစ်ခုဖြစ်သည်။
စာကြောင်းတစ်ခုတွင် သတ်မှတ်ထားသော ရှာဖွေမှုပုံစံပါရှိမရှိ စစ်ဆေးရန် RegEx ကို သုံးနိုင်သည်။
RegEx မော်ဂျူး
re
Python တွင် Regular Expressions နှင့် အလုပ်လုပ်ရန် အသုံးပြုနိုင်သည့် built-in package တစ်ခုရှိသည် ။
re
မော်ဂျူး ကို တင်သွင်းပါ -
import re
Python တွင် RegEx
မော်ဂျူးကို သင် တင်သွင်းသောအခါ re
၊ သင်သည် ပုံမှန်အသုံးအနှုန်းများကို စတင်အသုံးပြုနိုင်သည်-
ဥပမာ
"The" ဖြင့်စတင်ပြီး "စပိန်" ဖြင့်အဆုံးသတ်ခြင်းရှိမရှိကြည့်ရန် string ကိုရှာပါ-
import
re
txt = "The rain in Spain"
x = re.search("^The.*Spain$", txt)
RegEx လုပ်ဆောင်ချက်များ
မော် re
ဂျူးသည် ကျွန်ုပ်တို့အား ကိုက်ညီမှုအတွက် စာကြောင်းတစ်ကြောင်းကို ရှာဖွေနိုင်စေမည့် လုပ်ဆောင်ချက်အစုံကို ပေးဆောင်သည်-
Function | Description |
---|---|
findall | Returns a list containing all matches |
search | Returns a Match object if there is a match anywhere in the string |
split | Returns a list where the string has been split at each match |
sub | Replaces one or many matches with a string |
Metacharacters
Metacharacters များသည် အထူးအဓိပ္ပါယ်ရှိသော ဇာတ်ကောင်များဖြစ်သည်-
Character | Description | Example | Try it |
---|---|---|---|
[] | A set of characters | "[a-m]" | |
\ | Signals a special sequence (can also be used to escape special characters) | "\d" | |
. | Any character (except newline character) | "he..o" | |
^ | Starts with | "^hello" | |
$ | Ends with | "planet$" | |
* | Zero or more occurrences | "he.*o" | |
+ | One or more occurrences | "he.+o" | |
? | Zero or one occurrences | "he.?o" | |
{} | Exactly the specified number of occurrences | "he{2}o" | |
| | Either or | "falls|stays" | |
() | Capture and group |
အထူးအစီအစဉ်များ
အထူးအစီအစဥ်သည် \
အောက်ဖော်ပြပါစာရင်းရှိ ဇာတ်ကောင်များထဲမှ တစ်ခု၏နောက်တွင် ပါရှိပြီး အထူးအဓိပ္ပါယ်ရှိသည်-
Character | Description | Example | Try it |
---|---|---|---|
\A | Returns a match if the specified characters are at the beginning of the string | "\AThe" | |
\b | Returns a match where the specified characters are at the beginning or at the
end of a word (the "r" in the beginning is making sure that the string is being treated as a "raw string") |
r"\bain" r"ain\b" |
|
\B | Returns a match where the specified characters are present, but NOT at the beginning
(or at
the end) of a word (the "r" in the beginning is making sure that the string is being treated as a "raw string") |
r"\Bain" r"ain\B" |
|
\d | Returns a match where the string contains digits (numbers from 0-9) | "\d" | |
\D | Returns a match where the string DOES NOT contain digits | "\D" | |
\s | Returns a match where the string contains a white space character | "\s" | |
\S | Returns a match where the string DOES NOT contain a white space character | "\S" | |
\w | Returns a match where the string contains any word characters (characters from a to Z, digits from 0-9, and the underscore _ character) | "\w" | |
\W | Returns a match where the string DOES NOT contain any word characters | "\W" | |
\Z | Returns a match if the specified characters are at the end of the string | "Spain\Z" |
အစုံပါပဲ။
အတွဲတစ်ခုသည် အထူးအဓိပ္ပါယ်ရှိသော စတုရန်းကွင်းများအတွင်းမှ ဇာတ်ကောင်အစုအဝေးတစ်ခုဖြစ်သည်
[]
-
Set | Description | Try it |
---|---|---|
[arn] | Returns a match where one of the specified characters (a ,
r , or n ) are
present |
|
[a-n] | Returns a match for any lower case character, alphabetically between
a and n |
|
[^arn] | Returns a match for any character EXCEPT a ,
r , and n |
|
[0123] | Returns a match where any of the specified digits (0 ,
1 , 2 , or
3 ) are
present |
|
[0-9] | Returns a match for any digit between
0 and 9 |
|
[0-5][0-9] | Returns a match for any two-digit numbers from 00 and
59 |
|
[a-zA-Z] | Returns a match for any character alphabetically between
a and z , lower case OR upper case |
|
[+] | In sets, + , * ,
. , | ,
() , $ ,{}
has no special meaning, so [+] means: return a match for any
+ character in the string |
findall() လုပ်ဆောင်ချက်
လုပ်ဆောင်ချက် သည် findall()
ကိုက်ညီမှုအားလုံးပါဝင်သော စာရင်းကို ပြန်ပေးသည်။
ဥပမာ
ကိုက်ညီမှုအားလုံး၏စာရင်းကို ပရင့်ထုတ်ပါ။
import re
txt = "The rain in Spain"
x = re.findall("ai",
txt)
print(x)
စာရင်းတွင် ၎င်းတို့တွေ့ရှိသည့် အစီအစဥ်အတိုင်း ကိုက်ညီမှုများပါရှိသည်။
ကိုက်ညီမှုမတွေ့ပါက၊ စာရင်းအလွတ်တစ်ခုကို ပြန်ပေးသည်-
ဥပမာ
တူညီမှုမတွေ့ပါက ဗလာစာရင်းကို ပြန်ပေးပါ။
import re
txt = "The rain in Spain"
x = re.findall("Portugal",
txt)
print(x)
search() လုပ်ဆောင်ချက်
လုပ်ဆောင်ချက် သည် search()
ကိုက်ညီမှုတစ်ခုအတွက် စာကြောင်းကို ရှာဖွေပြီး ကိုက်ညီမှု ရှိလျှင် တူညီသည့် အရာတစ်ခု ကို ပြန်ပေးသည်။
ပွဲစဉ်တစ်ခုထက်ပိုပါက၊ ပွဲ၏ပထမအကြိမ်မှသာလျှင် ပြန်လည်ရောက်ရှိပါမည်-
ဥပမာ
စာကြောင်းရှိ ပထမဆုံး နေရာလွတ် စာလုံးကို ရှာပါ-
import re
txt = "The rain in Spain"
x = re.search("\s",
txt)
print("The first white-space character is located in
position:", x.start())
ကိုက်ညီမှုမရှိပါက၊ တန်ဖိုးကို None
ပြန်ပေးသည်-
ဥပမာ
ကိုက်ညီမှုမရှိသော ရှာဖွေမှုကို ပြုလုပ်ပါ-
import re
txt = "The rain in Spain"
x = re.search("Portugal",
txt)
print(x)
split() လုပ်ဆောင်ချက်
လုပ်ဆောင်ချက် သည် split()
ကိုက်ညီမှုတစ်ခုစီတွင် စာကြောင်းခွဲပြီးသည့်စာရင်းကို ပြန်ပေးသည်-
ဥပမာ
နေရာလွတ် အက္ခရာတစ်ခုစီတွင် ခွဲထားသည်-
import re
txt = "The rain in Spain"
x = re.split("\s",
txt)
print(x)
ပါရာမီတာ ကို သတ်မှတ်ခြင်းဖြင့် သင်သည် ဖြစ်ပွားမှုအရေအတွက်ကို ထိန်းချုပ်နိုင်သည်
maxsplit
-
ဥပမာ
ပထမအကြိမ်တွင်သာ string ကို ခွဲပါ-
import re
txt = "The rain in Spain"
x = re.split("\s",
txt,
1)
print(x)
sub() လုပ်ဆောင်ချက်
sub()
လုပ်ဆောင်ချက်သည် ကိုက်ညီမှုများကို သင်ရွေးချယ်သော စာသားဖြင့် အစားထိုးသည် -
ဥပမာ
နေရာလွတ် အက္ခရာတိုင်းကို နံပါတ် 9 ဖြင့် အစားထိုးပါ။
import re
txt = "The rain in Spain"
x = re.sub("\s",
"9", txt)
print(x)
ကန့်သတ်ချက်များ သတ်မှတ်ခြင်းဖြင့် အစားထိုးမှုအရေအတွက်ကို သင်ထိန်းချုပ်နိုင်သည်
count
-
ဥပမာ
ပထမအကြိမ် ၂ ခုကို အစားထိုးပါ။
import re
txt = "The rain in Spain"
x = re.sub("\s",
"9", txt, 2)
print(x)
အရာဝတ္ထုကို ယှဉ်ပါ။
Match Object သည် ရှာဖွေမှုနှင့် ရလဒ်ဆိုင်ရာ အချက်အလက် ပါဝင်သော အရာဝတ္ထုတစ်ခု ဖြစ်သည်။
မှတ်ချက်- ကိုက်ညီမှုမရှိပါက၊ None
Match Object အစား တန်ဖိုးကို ပြန်ပေးပါမည်။
ဥပမာ
Match Object ကို ပြန်ပေးမည့် ရှာဖွေမှုကို ပြုလုပ်ပါ-
import re
txt = "The rain in Spain"
x = re.search("ai",
txt)
print(x) #this will print an object
Match object တွင် ရှာဖွေမှုနှင့် ပတ်သက်သော အချက်အလက်များကို ရယူရန် အသုံးပြုသည့် ဂုဏ်သတ္တိများနှင့် နည်းလမ်းများ ရှိပြီး ရလဒ်-
.span()
ပွဲ၏ start- နှင့် end position များပါရှိသော tuple တစ်ခုကို ပြန်ပေးသည်။
.string
function ထဲသို့ ဖြတ်သွားသော string ကို
.group()
return ပြန်သည် ၊ match ရှိခဲ့သည့် string ၏ အစိတ်အပိုင်းကို ပြန်ပေးသည်
ဥပမာ
ပထမပွဲဖြစ်ပွားမှု၏ အနေအထား (အစ-နှင့် အဆုံး-အနေအထား) ကို ပရင့်ထုတ်ပါ။
ပုံမှန်အသုံးအနှုန်းသည် စာလုံးကြီး "S" ဖြင့် စတင်သည့် မည်သည့်စကားလုံးကိုမဆို ရှာဖွေသည်-
import re
txt = "The rain in Spain"
x = re.search(r"\bS\w+", txt)
print(x.span())
ဥပမာ
လုပ်ဆောင်ချက်ထဲသို့ ဖြတ်သွားသော စာကြောင်းကို ပရင့်ထုတ်ပါ-
import re
txt = "The rain in Spain"
x = re.search(r"\bS\w+", txt)
print(x.string)
ဥပမာ
ကိုက်ညီမှုရှိသည့် စာတန်း၏အပိုင်းကို ပရင့်ထုတ်ပါ။
ပုံမှန်အသုံးအနှုန်းသည် စာလုံးကြီး "S" ဖြင့် စတင်သည့် မည်သည့်စကားလုံးကိုမဆို ရှာဖွေသည်-
import re
txt = "The rain in Spain"
x = re.search(r"\bS\w+", txt)
print(x.group())
မှတ်ချက်- ကိုက်ညီမှုမရှိပါက၊ None
Match Object အစား တန်ဖိုးကို ပြန်ပေးပါမည်။