2012-07-13 4 views
0

비슷한 형식 (실제로 파일 경로)의 행이 있습니다. 예를 들어 :특수 단어 앞에 단일 슬래시가있는 행을 일치 시키십시오.

root/DATA/some/file.txt 
root/DATA/another/file.txt 
root/DATA/yet/another/file.exe 
root/site/some/other/folder/before/DATA/file.xml 
root/site/some/other/folder/DATA/file2.xml 

난 단지DATA 전에 단일 슬래시를 포함하는 것들을하고 싶습니다, 그것은 먼저 3 위의 일치해야하지만, 지난 2 안. 참고 : root/\을 제외한 일련의 문자로 간주됩니다.

나는이 정규식 결국,하지만 여전히 5 개 샘플과 일치 :

[^/]*/data/.* 

을 그리고 난 경우 DATA의 행을 필터링하도록 지시하려면 다음을하지 않습니다 ... 어떻게 여기에 붙어있어 명시 적으로 첫 번째 슬래시 뒤에?

^[^/]*/data/.* 
또한

, 정규식 여러 줄 모드에 있는지 확인 & 경우이 문제가 해결됩니다

+0

Regex 대소 문자를 구분하지 않으셨습니까? – Oded

+0

@Oded, 네.하지만 문제는 정규식 자체에서 볼 수 있습니다. –

답변

3

을 무시 :

+0

사실! 평소와 같이, 마지막 작은 하나가 빠졌습니다 ... 고마워요! –

+0

정규 표현식에서 ^를 사용하는 성가신 부분은 두 가지 의미가 있다는 것입니다. 물건을 간과하기 쉬워집니다. – dda

1

당신은 라인의 시작을 표시해야합니다

^[^/]*/DATA/.*$ 
+0

감사합니다! 실제로, 매 단선마다 매치를 체크하므로'multiline'을 on으로 설정할 필요가 없습니다. –

0
Regex regex = new Regex("^[^/]*/data/.*", 
         RegexOptions.IgnoreCase|RegexOptions.Multiline); 
+0

-1 : 여러 줄로 설정해야합니다 –

+0

그냥 고쳐주고 수정했습니다. –

0

많은 옵션이 있습니다. 모든 DATA를 캡처 한 다음 얼마나 많은 데이터를 확인할 수 있는지 '/'요소는 DATA 텍스트 앞에 있습니다 (예 : 첫 번째 그룹). 당신은 긴 문자열 등을 확인 할 수 - 정확히 당신이 코드를 사용하여 시뮬레이션하고 재사용 할 수 요청한 것은 : 그것의

string type_1 = "" + 
    "root/DATA/some/file.txt" + "\n" + 
    "root/DATA/another/file.txt" + "\n" + 
    "root/DATA/yet/another/file.exe" + "\n" + 
    "root/site/some/other/folder/before/DATA/file.xml" + "\n" + 
    "root/site/some/other/folder/DATA/file2.xml"; 

Console.WriteLine ("Start TEXT:"); 
Console.WriteLine (type_1); 


Console.WriteLine ("Result TEXT:"); 
MatchCollection mat = Regex.Matches (type_1, "^[^/]*/DATA.*?$", RegexOptions.Compiled|RegexOptions.Multiline); 
Console.WriteLine (mat.Count); 
foreach (Match m in mat) { 
    Console.WriteLine (m.ToString()); 
} 

결과는 일이있어 :

Start TEXT: 
root/DATA/some/file.txt 
root/DATA/another/file.txt 
root/DATA/yet/another/file.exe 
root/site/some/other/folder/before/DATA/file.xml 
root/site/some/other/folder/DATA/file2.xml 
Result TEXT: 
3 
root/DATA/some/file.txt 
root/DATA/another/file.txt 
root/DATA/yet/another/file.exe 

가 작동을 첫 번째 'DATA'앞에 '/'를 사용할 수 없다고 가정하면됩니다.

관련 문제