# Regex 作業說明 ## 題目內容 請構造 regex (正規表達式)來匹配以下形式的字串。注意到你構造的每一行應該要剛剛好匹配成功(所以你的答案形式應該會是 `^...$`)。 > 註:答案是一行 `^` 開頭,`$` 結尾的正規表達式 > 評分標準:你的表達式應該要可以匹配到所有符合那個規則的整個字串,也就是說要完整匹配(full match)才正確。 1. 合法自然數(沒有前導零) :heavy_check_mark: `1`, `12`, `123`, `999999`, `1039402` :x: `-1`, `0123`, `00123`, `a123b`, `abc`, `0` 2. 合法整數(不含零) :heavy_check_mark: `1`, `-1`, `-12`, `123` :x: `0`, `a-1`, `-1a`, `--1`, `+1` 3. 合法身分證字號(字母 + 九個數字,忽略驗證規則) :heavy_check_mark: `A123456789` :x: `A1234567890`, `AB12345678`, `+_Av12d`, `A1234567890-` 4. 合法台灣手機號碼(09開頭,共10碼) :heavy_check_mark: `0912345678`, `0981789364` :x: `0891236482`, `0912345678a`, `091234567a` 5. 合法的 python 單行註解 :heavy_check_mark: `# 123`, `# 1#2#2#`, `#########` :x: `123 #`, `123 abc #` 6. 合法的 python 字串(只考慮由雙引號包起來的)(bonus) :heavy_check_mark: `"a"`, `"a\"b"`, `"a\n\\\""`, `"123"`, `""` :x: `"a`, `\"a`, `"\"`, `"a""b"`, `"\\\"`, `'a'` (hint: 這題很難,要考慮跳脫字元,會需要用到課外知識,可以參考[這個](https://www.fooish.com/regex-regular-expression/groups-lookaround.html)) 以上題目可以使用 python 的 `re` module 或是到 [regex101.com](regex101.com) 來檢查和 debug! ## 繳交說明 * 繳交期限: * 北區 2021/05/22 23:59:59 * 竹區 ~~2021/05/20 23:59:59~~ $\longrightarrow$ **2021/5/24 23:59:59** * 繳交方式:請將答案填至 Google Form * 連結:https://forms.gle/BvDgs9nvoDxyzf5v6 ## 解答 1. `^[1-9][0-9]*$` 2. `^[\-]?[1-9][0-9]*$` 3. `^[A-Z][0-9]{9}$` 4. `^09[0-9]{8}$` 5. `^#.*$` 6. `^\"(?:[^\\\"]+?|(?:\\.))*\"$`