正規式 (a|b) 和 正規式 [ab] 兩者皆可匹配1個a或1個b,所以匹配效果一樣。 但是兩者的語法不同,表達能力也不同,說明如下: []表示匹配中括號(bracket)內字元集合的一個字元,所以只能匹配1個字元; ()表示匹配小括號(parenthesis)內直槓(vertical bar,|)隔開的任一正規式,所以可能匹配不止1個字元。 例如: 1.正規式 hello 匹配 hello 5個字元 2.正規式 [hello] 匹配 h或e或l或o 任1個字元 3.正規式 (hello) 匹配 hello 5個字元 4.正規式 (hello|world) 匹配 hello 或 world 5個字元 5.正規式 ([hello]|world) 匹配 h或e或l或o 任1個字元 或 world 5個字元 參考: difference b/w [ab] and (a|b) in regex match?
regular expressions (a|b) and [ab] are the same?
訂閱:
張貼留言 (Atom)
compare java wait/notify synchronization with lock or condition in java.util.concurrent (juc)
為什麼 Java 的 wait/notify 逐漸被新 API 取代? 在 Java 的演進過程中,雖然 Object.wait() 與 Object.notify() 仍存在於核心庫中,但在現代開發場景, ...
沒有留言:
張貼留言