簡體   English   中英

Java:從字符串中提取數字

[英]Java : Extract numbers from a string

我試圖使用正則表達式從字符串中提取數據。

我的數據如下:

 12 170 0.11918
170  12 0.11918
 12 182 0.06361
182  12 0.06361
 12 198 0.05807
198  12 0.05807
 12 242 0.08457
242  12 0.08457
 11  30 0.08689
 30  11 0.08689

這里的問題是兩個數字之間的空白數不同。

總之,我想從每行中提取兩個Integer和一個Double。 因此,我嘗試使用正則表達式。

  Pattern p = Pattern.compile("(([0-9]+.[0-9]*)|([0-9]*.[0-9]+)|([0-9]+))");
  Matcher m = p.matcher("  6    7781     0.01684000");
  while (m.find()) {
     System.out.println(m.group(0));  
  }

我現在的正則表達式不起作用。 有沒有人對適當的正則表達式有所幫助,因此我可以為我處理數據或其他任何幫助?

為什么不閱讀每一行並執行line.trim().split("\\\\s+") 如果您的項目已經使用了番石榴,則也可以使用Splitter

我建議使用Scanner

Scanner scanner = new Scanner(line);
scanner.useDelimiter(" ");
int int1 = scanner.nextInt()
int int2 = scanner.nextInt()
double double1 = scanner.nextDouble()

檢查http://txt2re.com/index-java.php3?s=%2012%20170%200.11918&11&5&12&4&13&1

您可能對下面的int1,int2和float1感興趣

 public static void main(String[] args)
  {
    String txt=" 12 170 0.11918";

    String re1="(\\s+)";    // White Space 1
    String re2="(\\d+)";    // Integer Number 1
    String re3="(\\s+)";    // White Space 2
    String re4="(\\d+)";    // Integer Number 2
    String re5="(\\s+)";    // White Space 3
    String re6="([+-]?\\d*\\.\\d+)(?![-+0-9\\.])";  // Float 1

    Pattern p = Pattern.compile(re1+re2+re3+re4+re5+re6,Pattern.CASE_INSENSITIVE | Pattern.DOTALL);
    Matcher m = p.matcher(txt);
    if (m.find())
    {
        String ws1=m.group(1);
        String int1=m.group(2);
        String ws2=m.group(3);
        String int2=m.group(4);
        String ws3=m.group(5);
        String float1=m.group(6);
        System.out.print("("+ws1.toString()+")"+"("+int1.toString()+")"+"("+ws2.toString()+")"+"("+int2.toString()+")"+"("+ws3.toString()+")"+"("+float1.toString()+")"+"\n");
    }
  }

嘗試這個:

([\\d.]+) -這將獲取所有僅包含數字或句點(。)的字符串。

編輯:

我看到您想將三個小組排成一行。 相反,這將有助於忽略空白,並抓住三組數字。 前導^和尾隨$確保您只匹配一行。

^\\s*?([\\d.]+)\\s*([\\d.]+)\\s*?([\\d.]+)\\s*?$

這樣的事情(根據需要修復浮動部分)-

 # raw:  (?m)^\h*(\d+)\h+(\d+)\h+(\d*\.\d+)
 # quoted: "(?m)^\\h*(\\d+)\\h+(\\d+)\\h+(\\d*\\.\\d+)"

 (?m)             # Multi-line modifier
 ^                # BOL
 \h*              # optional, horizontal whitespace
 ( \d+ )          # (1), int
 \h+              # required, horizontal whitespace
 ( \d+ )          # (2), int
 \h+              # required, horizontal whitespace
 ( \d* \. \d+ )   # (3), float
String s = " 12 170 0.11918\n" + "170  12 0.11918 \n"
            + " 12 182 0.06361\n" + "182  12 0.06361 \n"
            + " 12 198 0.05807\n" + "198  12 0.05807 \n"
            + " 12 242 0.08457\n" + "242  12 0.08457 \n"
            + " 11  30 0.08689\n" + " 30  11 0.08689 \n";

    String[] lines = s.split("\\n");

    for( String line : lines ) {
        Scanner scan = new Scanner(line);
        scan.useDelimiter("\\s+");
        scan.useLocale(Locale.ENGLISH);
        System.out.println(scan.nextInt());
        System.out.println(scan.nextInt());
        System.out.println(scan.nextDouble());
    }

我將使用掃描儀解決此問題。

暫無
暫無

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

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