Home > Blockchain >  Regex for matching content between two string in multiple lines
Regex for matching content between two string in multiple lines

Time:07-11

I'm trying to get content between "POST /api/user/register" and "client : 2" in log text. Use this regex:

.*POST  \/api\/user\/register(?!client : 1)[\s\S]*?client : 2

Log text:

2022-06-30 15:54:31.456  INFO 22593 --- [http-nio-8080-exec-10]    : ###### Start ######
2022-06-30 15:54:31.456  INFO 22593 --- [http-nio-8080-exec-10]    : POST  /api/user/register
2022-06-30 15:54:31.457  INFO 22593 --- [http-nio-8080-exec-10]    : key1 : value1
2022-06-30 15:54:31.457  INFO 22593 --- [http-nio-8080-exec-10]    : key2 : value2
2022-06-30 15:54:31.457  INFO 22593 --- [http-nio-8080-exec-10]    : client : 2
2022-06-30 15:54:31.456  INFO 22593 --- [http-nio-8080-exec-10]    : ###### End ######
2022-06-30 15:54:31.456  INFO 22593 --- [http-nio-8080-exec-10]    : ###### Start ######
2022-06-30 15:54:31.456  INFO 22593 --- [http-nio-8080-exec-10]    : POST  /api/user/register
2022-06-30 15:54:31.457  INFO 22593 --- [http-nio-8080-exec-10]    : key1 : value1
2022-06-30 15:54:31.457  INFO 22593 --- [http-nio-8080-exec-10]    : key2 : value2
2022-06-30 15:54:31.457  INFO 22593 --- [http-nio-8080-exec-10]    : client : 1
2022-06-30 15:54:31.456  INFO 22593 --- [http-nio-8080-exec-10]    : ###### End ######
2022-06-30 15:54:31.456  INFO 22593 --- [http-nio-8080-exec-10]    : ###### Start ######
2022-06-30 15:54:31.456  INFO 22593 --- [http-nio-8080-exec-10]    : POST  /api/user/register
2022-06-30 15:54:31.457  INFO 22593 --- [http-nio-8080-exec-10]    : key1 : value1
2022-06-30 15:54:31.457  INFO 22593 --- [http-nio-8080-exec-10]    : key2 : value2
2022-06-30 15:54:31.457  INFO 22593 --- [http-nio-8080-exec-10]    : client : 2
2022-06-30 15:54:31.456  INFO 22593 --- [http-nio-8080-exec-10]    : ###### End ######
2022-06-30 15:54:31.456  INFO 22593 --- [http-nio-8080-exec-10]    : ###### Start ######
2022-06-30 15:54:31.456  INFO 22593 --- [http-nio-8080-exec-10]    : POST  /api/user/register
2022-06-30 15:54:31.457  INFO 22593 --- [http-nio-8080-exec-10]    : key1 : value1
2022-06-30 15:54:31.457  INFO 22593 --- [http-nio-8080-exec-10]    : key2 : value2
2022-06-30 15:54:31.457  INFO 22593 --- [http-nio-8080-exec-10]    : client : 1
2022-06-30 15:54:31.456  INFO 22593 --- [http-nio-8080-exec-10]    : ###### End ######
2022-06-30 15:54:31.456  INFO 22593 --- [http-nio-8080-exec-10]    : ###### Start ######
2022-06-30 15:54:31.456  INFO 22593 --- [http-nio-8080-exec-10]    : POST  /api/user/register
2022-06-30 15:54:31.457  INFO 22593 --- [http-nio-8080-exec-10]    : key1 : value1
2022-06-30 15:54:31.457  INFO 22593 --- [http-nio-8080-exec-10]    : key2 : value2
2022-06-30 15:54:31.457  INFO 22593 --- [http-nio-8080-exec-10]    : client : 2
2022-06-30 15:54:31.456  INFO 22593 --- [http-nio-8080-exec-10]    : ###### End ######

The match reuslt is:

