1

I have a string eg : DIGITAL SPORTS$8.95HD AO$9.95UCC REC$1.28RENTAL FEE$7.00LOCAL FRANCHISE$4.67 Now I want to split the string and create a map as

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

I wrote a regular expression to split the string. Please find below piece of code

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));
    } 

}

could you please some one help me how to extract the data from the above string

4

6 回答 6

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
于 2013-05-28T15:32:00.573 回答
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));
    } 

}
于 2013-05-28T15:34:34.590 回答
1

尝试这个:

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

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

演示

于 2013-05-28T15:30:44.413 回答
0

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

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

因为这标识了您感兴趣的每个“行”。然后您可以在每行上使用 split("$") 将描述与价格分开。

于 2013-05-28T15:35:34.133 回答
0

也许您可以用“,$”(逗号美元)符号替换所有出现的“$”符号。之后,您可以使用“,”(逗号)对其进行拆分。执行以下操作:

ledgerString = ledgerString.replaceAll("$", ",$");
String[] tokens = ledgerString.split(",");
于 2013-05-28T15:30:29.930 回答
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);
    }
于 2013-05-28T15:46:52.680 回答