簡體   English   中英

正則表達式刪除井號和雙逗號java csv

[英]Regex to remove pound sign and double commas java csv

我正在處理一個CSV文件,該文件在某些​​地方具有多個逗號和井號。 我的問題是關於如何刪除多個逗號和井號,同時在字段之間保留單個逗號。

我要做的這項工作的一部分是,僅使用java,而不使用外部庫來對csv文件進行排序,並按價格對數組進行排序。 我要輸入一個數字作為輸入參數,並返回該行數,按價格排序。

我目前擁有大約1000行數據,如下所示:

18,5 Ramsey Lane,See,Amerighi,samerighih@trellian.com,,£307018.48,

我需要刪除雙逗號和英鎊符號,但對我而言,一生都無法使其正常運行。

這是我用於正則表達式的行。

         String currentLine = line.replaceAll("[,{2}|£]", "");

輸出的行如下所示:

100086 Norway Maple WayMadelleGeorgeotmgeorgeotrr@hao13.com417175.60

更大的代碼塊看起來像這樣,絕不是幾乎完成了:

  public String[] getTopProperties(int n){
    String[] properties = new String[n];
    String file = "data.csv";
    String line = "";
    String splitBy = ",";

    try (BufferedReader br = new BufferedReader(new FileReader(file))) {

        while ((line = br.readLine()) != null) {

          String currentLine = line.replaceAll("[,{2}|£]", "");

          System.out.println("Current line is: " + currentLine);
            String[] user = currentLine.split(splitBy);
           }
      } catch (IOException e) {
        e.printStackTrace();
      }

    return properties;
}

問題是,它現在刪除了所有逗號,而價格和雙逗號的位置現在已經連接起來。 可以使用一些幫助來找到一些正則表達式,以使每個字段之間保持單個逗號,並刪除井號。

在您的正則表達式中.replaceAll("[,{2}|£]", ""); 方括號創建一個字符類 ,因此,這意味着“替換的任何字符,{2}| ,或£ ”。

您真正想要的是用單個逗號替換序列 ,,£ ,即.replaceAll(",,£", ",")

在Java 腳本中,這將是...

 var line="18,5 Ramsey Lane,See,Amerighi,samerighih@trellian.com,,£307018.48,"; console.log(' original line: ' + line); console.log('replacement line: ' + line.replace(/,,£/, ",")); 


更新

將其作為獨立的測試程序轉換為Java以證明它確實有效,我得到以下信息:

public class so50419207
{
    public static void main(String... args)
    {
        String input = "18,5 Ramsey Lane,See,Amerighi,samerighih@trellian.com,,£307018.48,";
        String replaced = input.replace(",,£", ",");
        System.out.println("original string: " + input);
        System.out.println("replaced string: " + replaced);
    }
}

運行這個...

$ javac so50419207.java ; java so50419207
original string: 18,5 Ramsey Lane,See,Amerighi,samerighih@trellian.com,,£307018.48,
replaced string: 18,5 Ramsey Lane,See,Amerighi,samerighih@trellian.com,307018.48,

您可以通過將CSV文件解析為2D數組並忽略由雙逗號引起的空列來簡化此過程。 然后解析貨幣列很容易:只需忽略第一個字符即可。

嘗試過正則表達式(,,)(£)? 並在ideone中對其進行了測試:請找到以下代碼:

import java.util.*;
import java.lang.*;
import java.io.*;
        import java.util.regex.Matcher;
import java.util.regex.Pattern;


/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
    public static void main (String[] args) throws java.lang.Exception
    {

final String regex = "(,,)(£)?";
final String string = "18,,5 Ramsey Lane,,See,Amerighi,,samerighih@trellian.com,,£307018.48,,\n"
     + "18,,5 Ramsey Lane,,See,Amerighi,,samerighih@trellian.com,,£307018.48,,\n"
     + "18,5 Ramsey Lane,,See,Amerighi,,samerighih@trellian.com,,£307018.48,,\n"
     + "18,,5 Ramsey Lane,,See,Amerighi,,samerighih@trellian.com,,£307018.48,,";
final String subst = ",";

final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE);
final Matcher matcher = pattern.matcher(string);

// The substituted value will be contained in the result variable
final String result = matcher.replaceAll(subst);

System.out.println("Substitution result: " + result);
    }
}

輸出:

Substitution result: 18,5 Ramsey Lane,See,Amerighi,samerighih@trellian.com,307018.48,
18,5 Ramsey Lane,See,Amerighi,samerighih@trellian.com,307018.48,
18,5 Ramsey Lane,See,Amerighi,samerighih@trellian.com,307018.48,
18,5 Ramsey Lane,See,Amerighi,samerighih@trellian.com,307018.48,

暫無
暫無

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

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