(* compare the results for testing match with Mathematica*) SetAttributes[g,Orderless] SetAttributes[h,Flat] SetAttributes[gh,{Flat,Orderless}] ques={List[f[a | b, b], f[a, b]], List[f[a | b, b], f[b, b]], List[f[a_, b_, a_ | b_], f[x, y, y]], List[f[a_, b_, a_ | b_], f[x, y, x]], List[f[a_ | b_, a_, b_], f[x, x, y]], List[f[_, _], f[1, 2]], List[f[ww_, y], f[x, y]], List[f[_Integer, _], f[1, 2]], List[f[_Symbol, _], f[x, 2]], List[f[x__], f[a, b, c]], List[f[x___Symbol], f[x, y]], List[a:b_, xxx], List[f[x___Symbol], f[x, 2]], (* false *) List[f[x___Integer, 2], f[1, 2]], List[f[x___], f[1, y]], List[f[a__], f[1, 2]], List[f[a__, b__], f[1, 2]], List[f[a___, b__], f[1, 2]], List[P[a_, b_Sin], P[x, Sin[y]]], List[a:f[ b_], f[c]], List[w[a_, b_Sin], w[x, Sin[y]]], List[f[x_], f[a, b, c]], List[f[x__], f[a, b, c]], List[f[x___], f[a, b, c]], List[gh[a_, b_Sin], gh[Sin[y], x]], List[gh[a___, b_Sin], gh[Sin[y]]], List[f[a_ | b_, a_, b_], f[y, x, y]], List[f[a_ | b_, a_, b_], f[x, x, y]], List[q[a_, b_], q[1, 2]], List[q[a_, a_], q[1, 2]], List[x:f[z_], f[z[3]]], List[f[a, Optional[x_, 0]], f[a]], List[f[Optional[x_, 0], a], f[a]], List[f[Optional[x_, 0], a], f[xx, a]], List[w[a__], w[x, Sin[y]]], List[P[a__], P[x, Sin[y]]], List[P[a___], P[x, y, z]], List[f[a__, b__], f[1, 2, 3, 4]], List[f[a__, b__], f[1, 2]], List[q[a__, a__], q[1, 2]], List[g[a, b], g[a, b]], List[w[x_, a], w[b, a]], List[g[a, b, x_], g[a, c, b]], List[gh[a, b, x_], gh[a, c, b, d]], List[f[a, x_, b], f[a, c, b]], List[ff[x__, b], ff[b, a]], List[ff[x__, b], ff[a, b]], List[ff[x__, b], ff[a, c, b]], List[ff[a, x__, b], ff[a, c, b]], List[ff[a, x__, x__, b], ff[a, c, d, c, d, b]], List[f[a___, b__], f[1, 2, 3, 4]], List[r[x_, s[x_]], r[a, s[a]]], List[r[x__, s[x__]], r[a, b, s[a, b]]], List[r[x___, s[x___]], r[s[]]], List[f[Optional[x_, 0], a], f[a]], List[w[x_, f[x_]], w[a, f[a]]], List[w[a, b], w[b, a]], List[w[Except[b]], w[a]], List[w[Except[b]], w[b]], List[a b, b a], List[g[a, b, x_], g[a, c, b]], List[a b c x_, a c b], List[a + b + c + x_, a + c + b], List[a + b + c + x_, a + c + b + 3 z], List[a + b + c + d e x_, a + c + b + d e], List[a + b + c + d e x_, a + c + b + d e f g], List[(d + YY) (e + ZZ), (d + YY) (e + ZZ)], List[(d + y_) (e + z_), (d + YY) (e + ZZ)], List[(d + YY) (e + ZZ), (YY + d) (e + ZZ)], List[a + Sin[a], Sin[a] + a], List[x_ + Sin[a], Sin[a]], List[g[a, b], g[b, a]], List[x1__, e + f], List[a + x_, a + b], List[a + x_, a + b + c], List[b + x_, a + b + c], List[a + b + z_, a + b], List[a + b + z___, a + b], List[a + b + z__, a + b], List[a + z_, a], List[a + x_, a], List[a + b + z___, a + b], List[a + b + z_, a + b], List[ff[x__, y__, x__], ff[a, b, a]], List[ff[x__, y___, x__], ff[a, a]], List[ww[a c, b y_], ww[a c, b d]], List[ww[a x_, b y_], ww[a c, b d]], List[ww[a x_, b y_], ww[a r s, b t u]], List[ww[a x_, b y_, z___], ww[a r s, b t u, 34, 35]], List[b y_ + a, a + b d], List[b y_ + x_, a + b d], List[b y_ + x_, a c + b d], List[b y_ + a x_, a c + b d], List[b x_ + a x_, a c + b d], List[x1___ + d + e, a + b + c + d + e], List[a + x1___ + c, a + b + c], List[x1__ + c + d + x2___, a + b + c + d + e], List[x1___ + c + d + x2___, a + b + c + d + e], List[g[a, b, x__], g[a, c, b]], List[x1___ + b + d, a + b + c + d + e], List[x_ + y_ + x_, a + b + a], List[x_ + y__ + x_, a + b + b + b + a], List[x_ + y__ + x_, a + b + b + b + a + c], List[gh[x_], gh[a, b, c]], List[gh[x_, b], gh[b, a]], List[x + PatternTest[y_, OddQ], x + 3], List[x + PatternTest[y_, OddQ], x + 4], List[PatternTest[y_, OddQ] + PatternTest[z_, EvenQ], 3 + 4], List[y_Integer + z_, 3 + x + y], List[x_ + Sin[x_], Sin[a] + a], List[x__ + y__ + x__, a + b + c + a + b], List[f[a, b, x^Optional[n_, 1] , c], f[a, b, x, c]], List[f[a, b, x^Optional[n_,1] , c], f[a, b, x^4 , c]], List[Optional[n_] q, q], List[Optional[n_, 1] q r, q r], List[Optional[n_] q r, q r], List[Optional[n_] + r, r], List[f[a, b, x^Optional[n_, 1] , c], f[a, b, x, c]], List[a + b + x^Optional[n_, 1] + c, a + x + c + b], List[a + b + x^Optional[n_, 1] + c, a + b + x^5 + c], List[b y_ z_ + a x_, a c + b d gg], List[x__ + y__ + x__, a + b + a], List[x_ + y__, a + b + a], List[x_ + y_ + x_, a + b + a], List[w[Cos[x_], Sin[x_]], w[Cos[a], Sin[a]]], List[Cos[x_] + Sin[x_], Cos[a] + Sin[a]], List[w[x__, x__], w[1, 2, 3, 1, 2, 3]], List[gh[x__, x__], gh[1, 2, 3, 1, 2, 3]], List[gh[x__, x__], gh[1, 1, 2, 2, 3, 3]]} Table [ {k, ques[[k]], MatchQ[ ques[[k]][[2]],ques[[k]][[1]]] }, {k,1,Length[ques] }]