2014-12-06 180 views
0

我試圖用正則表達式如何使用正則表達式分割2個字符串?

例如

String original1 = "Calpol Plus 100MG"; 

一個字符串分割成兩個字符串上面的字符串應該分成

String string1 = "Calpol Plus";String string2 = "100MG";

我嘗試使用.split(" ")方法,但它只適用於原始字符串是"Calpol 100MG"

由於我是新來的正則表達式我搜索了幾個正則表達式,並提出了正則表達式爲"[^0-9MG]" 但它仍然不喜歡"Syrup 10ML"

在一根繩子上工作,我想用這將在兩個工作一般的正則表達式字符串的類型。

+0

請澄清分裂標準是什麼? – 2014-12-06 07:49:03

回答

1

只需按照<number>MG字符串或<number>ML字符串之前的一個或多個空格字符分割輸入即可。

string.split("\\s+(?=\\d+M[LG])"); // Use this regex "\\s+(?=\\d+(?:\\.\\d+)?M[LG])" if the there is a possibility of floating point numbers. 

實施例:

String original1 = "Calpol Plus 100MG"; 
String strs[] = original1.split("\\s+(?=\\d+M[LG])"); 
for (int i=0; i<strs.length; i++) { 
    System.out.println(strs[i]); 
} 

要分配的結果給一個變量。

String original1 = "Calpol Plus 100MG"; 
String strs[] = original1.split("\\s+(?=\\d+M[LG])"); 
String string1 = strs[0]; 
String string2 = strs[1]; 
System.out.println(string1); 
System.out.println(string2); 

輸出:

Calpol Plus 
100MG 

代碼2:

String original1 = "Syrup 10ML"; 
String strs[] = original1.split("\\s+(?=\\d+M[LG])"); 
String string1 = strs[0]; 
String string2 = strs[1]; 
System.out.println(string1); 
System.out.println(string2); 

輸出:

Syrup 
10ML 

說明:

  • \s+匹配一個或多個空格字符。
  • (?=\\d+M[LG])正預測先行斷言比賽必須遵循的一個或多個數字\d+且然後進行MGML

ReGex DEMO

0

試着這麼做:

String original1 = "Calpol Plus 100MG"; 
Pattern p = Pattern.compile("[A-Za-z ]+|[0-9]*.*"); 
Matcher m = p.matcher(original1); 
while (m.find()) { 
     System.out.println(m.group()); 
} 
0

我目前有兩種解決方案:

  • 您可以創建相匹配的整個字符串和使用羣體中提取所需的信息
  • 可以使用前瞻 - 斷言,以確保您拆分的數字

前面的圖案,其最適合你的解決方案取決於你擁有的各種輸入。如果您使用羣組,您將始終找到最後的金額部分。如果您使用拆分,則可能能夠提取更復雜的數量組,例如「2茶匙」(第一種解決方案需要擴展[A-Za-z]類別以包括-,例如使用[-A-Za-z]代替)或「2.5L」(使用第一種解決方案時,需要擴展[0-9]類以包含.,例如使用[0-9.]代替)更容易。

來源:究竟何時應該發生:

import java.util.Arrays; 
import java.util.regex.Matcher; 
import java.util.regex.Pattern; 

/** 
* Created for http://stackoverflow.com/q/27329519/1266906 
*/ 
public class RecipeSplitter { 

    /** 
    * {@code ^} the Pattern has to be applied from the start of the String on 
    * {@code (.*)} match any characters into Group 1 
    * {@code \\s+} followed by at least one whitespace 
    * {@code ([0-9]+\s*[A-Za-z]+)} followed by Group 2 which is made up by at least one digit, optional whitespace and 
    *        at least one character 
    * {@code $} the Pattern has to be applied so that at the End of the Pattern the End of the String is reached 
    */ 
    public static final Pattern INGREDIENT_PATTERN     = Pattern.compile("^(.*)\\s+([0-9]+\\s*[A-Za-z]+)$"); 
    /** 
    * {@code \\s+} at least one whitespace 
    * {@code (?=[0-9])} next is a digit (?= will ensure it is there but doesn't include it into the match so we don't 
    *     remove it 
    */ 
    public static final Pattern WHITESPACE_FOLLOWED_BY_DIGIT_PATTERN = Pattern.compile("\\s+(?=[0-9])"); 

    public static void matchWholeString(String input) { 
     Matcher matcher = INGREDIENT_PATTERN.matcher(input); 
     if (matcher.find()) { 
      System.out.println(
        "\"" + input + "\" was split into \"" + matcher.group(1) + "\" and \"" + matcher.group(2) + "\""); 
     } else { 
      System.out.println("\"" + input + "\" was not of the expected format"); 
     } 
    } 

    public static void splitBeforeNumber(String input) { 
     String[] strings = WHITESPACE_FOLLOWED_BY_DIGIT_PATTERN.split(input); 
     System.out.println("\"" + input + "\" was split into " + Arrays.toString(strings)); 
    } 

    public static void main(String[] args) { 
     matchWholeString("Calpol Plus 100MG"); 
     // "Calpol Plus 100MG" was split into "Calpol Plus" and "100MG" 
     matchWholeString("Syrup 10ML"); 
     // "Syrup 10ML" was split into "Syrup" and "10ML" 
     splitBeforeNumber("Calpol Plus 100MG"); 
     // "Calpol Plus 100MG" was split into [Calpol Plus, 100MG] 
     splitBeforeNumber("Syrup 10ML"); 
     // "Syrup 10ML" was split into [Syrup, 10ML] 
    } 
} 
相關問題