Anagram Validator


/ Published in: Java
Save to your folder(s)

This is a snippet to validate anagrams of 'debit card'. It validates 'debit card' and 'bad credit'


Copy this code and paste it in your HTML
  1. //regular expression
  2. String patternStr = "(?=.*d.*d)(?=.*e)(?=.*b)(?=.*i)(?=.*t)(?=.*\\s)(?=.*c)(?=.*a)(?=.*r)^..........$";
  3. boolean b;
  4. Matcher matcher;
  5. Pattern pattern;
  6.  
  7. System.out.println("Regex pattern = "+patternStr);
  8. pattern = Pattern.compile(patternStr);//compiles the regex
  9.  
  10. //validates bad credit
  11. System.out.println("Validating: bad credit");
  12. matcher = pattern.matcher("bad credit");
  13. b = matcher.matches();
  14. if(b) System.out.println("bad credit: matched");
  15. else System.out.println("bad credit: not matched");
  16.  
  17. //validates debit card
  18. System.out.println("Validating: debit card");
  19. matcher = pattern.matcher("debit card");
  20. b = matcher.matches();
  21. if(b) System.out.println("debit card: matched");
  22. else System.out.println("debit card: not matched");

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.