Source: https://www.regextester.com/
Character classes | |
---|---|
. | any character except newline |
\w \d \s | word, digit, whitespace |
\W \D \S | not word, digit, whitespace |
[abc] | any of a, b, or c |
[^abc] | not a, b, or c |
[a-g] | character between a & g |
Anchors | |
^abc$ | start / end of the string |
\b | word boundary |
Escaped characters | |
\. \* \\ | escaped special characters |
\t \n \r | tab, linefeed, carriage return |
\u00A9 | unicode escaped © |
Groups & Lookaround | |
(abc) | capture group |
\1 | backreference to group #1 |
(?:abc) | non-capturing group |
(?=abc) | positive lookahead |
(?!abc) | negative lookahead |
Quantifiers & Alternation | |
a* a+ a? | 0 or more, 1 or more, 0 or 1 |
a{5} a{2,} | exactly five, two or more |
a{1,3} | between one & three |
a+? a{2,}? | match as few as possible |
ab|cd | match ab or cd |
Source: PHP / Javascript 正規表示法 檢測 中文 的表示式 | 微。細節
在Javascript中:
[\u4e00-\u9fa5]
在PHP中此則表示法要改為16進制:
[\x4e00-\x9fa5]
使用以上正規表示法式子 可匹配中文字
為什麼? 請參考
更多表示式範例
Source: 比較詳細Python正則表達式操作指南(re使用)_python_腳本之家
REGex TESTER v1.5.3 - test/validate regular expressions, online tester
Regex Examples (PCRE)
Regex Cheatsheet | nowhere useful.
orginally from: addedbytes.com
It's really incredibly useful, and every time i create a new regex string i can't resist to give it a look.
Recipe 6.15. Greedy and Non-Greedy Matches
Problem
You have a pattern with a greedy quantifier like *
, +
, ?
, or {}
, and you want to stop it from being greedy.
Regular Expression 判斷 1-99 | Tsung's Blog
剛開始想 ^[1-9]?\d$
就解決了, 但是又有 0 的狀況, 突然覺得似乎不太容易, 隨手 Google 找 regex 1-99
, 沒想到有這問題的還真不少.