1

我需要从日志中过滤查询,它需要查找换行符,我一直在使用正则表达式过滤新行

这是我要过滤的日志示例

      139 Connect   root@localhost 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
4

1 回答 1

0

您可以使用正则表达式

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

它将检查Query当前位置的左侧是否立即存在,然后将消耗(将放入匹配值)任何 0+ 字符(包括换行符)。

于 2017-04-06T12:52:07.707 回答