正则表达式过滤新行搜索

时间:2017-04-05 18:56:15

标签: sql regex general-log

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

这是我要过滤的日志示例

      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

1 个答案:

答案 0 :(得分:0)

您可以使用像

这样的正则表达式
(?=Query)[\s\S]*

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