2014-12-06 132 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] 
    } 
} 
相关问题