简体   繁体   中英

Matching regex for optional values

I'm trying to extract some information from web server log and it's not very structured so I run into troubles, I'm trying to match :

Example 1 :

2011-11-29 11:30:23,685 DEBUG [my.fully.qualified.package.Service] Added Action Item: M= 2  Success

Example 2 :

2011-11-29 11:30:23,685 DEBUG [my.fully.qualified.package.Service] Added Action Item: M=10  Success

This regex works for example 1 :

(\d\d\d\d-\d\d-\d\d)\s[\d|:]+,\d+\s([A-Z]+)\s\[(.+)\]\s.+:\sM=\s(\d).+

Where first group is the date, second is log level, third is class name and third one is the value of M .

You may have noticed that in example 1, after M= there is a space before digit and in the other example there is not that's why this regex is not working.

I did try something like M=[\\s|d]+ but I get some more characters matched that I asked for, anyone have suggestion how to match both of these examples with one regex?

你想要M=\\s*(\\d+) ,它将在=之后立即允许零个或多个空格,但不能在数字之后的任何空格。

M=\s?(\d).+

=后允许可选的空格。

我相信你有一些拼写错误,应该是

M=[\\s|\\d]+

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM