Posted By

mangrar on 03/05/10


Tagged

regex java Expression validator anagram regulas


Versions (?)

Anagram Validator


 / Published in: Java
 

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

  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
Posted By: mangrar on March 5, 2010

A better example:

//Example: bad credit debit card String patternStr = "(?=.d.d)(?=.e)(?=.b)(?=.i)(?=.t)(?=.\s)(?=.c)(?=.a)(?=.r)^..........$"; String matcherStr = "bad credit";

You need to login to post a comment.