Advertisement
Guest User

Untitled

a guest
Jan 29th, 2015
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.70 KB | None | 0 0
  1. import java.util.HashMap;
  2. import java.util.Random;
  3. import java.util.regex.Matcher;
  4. import java.util.regex.Pattern;
  5.  
  6. public class Algorithm {
  7.  
  8. static HashMap<String, String> dictionarie = new HashMap<String, String>();
  9. static Random rand = new Random();
  10.  
  11. public static String getFromDict(String dict, int count) {
  12. StringBuffer result = new StringBuffer();
  13.  
  14. for (int i = 0; i < count; i++)
  15. result.append(dict.charAt(rand.nextInt(dict.length())));
  16.  
  17. return result.toString();
  18. }
  19.  
  20. public static String change(String str) {
  21. StringBuffer output = new StringBuffer();
  22.  
  23. Matcher matcher = Pattern.compile("\\\\([a-z])\\{([1-9][0-9]*|[1-9][0-9]*,[1-9][0-9]*)\\}").matcher(str);
  24. while (matcher.find()) {
  25. if (dictionarie.containsKey(matcher.group(1))) {
  26. int quant = 0;
  27. if (matcher.group(2).contains(",")) {
  28. String[] numb = matcher.group(2).split(",");
  29. if (Integer.parseInt(numb[1]) > Integer.parseInt(numb[0]))
  30. quant = rand.nextInt(Integer.parseInt(numb[1]) - Integer.parseInt(numb[0]) + 1 ) + Integer.parseInt(numb[0]);
  31. else {
  32. matcher.appendReplacement(output, "\\"+matcher.group(0));
  33. continue;
  34. }
  35. } else
  36. quant = Integer.parseInt(matcher.group(2));
  37.  
  38. matcher.appendReplacement(output, getFromDict(dictionarie.get(matcher.group(1)), quant));
  39.  
  40. } else
  41. matcher.appendReplacement(output, "\\"+matcher.group(0));
  42. }
  43. matcher.appendTail(output);
  44. return output.toString();
  45. }
  46.  
  47. public static void main(String[] args) {
  48. dictionarie.put("d", "0123456789");
  49. dictionarie.put("w", "abcdefghijklmnopqrstuvxwyz");
  50.  
  51. for(int i = 0; i < 1000; i++)
  52. System.out.println(change("\\d{3}.\\d{3}.\\d{3}-\\d{2}"));
  53. }
  54.  
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement