2017-04-05 45 views
1

我需要從一個日誌過濾查詢,它需要查找新行,我已經陷在正則表達式過濾新線正則表達式過濾換行符搜索

這裏就是我想過濾

日誌的例子
 139 Connect [email protected] as anonymous on 
     139 Query SET CHARACTER SET 'utf8mb4' 
     139 Query SET collation_connection = 'utf8mb4_unicode_ci' 
     139 Query SELECT CURRENT_USER() 
     139 Query FLUSH PRIVILEGES 
     139 Query SELECT * FROM `mysql`.`db` LIMIT 1 
     139 Query DELETE FROM `mysql`.`db` WHERE `host` = "" AND `Db` = "" AND `User` = "" LIMIT 1 
     139 Query INSERT INTO `mysql`.`db`(`host`, `Db`, `User`) VALUES("pma_test_host", "mysql", "pma_test_user") 
     139 Query DELETE FROM `mysql`.`db` WHERE host = "pma_test_host" AND Db = "mysql" AND User = "pma_test_user" LIMIT 1 
     139 Query UPDATE `mysql`.`db` SET `host` = "" WHERE `host` = "" AND `Db` = "" AND `User` = "" LIMIT 1 

僅在行日誌文本我用正則表達式這樣

(?=Query).* 

但搜索結果

Query SET CHARACTER SET 'utf8mb4' 

因爲我使用「。」字符。在正則表達式中,這意味着不在新行中進行搜索。我試圖用幾種方式放置「\ n」,但它不起作用。我需要一個建議,獲取結果是這樣的:

Query SET CHARACTER SET 'utf8mb4' 
      139 Query SET collation_connection = 'utf8mb4_unicode_ci' 
      139 Query SELECT CURRENT_USER() 
      139 Query FLUSH PRIVILEGES 
      139 Query SELECT * FROM `mysql`.`db` LIMIT 1 
      139 Query DELETE FROM `mysql`.`db` WHERE `host` = "" AND `Db` = "" AND `User` = "" LIMIT 1 
      139 Query INSERT INTO `mysql`.`db`(`host`, `Db`, `User`) VALUES("pma_test_host", "mysql", "pma_test_user") 
      139 Query DELETE FROM `mysql`.`db` WHERE host = "pma_test_host" AND Db = "mysql" AND User = "pma_test_user" LIMIT 1 
      139 Query UPDATE `mysql`.`db` SET `host` = "" WHERE `host` = "" AND `Db` = "" AND `User` = "" LIMIT 1 
+0

如果您不讓我們知道您使用正則表達式的位置,則無法幫助您。或許,'(?s)(?= Query)。*'會起作用。或'(?=查詢)[\ s \ S] *' –

+0

我在java中使用正則表達式thankyou我會試試 – Risa

+0

然後'(?s)(?= Query)。*'會工作。 –

回答

0

您可以使用正則表達式像

(?=Query)[\s\S]* 

它會檢查是否馬上有Query到當前位置的左邊,然後將消耗(將放入匹配值)任何0+字符(包括換行符)。