2022-06-30 15:54:31.456  INFO 22593 --- [http-nio-8080-exec-10]    : POST  /api/user/register
2022-06-30 15:54:31.457  INFO 22593 --- [http-nio-8080-exec-10]    : key1 : value1
2022-06-30 15:54:31.457  INFO 22593 --- [http-nio-8080-exec-10]    : key2 : value2
2022-06-30 15:54:31.457  INFO 22593 --- [http-nio-8080-exec-10]    : client : 2
2022-06-30 15:54:31.456  INFO 22593 --- [http-nio-8080-exec-10]    : POST  /api/user/register
2022-06-30 15:54:31.457  INFO 22593 --- [http-nio-8080-exec-10]    : key1 : value1
2022-06-30 15:54:31.457  INFO 22593 --- [http-nio-8080-exec-10]    : key2 : value2
2022-06-30 15:54:31.457  INFO 22593 --- [http-nio-8080-exec-10]    : client : 1
2022-06-30 15:54:31.456  INFO 22593 --- [http-nio-8080-exec-10]    : ###### End ######
2022-06-30 15:54:31.456  INFO 22593 --- [http-nio-8080-exec-10]    : ###### Start ######
2022-06-30 15:54:31.456  INFO 22593 --- [http-nio-8080-exec-10]    : POST  /api/user/register
2022-06-30 15:54:31.457  INFO 22593 --- [http-nio-8080-exec-10]    : key1 : value1
2022-06-30 15:54:31.457  INFO 22593 --- [http-nio-8080-exec-10]    : key2 : value2
2022-06-30 15:54:31.457  INFO 22593 --- [http-nio-8080-exec-10]    : client : 2
2022-06-30 15:54:31.456  INFO 22593 --- [http-nio-8080-exec-10]    : POST  /api/user/register
2022-06-30 15:54:31.457  INFO 22593 --- [http-nio-8080-exec-10]    : key1 : value1
2022-06-30 15:54:31.457  INFO 22593 --- [http-nio-8080-exec-10]    : key2 : value2
2022-06-30 15:54:31.457  INFO 22593 --- [http-nio-8080-exec-10]    : client : 1
2022-06-30 15:54:31.456  INFO 22593 --- [http-nio-8080-exec-10]    : ###### End ######
2022-06-30 15:54:31.456  INFO 22593 --- [http-nio-8080-exec-10]    : ###### Start ######
2022-06-30 15:54:31.456  INFO 22593 --- [http-nio-8080-exec-10]    : POST  /api/user/register
2022-06-30 15:54:31.457  INFO 22593 --- [http-nio-8080-exec-10]    : key1 : value1
2022-06-30 15:54:31.457  INFO 22593 --- [http-nio-8080-exec-10]    : key2 : value2
2022-06-30 15:54:31.457  INFO 22593 --- [http-nio-8080-exec-10]    : client : 2

The result include between "POST /api/user/register" and "client : 1",How to exculde that? The result I want is:

2022-06-30 15:54:31.456  INFO 22593 --- [http-nio-8080-exec-10]    : POST  /api/user/register
2022-06-30 15:54:31.457  INFO 22593 --- [http-nio-8080-exec-10]    : key1 : value1
2022-06-30 15:54:31.457  INFO 22593 --- [http-nio-8080-exec-10]    : key2 : value2
2022-06-30 15:54:31.457  INFO 22593 --- [http-nio-8080-exec-10]    : client : 2

2022-06-30 15:54:31.456  INFO 22593 --- [http-nio-8080-exec-10]    : POST  /api/user/register
2022-06-30 15:54:31.457  INFO 22593 --- [http-nio-8080-exec-10]    : key1 : value1
2022-06-30 15:54:31.457  INFO 22593 --- [http-nio-8080-exec-10]    : key2 : value2
2022-06-30 15:54:31.457  INFO 22593 --- [http-nio-8080-exec-10]    : client : 2

2022-06-30 15:54:31.456  INFO 22593 --- [http-nio-8080-exec-10]    : POST  /api/user/register
2022-06-30 15:54:31.457  INFO 22593 --- [http-nio-8080-exec-10]    : key1 : value1
2022-06-30 15:54:31.457  INFO 22593 --- [http-nio-8080-exec-10]    : key2 : value2
2022-06-30 15:54:31.457  INFO 22593 --- [http-nio-8080-exec-10]    : client : 2

CodePudding user response:

You are missing one open and close parenthesis to make them in one capturing group:

.*POST  \/api\/user\/register((?!client : 1)[\s\S])*?client : 2
  • Related