2010-12-20 5 views
0

파스칼 문자열 리터럴 입력을 다음 패턴으로 일치 시키려고합니다 : @"^'([^']|(''))*'$",하지만 작동하지 않습니다. 이 패턴에 어떤 문제가 있습니까? ,파스칼과 같은 문자열 리터럴 정규 표현식

private void Initialize() 
{ 
    MatchingTable = new Dictionary<TokenUnit.TokenType, Regex>(); 

    MatchingTable[TokenUnit.TokenType.Identifier] = new Regex 
    (
     @"^[_a-zA-Z]\w*$", 
     RegexOptions.Compiled | RegexOptions.Singleline 
    ); 
    MatchingTable[TokenUnit.TokenType.NumberLiteral] = new Regex 
    (
     @"(?:^\d+$)|(?:^\d+\.\d*$)|(?:^\d*\.\d+$)", 
     RegexOptions.Compiled | RegexOptions.Singleline 
    ); 
} 
// ... Here it all comes together 
public TokenUnit Scan(String input) 
{       
    foreach(KeyValuePair<TokenUnit.TokenType, Regex> node in this.MatchingTable) 
    { 
     if(node.Value.IsMatch(input)) 
     { 
      return new TokenUnit 
      { 
       Type = node.Key       
      }; 
     } 
    } 
    return new TokenUnit 
    { 
     Type = TokenUnit.TokenType.Unsupported 
    }; 
} 
+3

파스칼 급 문자열 리터럴이란 무엇입니까? [This?] (http://en.wikipedia.org/wiki/String_literal#Double-up_escape_sequence) –

+1

일부 입력 문자열과 예상 결과를 표시 할 수 있습니까? – Toto

답변

1
패턴이 올 것으로 보인다

: 나는 어떤 파스칼 주석이 항목에 대한 입력 문자열을 검색 공백으로 대체

public void Run() 
{    
    using(StreamReader reader = new StreamReader(String.Empty)) 
    { 
     var LineNumber = 0; 
     var LineContent = String.Empty; 

     while(null != (LineContent = reader.ReadLine())) 
     { 
      LineNumber++; 

      String[] InputWords = new Regex(@"\(\*(?:\w|\d)*\*\)").Replace(LineContent.TrimStart(' '), @" ").Split(' '); 

      foreach(String word in InputWords) 
      { 
       Scanner.Scan(word); 
      } 

     } 
    } 
} 

후 나는 다음에 일치하는 문자열에 입력을 분할 그것은 간단하게 할 수 있지만 :

^'(?:[^']+|'')*'$ 

설명 :

^  # Match start of string 
'  # Match the opening quote 
(?: # Match either... 
[^']+ # one or more characters except the quote character 
|  # or 
'' # two quote characters (= escaped quote) 
)*  # any number of times 
'  # Then match the closing quote 
$  # Match end of string 

이 정규식을 검사하는 입력에 파스칼 문자열 (예 : 주변 공백) 이외의 문자가 포함되어 있으면이 정규식이 실패합니다.

큰 텍스트 코퍼스에서 파스칼 문자열을 찾으려면 정규 표현식을 사용하려면 ^$ 앵커를 제거해야합니다. C#에서

^(?:'(?:[^']+|'')*'|"(?:[^"]+|"")*")$ 

: 당신이 너무 큰 따옴표를 허용하려면

이 그리고, 당신은 정규식을 확대 할 필요가

foundMatch = Regex.IsMatch(subjectString, "^(?:'(?:[^']+|'')*'|\"(?:[^\"]+|\"\")*\")$"); 

이 정규식

'This matches.' 
'This too, even though it ''contains quotes''.' 
"Mixed quotes aren't a problem." 
'' 
같은 문자열을 일치합니다

문자열과 일치하지 않습니다.

'The quotes aren't balanced or escaped.' 
There is something 'before or after' the quotes. 
    "Even whitespace is a problem." 
+0

하나의 어휘 클래스와 일치시키기 위해 단일 문자열 당 공백 분할 입력을 사용합니다. 그래서 앵커를 붙였습니다. 그래서 내가 알기 론 파스칼의 '파스칼과 같은' '문자열'에서는 적절한 순서가 아닙니다. 내가 맞습니까? – lexeme

+0

공백으로 입력을 분할하면 문자열 안에 분할됩니다. 그렇지 않습니까? 이 정규식의 일치 여부와 일치하지 않는 몇 가지 예를 제공합니다. 실제 입력의 샘플을 제공하고 (질문을 수정하고 샘플을 붙여 넣기를 원할 수도 있음) –

+0

문자열 리터럴에 대해 알고 싶습니다. 라인을 공백으로 나눈다면 나는 작업 패턴과조차도 일치시킬 수 없을 것입니다. 그래서 내가 뭘 할거야 ?? 덕분에 조언을! – lexeme