2012-12-11 5 views
1

에 예외를 던지는 다음 코드는 'ConsoleApplication1.Program'의 형식 이니셜 라이저에서 예외를 Throw정규식은 [] 실행

가 발생합니다. 선 내가 그것을 어떻게 왜 문제를 해결할 수이 잘못되고

public static Regexp[] keepers = { ... }; 

?

namespace ConsoleApplication1 
{ 
    class Program 
    { 
     public static String output = ""; 
     public static Regex[] keepers = { 
              new Regex(@"using(?<everythingElse> [a-zA-Z.]+;)"), 
              new Regex(@"namespace(?<everythingElse> [a-zA-Z._]+)"), 
              new Regex(@"class(?<everythingElse> [a-zA-Z._]+)"), 
              new Regex(@"(public|private)? ?(static)? ?(?<type> String|void|int|Double)(" + Regex.Escape("[") + "?" + Regex.Escape("]") + "?" + "(?<functionName> [a-z_]+)(?<params> [^\r\n]+)") 
             }; 
     [STAThread] 
     static void Main(string[] args) 
     {}}} 
+0

의 InnerException을 읽어보십시오. – SLaks

+2

배열의 정규 표현식에 중 일부는 잘못된 것입니다. 당신은 제거하는 경우 이니셜 라이저를 초기화하고 Main()에서 그것들 모두를 초기화하려고하면 어떤 것을 찾을 수 있습니까? – nothrow

+0

왜 그것은 정말로 부풀어 오른''+ [] 대신 Regex.Escape ("[") + '? –

답변

3

는이 (말에. 이것은 폐쇄하지 않을 열립니다!

+0

+1 정답 –

6

항상 전체 예외에보세요. 여기가 (약간 포맷) 귀하의 경우입니다 :

Unhandled Exception: System.TypeInitializationException: The type initializer for 
'ConsoleApplication1.Program' threw an exception. ---> System.ArgumentException: 
parsing "(public|private)? ?(static)? ?(?<type> String|void|int|Double)(\[?]? 
(?<functionName> [a-z_]+)(?<params> [^]+)" - Not enough)'s. 
    at System.Text.RegularExpressions.RegexParser.ScanRegex() 
    at System.Text.RegularExpressions.RegexParser.Parse(String re, RegexOptions op) 
    at System.Text.RegularExpressions.Regex..ctor(String pattern, RegexOptions options, TimeSpan matchTimeout, Boolean useCache) 
    at System.Text.RegularExpressions.Regex..ctor(String pattern) 
    at ConsoleApplication1.Program..cctor() 
    --- End of inner exception stack trace --- 
    at ConsoleApplication1.Program.Main(String[] args) 

그래서 당신은 볼 수 있습니다

  • 그것은 당신이 당신의 괄호에 문제가있어
  • 네 번째 정규 표현식의

다음으로 큰 정규 표현식을 작은 것으로 나눠서 시도해 보겠습니다. 대괄호가없는 이유는 무엇입니까?

"(" + Regex.Escape("[") + "?" + Regex.Escape("]") + "?" 

가 있어야한다 :

"(" + Regex.Escape("[") + "?" + Regex.Escape("]") + ")?" 

을 ...하지만 당신은 확인해야합니다 나는 이것을 생각한다. 4. 정규식

@"(public|private)? ?(static)? ?(?<type> String|void|int|Double)(

에서

+0

+0 대괄호에 어떤 문제가 있습니까? –

+0

그래, 그는 절대 닫히지 않는 연결 앞에 하나 열어. –

+1

@SamIam : OP에 맡기는 것이 합리적이라고 생각하지만 제안 해 드리겠습니다. –