2012-09-28 178 views
1

我想從正則表達式的java中使用string.split中的母串使用特殊字符串。使用單詞作爲分隔符分割字符串

string str = "name ching chang field computer engineering grade 9.98"; 
string[] splits = str.split("regex"); 

我想用單詞name,field和grade作爲分隔符。現在應該如何正則表達式,以便我的字符串數組(分割)其中有以下項目:

splits[0] = "ching chang" 
splits[1] = "computer engineering" 
splits[2] = "9.98" 

非常感謝您的時間!

+1

閱讀正則表達式。 – keyser

+0

年級不是分隔符,它不會按照你想要的方式分割。 –

+0

使用'split(「name | field | grade」)' – Nishant

回答

9
String str = "name ching chang field computer engineering grade 9.98"; 
String[] splits = str.split("name |field |grade "); 

//test with 
for(String s : splits) 
    System.out.println(s); 
1
String str = "name ching chang field computer engineering grade 9.98"; 
    String[] splits = str.split("name|field|grade"); 
    System.out.println(Arrays.asList(splits)); 
0
import java.util.Arrays; 
import java.util.HashMap; 
import java.util.Map; 
import java.util.Map.Entry; 

/** 
* Fieldsplitter with configurable fields 
*/ 
public class FieldSplitter { 

    private static Logger LOG = new Logger(); 

    /** 
    * Test method 
    * 
    * @param args 
    *   not used 
    */ 
    public static final void main(String[] args) { 

     String[] fields = new String[] { "name", "field", "grade" }; 
     // regular test case 
     printFieldValues(fields, 
       "name ching chang field computer engineering grade 9.98"); 
     // missing field 
     printFieldValues(fields, 
       "ching chang field computer engineering grade 9.98"); 
     // duplicate field 
     printFieldValues(fields, 
       "name ching chang field computer engineering name johnny bravo grade 9.98"); 
    } 

    /** 
    * Gets the values of the fields from the input. If a field is not found the 
    * value is null. If a field occurs multiple times the last value is used. 
    * 
    * @param fields 
    *   the known fields 
    * @param input 
    *   the input containing consecutive fields and their values 
    *   separated by whitespace 
    * @return a Map containing all known fields and their values 
    */ 
    public static Map<String, String> getFieldValues(String[] fields, 
      String input) { 
     Map<String, String> fieldValues = new HashMap<String, String>(); 
     for (String field : fields) { 
      fieldValues.put(field, null); 
     } 
     String[] words = input.split("\\s"); 
     LOG.debug("words:" + Arrays.toString(words)); 
     String field = null; 
     int start = 0; 
     for (String word : words) { 
      if (fieldValues.containsKey(word)) { 
       int end = input.indexOf(word, start); 
       if (end < 0) { 
        throw new RuntimeException(String.format(
          "Error: '%s' not found after position %s", word, 
          end)); 
       } 
       if (field != null) { 
        fieldValues.put(field, input.substring(start, end)); 
       } 
       field = word; 
       start = end + word.length(); 
      } 
     } 
     if (field != null) { 
      fieldValues.put(field, input.substring(start, input.length())); 
     } 
     return fieldValues; 
    } 

    private static void printFieldValues(String[] fields, String input) { 
     for (Entry<String, String> field : getFieldValues(fields, input) 
       .entrySet()) { 
      LOG.debug(String.format("%s=%s", field.getKey(), field.getValue())); 
     } 
    } 

    /** A simple logger */ 
    private static class Logger { 
     void debug(String message) { 
      System.out.println(message); 
     } 
    } 
} 

輸出:

words:[name, ching, chang, field, computer, engineering, grade, 9.98] 
grade= 9.98 
name= ching chang 
field= computer engineering 
words:[ching, chang, field, computer, engineering, grade, 9.98] 
grade= 9.98 
name=null 
field= computer engineering 
words:[name, ching, chang, field, computer, engineering, name, johnny, bravo, grade, 9.98] 
grade= 9.98 
name= johnny bravo 
field= computer engineering 

P.S.我剛剛發現了一個簡單而方便的技巧:在Eclipse中選擇您的代碼並按Tab鍵縮進它。然後將縮進的代碼複製並粘貼到Stackoverflow編輯字段中。這樣代碼將立即正確渲染。