简体   繁体   English

如何在正则表达式中区分具有相同格式的捕获组

[英]How to differentiate capture groups with the same format in Regex

I'm going through several log entires in one log file and each of them contain this line: 我正在一个日志文件中浏览几个日志,每个日志都包含以下行:

Completed in 0.00023 (4328 reqs/sec) | Rendering: 0.00010 (45%) | DB: 0.00000 (0%)

My capture groups are: 我的捕获组是:

0.00023 (4328 reqs/sec)
0.00010 (45%)
0.00000 (0%)

I need to parse each log entry and store the parsed values inside a database. 我需要解析每个日志条目并将解析后的值存储在数据库中。

Example: 例:

       cycle_time        |  render-time   |  active_record 
                         |                | 
 0.00023 (4328 reqs/sec) |  0.00010 (45%) |     0.00000 (0%)
                         |                |

I'm using regex to parse them but my problem is I can't differentiate the values from each other since they almost have the same format. 我正在使用正则表达式来解析它们,但我的问题是我无法将值彼此区分开,因为它们几乎具有相同的格式。 I currently use this regex expression: 我目前使用此正则表达式:

\d\.\d{5}\s\(\d{1,2}\%\)

However, it only captures the values of render_time and active_record and it can't even differentiate between the two. 但是,它仅捕获render_time和active_record的值,甚至无法区分两者。 Any ideas? 有任何想法吗?

You could try something like: 您可以尝试类似:

((?<=Completed in ).+?(?= \|)).*((?<=Rendering: ).+?(?= \|)).*((?<=DB: ).*)$

Which will give you 3 groups that you can then store separately into your database: 这将为您提供3个组,然后可以将它们分别存储到数据库中:

 1. 0.00023 (4328 reqs/sec)
 2. 0.00010 (45%)
 3. 0.00000 (0%)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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