2013-04-21 3 views
1

내가이 코드를 실행하면 흥미로운 결과가 나옵니다.

string readText = File.ReadAllText("d:\\temp.txt"); 
    Console.WriteLine(readText); 

    Console.WriteLine("From File: "+Regex.Matches(readText,"$").Count);//-->2 

    Console.WriteLine("Add \\n to the File: "+Regex.Matches(readText + "\n", "$").Count);//--->2 
    Console.WriteLine("Add multiple \\n to the file: "+Regex.Matches(readText + "\n\n\n", "$").Count);//--->2 

    Console.WriteLine("on Text \"sample\": "+Regex.Matches("sample", "$").Count);//--->1 
    Console.WriteLine("on Text \"sample\\n\\n\\n\": "+Regex.Matches("sample" + "\n\n\n", "$").Count);//--->2 

출력 :

First line 

third 


Line 6 
Line 7 

From File: 2 
Add \n to the File: 2 
Add multiple \n to the file: 2 
on Text "sample": 1 
on Text "sample\n\n\n": 2 

의 날과 같은 결과를 제공하는 이유. 어느 누구도이 비행기를 앞다 can 수 있습니까? two possible positions에서

답변

1

$ 경기 : 마지막 전에 위치에서 입력 문자열

  • 의 말에

    1. ("문자열은 줄 바꿈으로 끝나는 '섹션으로 스크롤) 문자열이 linebreak로 끝나면 문자열에서 줄 바꿈을하십시오.

    문자열이 하나 이상의 개행으로 끝나면 $에 대해 두 개의 일치 항목이 표시됩니다. 다른 경우에는 하나를 얻습니다.

    문자열의 맨 끝 만 일치 시키려면 대신 \z을 사용하십시오.

    파이썬에서 실험 : 내가 처음 기초를 읽을 필요 :-)

    >>> [match.start() for match in re.finditer("$", "hello")] 
    [5] 
    >>> [match.start() for match in re.finditer("$", "hello\n")] 
    [5, 6] 
    >>> [match.start() for match in re.finditer("$", "hello\n\n")] 
    [6, 7] 
    >>> [match.start() for match in re.finditer("$", "hello\n\n\n")] 
    [7, 8] 
    
  • +1

    감사합니다 – Civa

    관련 문제