有沒有想過,當使用者按下按鈕時,是如何觸發回應視窗,或者是其他動作呢?
一開始我們先記得,在事件處理的過程中,涉及主要下面內容:
1. Event Source(事件源)(Object):事件發生的場所,通常是各個元件,例如按鈕、視窗、選單...等。
2.Event(事件)(Object):當我們對某個元件,做出一些動作時,他會封裝所做的動作相關資訊。
3.Event Listener(事件監聽器)(Object):當有事件源發生事件時(監聽),會對各種事件派送給事件處理器來做相關處理及回應。
4.Event Handler(事件處理器)(Function):當事件監聽器將事件派遣給事件處理器,他會做一連串的回應動作。
參考:細說Java 8 異常處理與圖形介面程式設計 一書
2017年7月12日 星期三
2017年7月6日 星期四
正規表達式(Regular Expression, regex) for Java
--------------------------------------------------------------------------------------------------------------------------
普通字元組
[0-9], \d =>比對數字串
[a-z] =>比對小寫英文字母串
[0-9A-Za-z], \w=>比對數字串
-g =>(global) 全域比對,如果沒家只會比對第一個
-
-
[...] =>代表那些括號內的字元
[^...] =>代表不要那些括號內的字元
+ =>代表出現1次以上
* =>代表出現0次以上
? =>代表出現0次或1次
\n =>換行
\t =>跳格(tab)
\r =>windows中常接著\n一起代表換行(ASCII=0x0c)
\s =>空白型字元
\b =>匹配邊界
. =>代表任何字母(換行除外)
^ =>不在[...]裡面時,代表字串手
$ =>代表字串結尾
(預設)Greedy =>貪婪模式會一直匹配下去,直到無法匹配為止
(?)Reluctant =>勉強模式,只會匹配最少字元。
(+
\X代表不是\x的那些字母
(...) =>代表一個群組
*可以看這個網頁,補充很多知識
https://atedev.wordpress.com/2007/11/23/%E6%AD%A3%E8%A6%8F%E8%A1%A8%E7%A4%BA%E5%BC%8F-regular-expression/
--------------------------------------------------------------------------------------------------------------------------
POSIX字元組
--------------------------------------------------------------------------------------------------------------------------
(1).
可以觀看test2檔案執行結果
需找出c或d所在的位置。
而如果要連續
matcher的st
(2).
可以觀看test3檔案執行結果
來看一下+,*,? 和貪婪模式、勉強模式的差別。
(3).
可以觀看test4檔案執行結果
來看一下group的執行結果,其中他會依序尋找其結果
(4)
可以觀看test5檔案執行結果
用一個簡單的例子,試著抓出他的電話號碼
(5)
可以觀看test6檔案執行結果
找出每個句首大寫的單字
(6)
可以觀看test7檔案執行結果
可以利用此程式,知道該號碼為手機、家裡電話、或錯誤號碼
*
http://notes.maxwi.com/2015/10/06/Regex-practice/
* 如果想看更多範例可以到下面網址
http://www.iteye.com/topic/350789
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static void test3(){ | |
String inputStr="hello , java"; | |
//print($ ,java); | |
System.out.println(inputStr.replaceFirst("\\w*", "\\$")); | |
//print($hello , java); | |
System.out.println(inputStr.replaceFirst("\\w*?", "\\$")); | |
//print($ello , java); | |
System.out.println(inputStr.replaceFirst("\\w+?", "\\$")); | |
//print($hello , java); | |
System.out.println(inputStr.replaceFirst("\\w??", "\\$")); | |
//print($ , java);; | |
System.out.println(inputStr.replaceFirst("\\w+", "\\$")); | |
//print($ello , java); | |
System.out.println(inputStr.replaceFirst("\\w", "\\$")); | |
//print($ello , java); | |
System.out.println(inputStr.replaceFirst("\\w?", "\\$")); | |
//print($ , java); | |
System.out.println(inputStr.replaceFirst("\\w+", "\\$")); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void test7(){ | |
String inputStr1="0922-233-444"; | |
String inputStr2="0922233444"; | |
String inputStr3="02-1111-2222"; | |
String inputStr4="02-11112222"; | |
String inputStr5="54645612"; | |
String filterRegex="[-]";// or [^0-9] | |
Pattern pattern=Pattern.compile(filterRegex); | |
Matcher matcher=pattern.matcher(inputStr5); | |
String inputStr6=matcher.replaceAll(""); | |
System.out.println(inputStr6); | |
String mobileRegex="(09)+\\d{8}"; | |
pattern=Pattern.compile(mobileRegex); | |
matcher=pattern.matcher(inputStr6); | |
if(matcher.matches()){ | |
System.out.println("This is phone number"); | |
}else{ | |
String phoneRegex="(02)+\\d{8}"; | |
pattern=Pattern.compile(phoneRegex); | |
matcher=pattern.matcher(inputStr6); | |
if(matcher.matches()){ | |
System.out.println("This is home number"); | |
} | |
else{ | |
System.out.println("The number is error scheme"); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void test6(){ | |
String inputStr="WASHINGTON — North Korea’s newly demonstrated missile muscle puts Alaska within range of potential attack and stresses the Pentagon’s missile defenses like never before. Even more worrisome, it may be only a matter of time before North Korea mates an even longer-range ICBM with a nuclear warhead, putting all of the United States at risk."; | |
String regex="[A-Z]{1}[a-z]{1,}"; | |
Pattern pattern=Pattern.compile(regex); | |
Matcher matcher=pattern.matcher(inputStr); | |
while(matcher.find()){ | |
System.out.println(matcher.start()); | |
System.out.println(matcher.group(0)); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static void test5(){ | |
String inputStr="I want to buy the book, please contact the phone 0933485543"+ | |
"make friends, the phone 0934858219"+ | |
"sell the computer,contact the phone 0944584321"; | |
String patternStr="(09)\\d{8}"; | |
Pattern pattern=Pattern.compile(patternStr); | |
Matcher matcher=pattern.matcher(inputStr); | |
while(matcher.find()){ | |
System.out.println(matcher.group()); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static void test1(){ | |
String inputStr="1"; | |
String regex="[\\p{Alpha}]+"; | |
System.out.println(inputStr.matches(regex)); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static void test4(){ | |
String inputStr = "This order was placed for QT3000! OK?"; | |
String patternStr = "(\\D*)(\\d+)(.*)"; | |
Pattern pattern = Pattern.compile(patternStr); | |
Matcher matcher = pattern.matcher(inputStr); | |
if (matcher.find( )) { | |
//代表(\\D*)(\\d+)(.*) | |
System.out.println("Found value: " +matcher.group(0) ); | |
//代表(\\D*) | |
System.out.println("Found value: " +matcher.group(1) ); | |
//代表(\\d+) | |
System.out.println("Found value: " +matcher.group(2) ); | |
//代表(.*) | |
System.out.println("Found value: " +matcher.group(3) ); | |
} else { | |
System.out.println("NO MATCH"); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static void test2(){ | |
String inputStr="abdZZcdeQQbe"; | |
String patternStr="[c-d]{1}"; | |
Pattern pattern=Pattern.compile(patternStr); | |
Matcher matcher=pattern.matcher(inputStr); | |
if(matcher.find()){ | |
System.out.println(matcher.start()); | |
System.out.println(matcher.group(0)); | |
} | |
} |
訂閱:
文章 (Atom)