正規式 (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)
quick ways to initialize a list of numbers or strings in java
Java 快速建立整數與字串清單的寫法 在 Java 中,建立 List<Integer> 與 List<String> 是非常常見的需求。 以下依照不同 JDK 版本,整理出幾種 快速建立可修改(modifiab...
沒有留言:
張貼留言