Pastebin launched a little side project called HostCabi.net, check it out ;-)Don't like ads? PRO users don't see any ads ;-)
Guest

Tokenize a string with a space in java

By: a guest on Dec 16th, 2011  |  syntax: None  |  size: 3.57 KB  |  hits: 28  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. String line = "a=b c='123 456' d=777 e='uij yyy'";
  2.        
  3. String [] words = line.split(" ");
  4.        
  5. a=b
  6. c='123 456'
  7. d=777
  8. e='uij yyy';
  9.        
  10. a=b abc='123 456' &=777 #='uij yyy' ABC='slk slk'              123sdkljhSDFjflsakd@*#&=456sldSLKD)#(
  11.        
  12. a=b
  13. abc='123 456'
  14. &=777
  15. #='uij yyy'
  16. ABC='slk slk'            
  17. 123sdkljhSDFjflsakd@*#&=456sldSLKD)#(
  18.        
  19. import java.util.ArrayList;
  20. import java.util.List;
  21. import java.util.regex.Matcher;
  22. import java.util.regex.Pattern;
  23.  
  24. public class RegexTest {
  25.  
  26.     // SPACE CHARACTER                                                                  followed by
  27.     // sequence of non-space characters of 1 or more                    followed by
  28.     // first occuring EQUALS CHARACTER           
  29.     final static String regex = " [^ ]+?=";
  30.  
  31.  
  32.     // static pattern defined outside so that you don't have to compile it
  33.     // for each method call
  34.     static final Pattern p = Pattern.compile(regex);
  35.  
  36.     public static List<String> tokenize(String input, Pattern p){
  37.         input = input.trim(); // this is important for "last token case"
  38.                                                         // see end of method
  39.         Matcher m = p.matcher(input);
  40.         ArrayList<String> tokens = new ArrayList<String>();
  41.         int beginIndex=0;
  42.         while(m.find()){
  43.                 int endIndex = m.start();
  44.                 tokens.add(input.substring(beginIndex, endIndex));
  45.                 beginIndex = endIndex+1;
  46.         }
  47.  
  48.         // LAST TOKEN CASE
  49.         //add last token
  50.         tokens.add(input.substring(beginIndex));
  51.  
  52.         return tokens;
  53.     }
  54.  
  55.     private static void println(List<String> tokens) {
  56.         for(String token:tokens){
  57.                 System.out.println(token);
  58.         }
  59.     }
  60.  
  61.  
  62.     public static void main(String args[]){
  63.         String test = "a=b " +
  64.                         "abc='123 456' " +
  65.                         "&=777 " +
  66.                         "#='uij yyy' " +
  67.                         "ABC='slk slk'              " +
  68.                         "123sdkljhSDFjflsakd@*#&=456sldSLKD)#(";
  69.         List<String> tokens = RegexTest.tokenize(test, p);
  70.         println(tokens);
  71.     }
  72. }
  73.        
  74. line.split(" (?=[a-z+]=)")
  75.        
  76. a=b
  77. c='123 456'
  78. d=777
  79. e='uij yyy'
  80.        
  81. String s = "Ta=b c='123 456' d=777 e='uij yyy'";
  82. StreamTokenizer st = new StreamTokenizer(new StringReader(s));
  83. st.ordinaryChars('0', '9');
  84. st.wordChars('0', '9');
  85. while (st.nextToken() != StreamTokenizer.TT_EOF) {
  86.     switch (st.ttype) {
  87.     case StreamTokenizer.TT_NUMBER:
  88.         System.out.println(st.nval);
  89.         break;
  90.     case StreamTokenizer.TT_WORD:
  91.         System.out.println(st.sval);
  92.         break;
  93.     case '=':
  94.         System.out.println("=");
  95.         break;
  96.     default:
  97.         System.out.println(st.sval);
  98.     }
  99. }
  100.        
  101. Ta
  102. =
  103. b
  104. c
  105. =
  106. 123 456
  107. d
  108. =
  109. 777
  110. e
  111. =
  112. uij yyy
  113.        
  114. String line = "a=b c='123 456' d=777 e='uij yyy'";
  115. Matcher m = Pattern.compile("('[^']*?'|\S)+").matcher(line);
  116. while (m.find()) {
  117.   System.out.println(m.group()); // or whatever you want to do
  118. }
  119.        
  120. public static void main(String[] args) {
  121. String token;
  122. String value="";
  123. HashMap<String, String> attributes = new HashMap<String, String>();
  124. String line = "a=b c='123  456' d=777 e='uij yyy'";
  125. StringTokenizer tokenizer = new StringTokenizer(line," ");
  126. while(tokenizer.hasMoreTokens()){
  127.         token = tokenizer.nextToken();
  128.     value = token.contains("'") ? value + " " + token : token ;
  129.     if(!value.contains("'") || value.endsWith("'")) {
  130.            //Split the strings and get variables into hashmap
  131.            attributes.put(value.split("=")[0].trim(),value.split("=")[1]);
  132.            value ="";
  133.     }
  134. }
  135.     System.out.println(attributes);
  136. }
  137.        
  138. java.util.StringTokenizer tokenizer = new java.util.StringTokenizer(line, " ");
  139. while (tokenizer.hasMoreTokens()) {
  140.     String token = tokenizer.nextToken();
  141.     int index = token.indexOf('=');
  142.     String key = token.substring(0, index);
  143.     String value = token.substring(index + 1);
  144. }