# RegExp 筆記 * [咻咻的筆記小站](https://hengxiuxu.blogspot.com/2017/10/regular-expression.html) * [regex101](https://regex101.com/r/Dyi5q9/3) * :question: 請幫我找出桃園縣(H), 台南市(D), 嘉義縣(Q)中為女生的身分證字號 ## Lookahead lookbehind > 站在 X 往前看 Y -> lookbehind > 站在 X 往後看 Y -> lookahead * Ref: [RegExp 應用: lookahead , lookbehind](https://darkk6.blogspot.com/2017/03/regexp-lookahead-lookbehind.html) * :question: 在一個字串中找出連續數字 6~8 個」,這邊要注意的是,連續九個的話是不要的。 * Test case: * `12345 XD Hi12345678ab666666cd987654321` * want: * `12345678` * `666666` * don't want: * `98765432` * `87654321` * Answer * `(?<!\d)\d{6,8}(?!\d)` * `(?<!\d)\d{6,8}` * look for `` * `\d{6,8}(?!\d)` * [reg101](https://regex101.com/r/V6EhJD/1) * lookahead (要找的 `X` 在前面) * positive * `X(?=Y)` * e.g. :question: `123A4C5B67` 我要找一個數字,且後方必須是 `A` 或 `B` * [Ans](https://regex101.com/r/YFyovN/1) * negative * `X(?!Y)` * e.g. :question: `123A4C5B67` 我要找一個數字,且後方不能是 `A` 或 `B` * [Ans](https://regex101.com/r/J3I5Wr/1) * lookbehind (要找的 `X` 在後面) * positive * `(?<=Y)X` * `?<`: 前面的文字 * `=Y`: matches `Y` * e.g. :question: `AOIOOXPAOCKNJS` 要找任何一個字,且前方必須是O或P。或也可以這樣來稱呼:「尋找接再 O 或 P 後面的一個文字」 * [Ans](https://regex101.com/r/7EY1Rr/1) * e.g. :question: 擷取出 `file_XXXXX` 後面的文字 XXXXX * file_record_transcript.pdf * file_07241999.pdf * oldfile_from_bk201.pdf * testfile_fake.pdf.tmp * [ans](https://regex101.com/r/HylxeS/1) * e.g. 找出檔案在幾點幾分創立 ```shell= ubuntu@ubuntu_qemu_kvm:~/xxx$ ls -la | grep -P "(?<=Dec 29).*\s" drwxrwxr-x 5 ubuntu ubuntu 4096 Dec 29 09:28 . drwxr-xr-x 16 ubuntu ubuntu 4096 Dec 29 09:29 .. drwxrwxr-x 2 ubuntu ubuntu 4096 Dec 29 09:27 regexp -rw-rw-r-- 1 ubuntu ubuntu 59 Dec 29 09:28 t.txt ``` * ans: * e.g. 找出 `(f *Form) 的 method name` ```bash= func (f *Form) Required(fields ...string) { func (f *Form) MaxLength(field string, d int) { func (f *Form) PermittedValues(field string, opts ...string) { func (f *Form) Valid() bool { ``` * Ans: [regex101](https://regex101.com/r/YFyovN/2) :::info * MacOS 不支援 lookbehind! * 可用 brew install grep * 並用 -P set to Perl * ref: [Ask Different](https://apple.stackexchange.com/questions/256902/on-osx-grep-can-i-use-look-ahead#:~:text=grep%20in%20macOS%20does%20not,which%20does%20support%20your%20regex.) ::: * negative * `(?<!Y)X` * `?<`: 前面的文字 * `!Y`: doesn't match `Y` # BackTrack * :question: What is backTrack? * When condition not match, the state will go back to prev state. # Greedy and lazy * [咻咻](https://hengxiuxu.blogspot.com/2017/10/regular-expression.html) * 找 開頭為 `a` 結尾是 `t` * [reg101](https://regex101.com/r/P1pSz4/22) * [javascript.info tutorial](https://javascript.info/regexp-greedy-and-lazy) * :question: Try to explain FSM of `a.*t` and `a.*?t` # 自己做 regexp parser * [Finite State Machines and Regular Expressions](https://www.gamedev.net/tutorials/_/technical/general-programming/finite-state-machines-and-regular-expressions-r3176/) * contain simple c++ example of FSM in regexp * [LiveOverflow - Regular expression as Finite-state machine - Short](https://www.youtube.com/watch?v=hprXxJHQVfQ) * 對 FSM 的 brief introduction
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up