我试图从文本日志文件中提取某些数据并将该数据保存到输出文件中。 文本文件中的数据如下所示:
2014-08-23 19:05:09 <nonmatching line>
2014-08-23 19:05:09 MATCH_STRING <stuff_I_don't_want> @description='12345 queue1 1 2 3' <more_stuff_I_don't_want_to_EOL>
2014-08-23 19:05:09 <nonmatching line>
2014-08-23 19:05:09 <nonmatching line>
2014-08-23 19:05:09 MATCH_STRING <stuff_I_don't_want> @description='12345 queue1 4 5 6' <more_stuff_I_don't_want_to_EOL>
我想创建一个如下所示的输出文件:
2014-08-23 19:05:09 12345 queue1 1 2 3
2014-08-23 19:05:09 12345 queue1 4 5 6
我有两个RegEx表达式用于2个必要的匹配,当它们单独使用时,它们都可以工作,如下所示:
(^.*?)(?=\b\tMATCH_STRING\b)
返回
2014-08-23 19:05:09
2014-08-23 19:05:09
和
(?<=@description\=')(?:(?!').)*
返回
12345 queue1 1 2 3
12345 queue1 4 5 6
问题是: 如何将它们放在一起,以便它们匹配行开头的日期和行中带引号的字符串?
奖金问题:我正在尝试做什么更有效的RegEx?
由于
答案 0 :(得分:0)
(\d+-\d+-\d+\s*\d+:\d+:\d+).*?(?=@description).*?=.(\d+)\s*(.*?[\d\s]+)
这个正则表达式提供了你想要的所有组。
参见演示。
答案 1 :(得分:0)
另一种解决方案:
$matchstring = "MATCH_STRING"
$pattern = "(.*?)(?:\s*?$([regex]::Escape($matchstring)).*?description=')(.*?)'.*"
@"
2014-08-23 19:05:09 <nonmatching line>
2014-08-23 19:05:09 MATCH_STRING <stuff_I_don't_want> @description='12345 queue1 1 2 3' <more_stuff_I_don't_want_to_EOL>
2014-08-23 19:05:09 <nonmatching line>
2014-08-23 19:05:09 <nonmatching line>
2014-08-23 19:05:09MATCH_STRING <stuff_I_don't_want> @description='12345 queue1 4 5 6' <more_stuff_I_don't_want_to_EOL
"@ -split [environment]::newline |
Where-Object { $_ -match $pattern } |
ForEach-Object { $_ -replace $pattern, '$1 $2' }
2014-08-23 19:05:09 12345 queue1 1 2 3
2014-08-23 19:05:09 12345 queue1 4 5 6
答案 2 :(得分:0)
你可以使用这样的正则表达式:
^([\d-:\s]{2,}).*?(?<==)'(\d+)(.+?)'
<强> Working demo 强>
MATCH 1
1. [39-59] `2014-08-23 19:05:09 `
2. [107-112] `12345`
3. [112-125] ` queue1 1 2 3`
MATCH 2
1. [238-258] `2014-08-23 19:05:09 `
2. [306-311] `12345`
3. [311-324] ` queue1 4 5 6`