Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

regex.alf.nu solutions by Bisqwit (and others)

By: Bisqwit on Dec 20th, 2013  |  syntax: PCRE  |  size: 3.12 KB  |  views: 1,697  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. plain strings (207)     foo
  2. anchors (208)           k$
  3. ranges (202)            ^[a-f]*$
  4. backrefs (201)          (...).*\1
  5. abba (190)              ^((?!(.)(.)\3\2).)*$
  6. a man, a plan (177)     ^(.)[^p].*\1$
  7. prime (286)             ^(?!(xx+)\1+$)
  8. four (199)              (.)(.\1){3}
  9. order (199)             ^.{5}[^e]?$
  10. triples (574)           ^(([147]4|40|3[269]|9[05]|[378]1).+|0[369]*|[81][257])*$
  11. glob (384)              (rr|ll|[lbr]o|en|ta|y|cr|eat|up).*\1
  12. balance (251)           ^((<>)*|<(?!>).*>)$    //five false positives.
  13. powers (59)             ^(.|(?!.{48}$)(..)*)$  //three false positives
  14. longcount (218)         0..1..1.. (..0){3}..1* ..0..1..1..0.. ..11(.{9}1){2}
  15. longcount2 (218)        0..1..1.. (..0){3}..1* ..0..1..1..0.. ..11(.{9}1){2}
  16. alphabetical (180)      ^(a[er]\w+ ?)*(ass\w+ ?)*(ast+ ?)*(e\w+ ?)*(n\w+ ?)*(r\w+ ?)*(s\w+ ?)*(t\w+ ?)*$ // 4 errors
  17.  
  18. These are regex.alf.nu solutions by Bisqwit. Usernames indicated are from Reddit unless otherwise noted.
  19.  
  20. Alternatives:
  21.  
  22. ranges (202)            [a-f]{4}               //made of abuse.
  23. order (199)             ^[^o]?.{5}$            //another way of writing the same thing.
  24. powers (59)             ^(x|(xx){1,4}|((((((x{16})\8?)\7?)\6?)\5?)\4?)\3?)$ //zero false positives,
  25.                                                                             //with thanks to Hrafnahnef.
  26. alphabetical (156)      ^(a[er]\w+ ?)*(asse[rn]\w+ ?)*(asse[st]\w+ ?)*(ast\w+ ?)*(e[an]\w+ ?)*(e[rts]\w+ ?)*(n\w+ ?)*((?:ra|re[anr])\w+ ?)*(rese\w+ ?)*(rest\w+ ?)*(ret\w+ ?)*(se\w+ ?)*(s[nt]\w+ ?)*(t\w+ ?)*$ // no false positives
  27.  
  28. Better solutions by others:
  29.  
  30. abba (193)              ^(?!.*(.)(.)\2\1)                  // by Laugarhraun.
  31. triples (596)           00($|3|6|9|12|15)|4.2|.1.+4|55|.17 // by alexandrosm @ [1]
  32. glob (397)              ai|c$|^p|[bcnrw][bnopr] // by nwellnhof @ [2]
  33. balance (287)           ^(<(<(<(<(<(<.*)*>)*>)*>)*>)*>)*$  // by jensweh.
  34. balance (288)           ^(<(<(<(..)*>)*>)*>)*$ // by romanandreev @ [1]; some false positives
  35. powers (80)             ^(((x|x{8}|x{128})\3?)\2?)\1?$     // by pondscum; no false positives.
  36. powers (97)             ^(?!(.(..)+)\1*$) // by plby @ [1]; doesn't "cheat"
  37. longcount (253)         ^((.+)0 \2+1 ?)*$ // possibly by bbarry @ [3]
  38. longcount2 (253)        ^((.+)0 \2+1 ?)*$ // possibly by bbarry @ [3]
  39. alphabetical (303)      r sn|( t\w+)\1|(tat|r). r|a t| ae|e e // by alexandrosm @ [1]
  40.  
  41. Best solutions that work in the spirit of the test without abusing the test-cases:
  42. (Where different from what is posted above; recursion shortcomings are ignored)
  43.  
  44. a man, a plan (176)     ^(.)(.).*\2\1$
  45. triples (523)           ^([0369]|[258][0369]*[147]|([258][0369]*[258]|[147])([0369]|[147][0369]*[258])*([258]|[147][0369]*[147]))*$                                                          // by Bisqwit.
  46. balance (286)           ^(<(<(<(<(<(<.*>)*>)*>)*>)*>)*>)*$                     // by Overv.
  47. order (156)             ^a*b*c*d*e*f*g*h*i*j*k*l*m*n*o*p*q*r*s*t*u*v*w*x*y*z*$ // by Athox.
  48. glob (323)              ^(?:(.+) .+ \1|(.*)\*(.*) .+ \2.+\3|(.*)\*(.*)\*(.*) .+ \4.+\5.+\6|\*(.*)\*(.*)\* .+ .+\7.+\8.+)$                                                                         // by Lozzer2
  49. glob (333)              ^(\*?)(\w*)(\*?)(\w*)(\*?)(\w*) .* ((.(?!\1))+|\1)\2((.(?!\3))+|\3)\4((.(?!\5))+|\5)\6$ // by hadrel @ [2]
  50.  
  51. [1]: https://gist.github.com/jonathanmorley/8058871
  52. [2]: https://news.ycombinator.com/item?id=6941231
  53. [3]: https://gist.github.com/jpsim/8057500