2014-08-28 4 views
0

텍스트 로그 파일에서 특정 데이터를 가져 와서 해당 데이터를 출력 파일에 저장하려고합니다. 텍스트 파일 데이터는 다음과 같습니다 : 2 필요한 경기 두 정규식 표현과 때Powershell RegEx : 두 개의 일치하는 문자열을 어떻게 일치시킬 수 있습니까?

2014-08-23 19:05:09 12345 queue1 1 2 3 
2014-08-23 19:05:09 12345 queue1 4 5 6 

내가 가진 :

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> 

나는이처럼 보이는 넣어 파일을 만들려면 그들은 모두 아래 작업, 별도로 사용됩니다

(^.*?)(?=\b\tMATCH_STRING\b) 

반환

2014-08-23 19:05:09 
2014-08-23 19:05:09 

(?<[email protected]\=')(?:(?!').)* 

반환

12345 queue1 1 2 3 
12345 queue1 4 5 6 

질문은 : 그들이 줄의 시작 부분에 모두 날짜와의 인용 문자열을 일치하도록 내가 함께 넣어 어떻게 선?

보너스 질문 : 내가하려는 일에 대해 더 효율적인 RegEx가 있습니까?

감사

답변

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 
0

당신은 다음과 같은 정규식을 사용할 수 있습니다

^([\d-:\s]{2,}).*?(?<==)'(\d+)(.+?)' 

Working demo

enter image description here

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` 
관련 문제