Advertisement
Guest User

Untitled

a guest
Apr 24th, 2014
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.19 KB | None | 0 0
  1. // Note the alternation
  2. private static final Pattern PATTERN
  3. = Pattern.compile("\$[^.$]+(\.[^.$]+)*\$|[^.]+");
  4.  
  5. //
  6.  
  7. public List<String> matchesForInput(final String input)
  8. {
  9. final Matcher m = PATTERN.matcher(input);
  10. final List<String> matches = new ArrayList<>();
  11.  
  12. while (m.find())
  13. matches.add(m.group());
  14.  
  15. return matches;
  16. }
  17.  
  18. $[^$]+$|[[^]]+]|[^.]+
  19.  
  20. String s = "House.Car2.$4.45$.[0]";
  21. Pattern pattern = Pattern.compile("\$[^$]+\$|\[[^\]]+\]|[^.]+");
  22. Matcher matcher = pattern.matcher(s);
  23. while (matcher.find()) {
  24. System.out.println(matcher.group());
  25. }
  26.  
  27. House
  28. Car2
  29. $4.45$
  30. [0]
  31.  
  32. public static List<String> parse(String input){
  33. //list which will hold retuned tokens
  34. List<String> tokens = new ArrayList<>();
  35.  
  36. // flags representing if currently tested character is inside some of
  37. // special areas
  38. // (at start we are outside of these areas so hey are set to false)
  39. boolean insideDolar = false; // $...$
  40. boolean insideSquareBrackets = false; // [...]
  41. boolean insideAgleBrackets =false; // <...>
  42.  
  43. // we need some buffer to build tokens, StringBuilder is excellent here
  44. StringBuilder sb = new StringBuilder();
  45.  
  46. // now lets iterate over all characters and decide if we need to add them
  47. // to token or just add token to result list
  48. for (char ch : input.toCharArray()){
  49.  
  50. // lets update in which area are we
  51. // finding $ means that we either start or end `$...$` area so
  52. // simple negation of flag is enough to update its status
  53. if (ch == '$') insideDolar = !insideDolar;
  54. //updating rest of flags seems pretty obvious
  55. else if (ch == '[') insideSquareBrackets = true;
  56. else if (ch == ']') insideSquareBrackets = false;
  57. else if (ch == '<') insideAgleBrackets = true;
  58. else if (ch == '>') insideAgleBrackets = false;
  59.  
  60. // So now we know in which area we are, so lets handle special cases
  61. // if we are handling no dot
  62. // OR we are handling dot but we are inside either of areas we need
  63. // to just add it to token (append it to StringBuilder)
  64. if (ch != '.' || insideAgleBrackets|| insideDolar || insideSquareBrackets ){
  65. sb.append(ch);
  66. }else{// other case means that we are handling dot outside of special
  67. // areas where dots are not separators, so now they represents place
  68. // to split which means that we don't add it to token, but
  69. // add value from buffer (current token) to results and reset buffer
  70. // for next token
  71. tokens.add(sb.toString());
  72. sb.delete(0, sb.length());
  73. }
  74. }
  75. // also since we only add value held in buffer to list of tokens when we
  76. // find dot on which we split, there is high chance that we will not add
  77. // last token to result, because there is no dot after it, so we need to
  78. // do it manually after iterating over all characters
  79. if (sb.length()>0)//non empty token needs to be added to result
  80. tokens.add(sb.toString());
  81.  
  82. return tokens;
  83. }
  84.  
  85. String input = "House.Car2.$abc.def$<ghi.jk>.[0]";
  86. for (String s: parse(input))
  87. System.out.println(s);
  88.  
  89. House
  90. Car2
  91. $abc.def$<ghi.jk>
  92. [0]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement