sitcon 2014 - regular expression introduce

30
正規表示式簡介 http://goo.gl/EkSH5S 2014/03/15 中興資工 戴均民

Upload: -

Post on 25-May-2015

2.550 views

Category:

Education


0 download

DESCRIPTION

http://sitcon.org/2014/

TRANSCRIPT

Page 1: SITCON 2014 - Regular Expression Introduce

正規表示式簡介

http://goo.gl/EkSH5S

2014/03/15 中興資工 戴均民

Page 2: SITCON 2014 - Regular Expression Introduce

先給大家看一份 沒有用正規表示法的程式碼

2014/3/15 #sitcon2014

Page 3: SITCON 2014 - Regular Expression Introduce

2014/3/15 #sitcon2014

Page 4: SITCON 2014 - Regular Expression Introduce

2014/3/15 #sitcon2014

Page 5: SITCON 2014 - Regular Expression Introduce

2014/3/15 #sitcon2014

Page 6: SITCON 2014 - Regular Expression Introduce

正規表示式出沒!

處理文字之必(ㄕㄥˋ)備(ㄌㄧˋ)神(ㄅㄠˇ)器(ㄐㄧㄢˋ)

2014/3/15 #sitcon2014

Page 7: SITCON 2014 - Regular Expression Introduce

2014/3/15 #sitcon2014

Page 8: SITCON 2014 - Regular Expression Introduce

Everywhere in unix-like

• vim

• grep

• awk

• sed

2014/3/15 #sitcon2014

Page 10: SITCON 2014 - Regular Expression Introduce

Yacc & Bison

• http://zh.wikipedia.org/wiki/Yacc

• http://zh.wikipedia.org/wiki/GNU_bison

(這個我也沒玩過)

2014/3/15 #sitcon2014

Page 11: SITCON 2014 - Regular Expression Introduce

Modern Editors

• Sublime Text

• Notepad++

• …

(這裡只有列我常用的)

2014/3/15 #sitcon2014

Page 12: SITCON 2014 - Regular Expression Introduce

常用資料驗證語法

PCRE: PHP, javascript, R, Python …

2014/3/15 #sitcon2014

Page 13: SITCON 2014 - Regular Expression Introduce

2014/3/15 #sitcon2014

檢查手機

• https://www.debuggex.com/r/YOZU9u9F1hNCfTtf

Page 14: SITCON 2014 - Regular Expression Introduce

2014/3/15 #sitcon2014

西元日期驗證

• https://www.debuggex.com/r/SLXmA7kq8ke6C7wE

Page 15: SITCON 2014 - Regular Expression Introduce

2014/3/15 #sitcon2014

電子郵件

• https://www.debuggex.com/r/8xMsCex6P5m_munC

Page 16: SITCON 2014 - Regular Expression Introduce

2014/3/15 #sitcon2014

身份證驗證

• https://www.debuggex.com/r/-GSnGp0AH860Llbk

• 在此僅簡易驗證台灣身份證,仍然需要一些進階的檢查,如驗證檢查碼,或前往內政部戶政司 驗證。

Page 17: SITCON 2014 - Regular Expression Introduce

2014/3/15 #sitcon2014

網址驗證

• https://www.debuggex.com/r/kc64M_PYYHaArOe7

Page 18: SITCON 2014 - Regular Expression Introduce

2014/3/15 #sitcon2014

密碼驗證範例

• https://www.debuggex.com/r/JupgbYL0r3vE22kg

Page 19: SITCON 2014 - Regular Expression Introduce

網路資源簡介

2014/3/15 #sitcon2014

Page 20: SITCON 2014 - Regular Expression Introduce

2014/3/15 #sitcon2014

debuggex

• https://www.debuggex.com/

• Feature – 即時測試

– 圖像化顯示正規表示式

– 支援 JS, Python, PCRE

– 可以設定 Unit Test

– 可以儲存, 分享

– 有很多範例可參考

– 不支援 Replace

Page 21: SITCON 2014 - Regular Expression Introduce

2014/3/15 #sitcon2014

regexr

• http://www.regexr.com/

• Feature

– 即時測試

– 程式非常輕量,簡單易用

– 同時支援 Match 和 Replace 模式

– 提供儲存分享功能

– 提供離線版本,包含 Windows / Mac OSX

Page 22: SITCON 2014 - Regular Expression Introduce

2014/3/15 #sitcon2014

標點符號英文名稱

• http://en.wikipedia.org/wiki/Template:Punctuation_marks

• 由於無法使用在 Google 用符號的方式搜尋正規表示式的教學,所以只好折衷用符號的英文來查啦。

• 舉例:想要搜尋 (?<=) 的說明,可以使用 「regex question mark less than equals」 來搜尋,只打符號是沒用的。

Page 23: SITCON 2014 - Regular Expression Introduce

2014/3/15 #sitcon2014

維基百科:正規表示式

• http://zh.wikipedia.org/wiki/%E6%AD%A3%E8%A6%8F%E8%A1%A8%E7%A4%BA%E5%BC%8F

• Feature – 有 PCRE 規範的詳盡表格

– 恩….就是維基

Page 24: SITCON 2014 - Regular Expression Introduce

2014/3/15 #sitcon2014

regular-expressions.info

• http://www.regular-expressions.info/quickstart.html

• Feature – 教學網站

– 英文要很好

– 語法標色清晰

– 上面的資料五花八門,還頗詳盡的

– 部份資料需要付費

Page 25: SITCON 2014 - Regular Expression Introduce

2014/3/15 #sitcon2014

Regex Crossword

• http://regexcrossword.com/

• Feature

– 解迷練習

Page 26: SITCON 2014 - Regular Expression Introduce

情境練習 TWITCHPLAYSPOKEMON

2014/3/15 #sitcon2014

Page 27: SITCON 2014 - Regular Expression Introduce

2014/3/15 #sitcon2014

twitchplayspokemon

• 從2014/02/13開始,有神人在遊戲實況網站Twitch開台,讓玩家可以輸入指令,一起決定這個神奇寶貝紅版的命運。

• 在開台的過程中,實況主有也在隨時更新程式,後來甚至出現民主模式與無政府狀態。

• 紅版於2014/3/2破關,歷時16天7小時48分鐘,最高峰時曾達到十二萬人同時在線

http://brea.komica.org/wiki/index.php?Twitch%20Plays%20Pokemon

Page 28: SITCON 2014 - Regular Expression Introduce

2014/3/15 #sitcon2014

抓住閃電鳥的瞬間

Page 29: SITCON 2014 - Regular Expression Introduce

2014/3/15 #sitcon2014

用正規表達式驗證輸入

• 允許輸入這些指令

– up, down, left, right, a, b, start

• 在民主模式下,指令可以連鎖

– left2down2 代表兩次left+兩次up

• 請試著寫正規表示法驗證輸入的合法性 – 參考: https://www.debuggex.com/r/IrCM821yHvnS8vs-

Page 30: SITCON 2014 - Regular Expression Introduce

Any Question?

2014/3/15 #sitcon2014