Java ပုံမှန်အသုံးအနှုန်းများ
Regular Expression ဆိုတာ ဘာလဲ။
ပုံမှန်အသုံးအနှုန်းသည် ရှာဖွေမှုပုံစံတစ်ခုဖန်တီးသည့် ဇာတ်ကောင်များ၏ အတွဲလိုက်တစ်ခုဖြစ်သည်။ စာသားတစ်ခုတွင် ဒေတာကို သင်ရှာဖွေသောအခါ၊ သင်ရှာဖွေနေသည့်အရာကို ဖော်ပြရန် ဤရှာဖွေမှုပုံစံကို အသုံးပြုနိုင်သည်။
ပုံမှန်ဖော်ပြချက်တစ်ခုသည် စာလုံးတစ်လုံးတည်း သို့မဟုတ် ပိုမိုရှုပ်ထွေးသောပုံစံတစ်ခု ဖြစ်နိုင်သည်။
စာသားရှာဖွေမှု အမျိုးအစားအားလုံးကို လုပ်ဆောင်ရန် နှင့် စာသားအစားထိုး လုပ်ဆောင်ချက်များကို လုပ်ဆောင်ရန် ပုံမှန်အသုံးအနှုန်းများကို အသုံးပြုနိုင်သည် ။
Java တွင် Built-in Regular Expression အတန်းမပါဝင်သော်လည်း ပုံမှန်ဖော်ပြချက်များ java.util.regex
နှင့် အလုပ်လုပ်ရန် အထုပ်ကို တင်သွင်းနိုင်ပါသည်။ Package တွင် အောက်ပါ အတန်းများ ပါဝင်သည် ။
Pattern
အတန်းအစား - ပုံစံတစ်ခုကို သတ်မှတ်သည် (ရှာဖွေမှုတွင် အသုံးပြုရန်)Matcher
အတန်း - ပုံစံကို ရှာဖွေရန် အသုံးပြုသည်။PatternSyntaxException
အတန်းအစား - ပုံမှန်ဖော်ပြမှုပုံစံတွင် အထားအသိုအမှားကို ညွှန်ပြသည်။
ဥပမာ
ဝါကျတစ်ကြောင်းတွင် "w3schools" ဟူသော စကားလုံး၏ ဖြစ်ပျက်မှုများ ရှိ၊ မရှိ ရှာကြည့်ပါ-
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) {
Pattern pattern = Pattern.compile("w3schools", Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher("Visit W3Schools!");
boolean matchFound = matcher.find();
if(matchFound) {
System.out.println("Match found");
} else {
System.out.println("Match not found");
}
}
}
// Outputs Match found
နမူနာရှင်းပြထားပါတယ်။
ဤဥပမာတွင်၊ "w3schools" ဟူသောစကားလုံးကို ဝါကျတစ်ခုတွင် ရှာဖွေနေပါသည်။
Pattern.compile()
ပထမဦးစွာ ပုံစံကို နည်းလမ်းဖြင့် ဖန်တီးသည် ။ ပထမပါရာမီတာသည် မည်သည့်ပုံစံကို ရှာဖွေနေသည်ဆိုသည်ကို ညွှန်ပြပြီး ဒုတိယ ဘောင်တွင် ရှာဖွေမှုသည် စာလုံးအသေးမပါရှိသင့်ကြောင်း ညွှန်ပြရန် အလံတစ်ခုရှိသည်။ ဒုတိယဘောင်သည် စိတ်ကြိုက်ရွေးချယ်နိုင်သည်။
string တစ်ခု ရှိ matcher()
ပုံစံကို ရှာဖွေရန် နည်းလမ်းကို အသုံးပြုသည်။ ၎င်းသည် လုပ်ဆောင်ခဲ့သည့် ရှာဖွေမှုနှင့်ပတ်သက်သည့် အချက်အလက်ပါရှိသော Matcher အရာဝတ္ထုကို ပြန်ပေးသည်။
string တွင် find()
ပုံစံကို တွေ့ရှိပါက နည်းလမ်းသည် true နှင့် မတွေ့ပါက false ပြန်ပေးသည်။
အလံများ
compile()
နည်းလမ်း ရှိအလံ များသည် ရှာဖွေမှုလုပ်ဆောင်ပုံကို ပြောင်းလဲသည်။ ဤတွင် ၎င်းတို့ထဲမှ အချို့မှာ-
Pattern.CASE_INSENSITIVE
- ရှာဖွေမှုတစ်ခုလုပ်ဆောင်သောအခါတွင် စာလုံးအသေးကို လျစ်လျူရှုပါမည်။Pattern.LITERAL
- ပုံစံရှိ အထူးဇာတ်ကောင်များသည် အထူးအဓိပ္ပာယ်ရှိမည်မဟုတ်ဘဲ ရှာဖွေမှုတစ်ခုလုပ်ဆောင်သောအခါတွင် သာမန်ဇာတ်ကောင်များအဖြစ် သတ်မှတ်ခံရမည်ဖြစ်သည်။Pattern.UNICODE_CASE
-CASE_INSENSITIVE
အင်္ဂလိပ်အက္ခရာ၏အပြင်ဘက်ရှိ စာလုံးများကို လျစ်လျူရှုရန် အလံ နှင့် တွဲသုံးပါ။
ပုံမှန်ဖော်ပြမှုပုံစံများ
method ၏ပထမ parameter Pattern.compile()
သည် ပုံစံဖြစ်သည်။ ရှာဖွေနေသည့်အရာကို ဖော်ပြသည်။
စာလုံးအကွာအဝေးကိုရှာရန် ကွင်းပိတ်များကိုအသုံးပြုသည်-
Expression | Description |
---|---|
[abc] | Find one character from the options between the brackets |
[^abc] | Find one character NOT between the brackets |
[0-9] | Find one character from the range 0 to 9 |
Metacharacters
Metacharacters များသည် အထူးအဓိပ္ပါယ်ရှိသော ဇာတ်ကောင်များဖြစ်သည်-
Metacharacter | Description |
---|---|
| | Find a match for any one of the patterns separated by | as in: cat|dog|fish |
. | Find just one instance of any character |
^ | Finds a match as the beginning of a string as in: ^Hello |
$ | Finds a match at the end of the string as in: World$ |
\d | Find a digit |
\s | Find a whitespace character |
\b | Find a match at the beginning of a word like this: \bWORD, or at the end of a word like this: WORD\b |
\uxxxx | Find the Unicode character specified by the hexadecimal number xxxx |
အရေအတွက်
Quantifiers သည် ပမာဏများကို သတ်မှတ်သည်-
Quantifier | Description |
---|---|
n+ | Matches any string that contains at least one n |
n* | Matches any string that contains zero or more occurrences of n |
n? | Matches any string that contains zero or one occurrences of n |
n{x} | Matches any string that contains a sequence of X n's |
n{x,y} | Matches any string that contains a sequence of X to Y n's |
n{x,} | Matches any string that contains a sequence of at least X n's |
မှတ်ချက်- သင့်ဖော်ပြချက်သည် အထူးဇာတ်ကောင်များထဲမှ တစ်ခုကို ရှာဖွေရန် လိုအပ်ပါက ၎င်းတို့ကို လွတ်မြောက်ရန် backslash ( \ ) ကို အသုံးပြုနိုင်သည်။ Java တွင်၊ strings မှ backslashes များသည် ၎င်းတို့ကိုယ်တိုင်လွတ်မြောက်ရန် လိုအပ်သည်၊ ထို့ကြောင့် အထူးစာလုံးများမှထွက်ရန် backslash နှစ်ခုလိုအပ်ပါသည်။ ဥပမာအားဖြင့်၊ တစ်ခု သို့မဟုတ် တစ်ခုထက်ပိုသော မေးခွန်းအမှတ်အသားများကို ရှာဖွေရန် အောက်ပါအချက်များကို သင်အသုံးပြုနိုင်သည်- "\\?"