簡體   English   中英

正則表達式匹配新行

[英]Regex match for new lines

我正在嘗試使用Java在我的代碼中獲取日志語句。 我正在使用此正則表達式:

Log\.[dD].*\);

測試數據:

Log.d(TAG, "Construct product info");

test test test test 

Log.d(TAG, "Loading Product Id: %d, Version: %s",
    productInfo.getProductId(),
    productInfo.getProductVersion());

test test test test 

Log.d(TAG, "Loading Product Id: " +
    ProductId + "Version:" +
    Version);

test test test test

由於某種原因,正則表達式只選擇第一行

Log.d(TAG, "Construct product info");

正確的輸出應為:

Log.d(TAG, "Construct product info");

Log.d(TAG, "Loading Product Id: %d, Version: %s",
    productInfo.getProductId(),
    productInfo.getProductVersion());

Log.d(TAG, "Loading Product Id: " +
    ProductId + "Version:" +
    Version);

https://regex101.com/r/OCl9dy/3

如果我沒記錯的話,正則表達式應該返回所有以)結尾的Log。[dD]語句;

請讓我知道為什么它沒有收到別人以及如何解決此問題。

謝謝

這個正則表達式應該工作:

Log\.[dD][\s\S]*?\);

您犯的兩個錯誤是:

  • . 與行尾不匹配。 您應該使用類似於[\\s\\S] ,該名稱應與所有內容匹配。
  • 您需要懶惰地匹配,在看到的第一個( )處停止匹配。 那么寫*? 代替*

請注意,正則表達式不適用於Log.d(TAG, "Construct (product); info"); 因為第一個); 不是方法調用的結尾。 為了匹配這些,我建議您編寫/使用解析器。

演示版

暫無
暫無

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

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