fork download
  1. /* Expressões regulares com Java Patterns
  2.   http://pt.stackoverflow.com/q/186142/53463 */
  3.  
  4. import java.util.regex.Matcher;
  5. import java.util.regex.Pattern;
  6.  
  7. class Ideone
  8. {
  9. public static void main (String[] args) throws java.lang.Exception
  10. {
  11. final String regex = "^[^ab]*+(?=(?:[^b]*b){2})(?:[^a]*a){2}[^a]*$";
  12.  
  13. String[] exemplos = new String[] {
  14. "---aabb+++","---bbaa+++","---abab+++","---baba+++",
  15. "---babba++","---bbbbbaa","ababbb++++","ccabcab+++",
  16. "----bcdbaa","-ababd++++","bbbaabbbbb","bbbabbbbbb",
  17. "bbbaaabbbb","baaaaaaaaa","abbbbbbbbb","ccacbcacbc"
  18. };
  19.  
  20.  
  21. final Pattern pattern = Pattern.compile(regex);
  22.  
  23. for (String palavra : exemplos) {
  24. Matcher matcher = pattern.matcher(palavra);
  25.  
  26. if (matcher.find()) {
  27. System.out.println(palavra + " ✔️");
  28. } else {
  29. System.out.println(palavra + " ✖️️");
  30. }
  31. }
  32. }
  33. }
Success #stdin #stdout 0.04s 4386816KB
stdin
Standard input is empty
stdout
---aabb+++ ✔️
---bbaa+++ ✔️
---abab+++ ✔️
---baba+++ ✔️
---babba++ ✔️
---bbbbbaa ✔️
ababbb++++ ✔️
ccabcab+++ ✔️
----bcdbaa ✔️
-ababd++++ ✔️
bbbaabbbbb ✔️
bbbabbbbbb ✖️️
bbbaaabbbb ✖️️
baaaaaaaaa ✖️️
abbbbbbbbb ✖️️
ccacbcacbc ✔️