当没有使用java的分隔符时,使用正则表达式拆分字符串

时间:2013-05-28 15:21:29

标签: java

我有一个字符串,例如:DIGITAL SPORTS$8.95HD AO$9.95UCC REC$1.28RENTAL FEE$7.00LOCAL FRANCHISE$4.67 现在我想分割字符串并创建一个地图

DIGITAL SPORTS $8.95 
HD AO $9.95
UCC REC $1.28
RENTAL FEE $7.00
LOCAL FRANCHISE $4.67

我写了一个正则表达式来分割字符串。请在下面的代码中找到

private static String ledgerString = "DIGITAL SPORTS$8.95HD AO$9.95UCC REC$1.28RENTAL FEE$7.00LOCAL FRANCHISE$4.67";
private static Pattern pattern1 = Pattern.compile("([[a-zA-Z ]*\\$[0-9]*.[0-9][0-9]]*)");
private static Matcher matcher = null;
public static void main(String[] args) {
    // TODO Auto-generated method stub
    matcher = pattern1.matcher(ledgerString.trim());

    if (matcher.find()) {

        System.out.println(matcher.group(1));
    } 

}
你可以请一些人帮助我如何从上面的字符串中提取数据

6 个答案:

答案 0 :(得分:2)

第1组中的模式位于字符类[...]中,现在可能是您尝试执行的操作。也许将你的模式改为

Pattern.compile("([a-zA-Z ]*)(\\$[0-9]*.[0-9][0-9]*)");

并像这样使用

while (matcher.find()) {
    System.out.println(matcher.group(1)+" "+matcher.group(2));
}

此外,自Java7起,您可以为组(?<name>...)命名,因此这也是可能的

Pattern.compile("(?<name>[a-zA-Z ]*)(?<price>\\$[0-9]*.[0-9][0-9]*)");

while (matcher.find()) {
    System.out.println(matcher.group("name")+" "+matcher.group("price"));
}

输出

DIGITAL SPORTS $8.95
HD AO $9.95
UCC REC $1.28
RENTAL FEE $7.00
LOCAL FRANCHISE $4.67

答案 1 :(得分:1)

试试这个:

The Regex:    (?:(.+?)(\$\d*(?:\.\d+)?))

String regex = "(?:(.+?)(\\$\\d*(?:\\.\\d+)?))";

Demo

答案 2 :(得分:1)

private static String ledgerString = "DIGITAL SPORTS$8.95HD AO$9.95UCC REC$1.28RENTAL FEE$7.00LOCAL FRANCHISE$4.67";
private static Pattern pattern1 = Pattern.compile("([a-zA-Z ]+)(\\$[0-9]*\\.[0-9][0-9])");
private static Matcher matcher = null;
public static void main(String[] args) {
    // TODO Auto-generated method stub
    matcher = pattern1.matcher(ledgerString.trim());

    while (matcher.find()) {

        System.out.println(matcher.group(1) + " " + matcher.group(2));
    } 

}

答案 3 :(得分:0)

也许你可以用“,$”(逗号美元)符号替换所有出现的“$”符号。之后,您可以使用“,”(逗号)将其拆分。做类似的事情:

ledgerString = ledgerString.replaceAll("$", ",$");
String[] tokens = ledgerString.split(",");

答案 4 :(得分:0)

您要使用的正则表达式匹配您感兴趣的每个字符串。因此您要使用

Pattern.compile(&#34;([A-ZA-Z] \ $ [0-9] [0-9] [0-9])&#34;。) ;

因为这标识了每一行&#39;您对此感兴趣。然后您可以在每一行上使用拆分(&#34; $&#34;)将描述与价格分开。

答案 5 :(得分:0)

这是另一种方法:

 String mainString = "DIGITAL SPORTS$8.95HD AO$9.95UCC REC$1.28RENTAL FEE$7.00LOCAL FRANCHISE$4.67";

     String[] splittedArray = mainString.split("[0-9][A-Z]");
    int currentLength = 0;
    for(int i =0; i < splittedArray.length; i++) {
       String splitedString;

        if(i == 0) {
            char endChar = mainString.charAt(splittedArray[i].length());
            splitedString =  splittedArray[i] + endChar;
            currentLength += splittedArray[i].length();
        }
        else if(i == splittedArray.length -1){
           char beginChar = mainString.charAt(currentLength + 1);
            splitedString = beginChar + splittedArray[i];
        }
        else {
           char beginChar = mainString.charAt(currentLength + 1);
           char endChar = mainString.charAt(currentLength+splittedArray[i].length()+2);
           splitedString = beginChar + splittedArray[i] + endChar;
            currentLength += splittedArray[i].length()+2;
        }
         System.out.println(splitedString);
    }