簡體   English   中英

如何在Java中跳過行的一部分-不知道我在做什么

[英]How to skip a portion of a line in Java- Don't know what I am doing

我只需要在數字字符串中使用所有其他數字。 這就是xml文件內容的方式,但是我只想使用“第一個組”,然后使用其他每個組。 每個組中的第二個數字可以忽略。 最重要的數字是第一個數字,例如1,3,5和29您能幫上忙嗎? 每個組等於“ x”:“ x”,

<CatcList>{“1":"15","2":"15","3":"25","4":"25","5":"35","6":"35","29":"10","30":"10"}</CatcList> 

現在,我的腳本看起來像這樣,但我不是編寫它的人。

我只包括了需要的部分。 StartPage將是使用的變量。 如果您知道如何將1加到EndPage Integer,那也將非常有幫助。 謝謝!

 Util.StringList xs;
 line.parseLine(",", "", xs);
 for (Int i=0; i<xs.Size; i++) {
   Int qty = xs[i].right(xs[i].Length - xs[i].find(":")-1).toInt()-1;
   for (Int j=0; j<qty; j++) {
      Output_0.File.DocId = product;
      Output_0.File.ImagePath = Image;
      Output_0.File.ImagePath1 = Image;
  Output_0.File.StartPage = xs[i].left(xs[i].find(("-"))).toInt()-1;
  Output_0.File.EndPage = xs[i].mid(xs[i].find("-")+1, (xs[i].find(":") - xs[i].find("-")-1)).toInt()-0;
      Output_0.File.Quantity = qty.toString();
      Output_0.File.commit();

嘗試這個:

,?("(?<num>\d+)":"\d+"),?("\d+":"\d+")?

名為num的組將包含“ x”的第一部分的所有其他出現:“ x”

所以對於值:

"1":"14","2":"14","3":"14","4":"24","5":"33","6":"44","7":"55"

名為“ num”的組將包含1 3 5和7。

在這里查看示例

編輯:提取數字后,就可以使用所需的功能:

Pattern datePatt = Pattern.compile(",?(\"(?<num>\\d+)\":\"\\d+\"),?(\"\\d+\":\"\\d+\")?");
String dateStr = "\"1\":\"14\",\"2\":\"14\",\"3\":\"14\",\"4\":\"24\",\"5\":\"33\",\"6\":\"44\",\"7\":\"55\"";
Matcher m = datePatt.matcher(dateStr);

while (m.find()) {          
    System.out.printf("%s%n", m.group("num"));           
}

您可以將Pattern與循環和某些條件結合使用來提取此信息:

String string = "<CatcList>{\"1\":\"15\",\"2\":\"15\",\"3\":\"25\",\"4\":\"25\","
        + "\"5\":\"35\",\"6\":\"35\",\"29\":\"10\",\"30\":\"10\"}</CatcList> ";
String regex = "\"(\\d+)\":\"\\d+\"";

Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(string);
int index = 0;
while (matcher.find()) {
    if (index % 2 == 0) {//From your question you have to get alternative results 1 3 5 ...
        System.out.println(matcher.group(1) + " ---> " + matcher.group());
    }
    index++;
}

產出

1 ---> "1":"15"
3 ---> "3":"25"
5 ---> "5":"35"
29 ---> "29":"10"

正則表達式"(\\d+)":"\\d+"應該與我使用的"number":"number" (\\d+) "number":"number" (\\d+) "number":"number"任何組合"(\\d+)":"\\d+"匹配,因此我只能得到該組的信息。

該XML值看起來像一個JSON映射。 在沒有這些方法如何工作的細節的情況下,所有.right,.mid和.left代碼對我來說都很令人困惑。 這樣的事情看起來更清楚嗎:

// leaving out all the backslash escapes of the embedded quotes
String xmlElement = "{"1":"15","2":"15","3":"25","4":"25","5":"35","6":"35","29":"10","30":"10"}";
xmlElement = xmlElement.replaceAll("[}{]", "");
String[] mapEntryStrings = xmlElement.split(","); 
Map<Integer, String> oddStartPages = new HashMap<Integer, String>();
for (String entry : mapEntryStrings) {
    String[] keyAndValue = entry.split(":");
    int key = Integer.parseInt(keyAndValue[0]);
    if (key % 2 == 1) {// if odd 
        oddStartPages.put(key, keyAndValue[1]);
    }
}

然后,oddStartPages映射中的鍵集恰好是“第一個及所有其他組”要求中的第一個數字集。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM