2016-03-25 15 views
0

ich mit diesem Code Abfrage LINQWie kann ich Ausgabe in einer bestimmten Art und Weise drucken

static public void BracesRule(String input) 
    { 
     //Regex for Braces 
     string BracesRegex = @"\{|\}"; 

     Dictionary<string, string> dictionaryofBraces = new Dictionary<string, string>() 
     { 
      //{"String", StringRegex}, 
      //{"Integer", IntegerRegex }, 
      //{"Comment", CommentRegex}, 
      //{"Keyword", KeywordRegex}, 
      //{"Datatype", DataTypeRegex }, 
      //{"Not included in language", WordRegex }, 
      //{"Identifier", IdentifierRegex }, 
      //{"Parenthesis", ParenthesisRegex }, 
      {"Brace", BracesRegex }, 
      //{"Square Bracket", ArrayBracketRegex }, 
      //{"Puncuation Mark", PuncuationRegex }, 
      //{"Relational Expression", RelationalExpressionRegex }, 
      //{"Arithmetic Operator", ArthimeticOperatorRegex }, 
      //{"Whitespace", WhitespaceRegex } 
     }; 
     var matches = dictionaryofBraces.SelectMany(a => Regex.Matches(input, a.Value) 
     .Cast<Match>() 
     .Select(b => 
       new 
       { 
        Index = b.Index, 
        Value = b.Value, 
        Token = a.Key 
       })) 
     .OrderBy(a => a.Index).ToList(); 

     for (int i = 0; i < matches.Count; i++) 
     { 
      if (i + 1 < matches.Count) 
      { 
       int firstEndPos = (matches[i].Index + matches[i].Value.Length); 
       if (firstEndPos > matches[(i + 1)].Index) 
       { 
        matches.RemoveAt(i + 1); 
        i--; 
       } 
      } 
     } 
     foreach (var match in matches) 
     { 
      Console.WriteLine(match); 
     } 

    } 

es Ausgabe geschrieben haben, ist so etwas wie dieses {Index = 0, Wert = {, Token = Brace}

Aber ich will Output sein wie {BRACE

+0

Könnten Sie ein littble bisschen mehr auf Ihr Problem näher erläutern? Können Sie ein Beispiel für Ihre Eingabe angeben? Welchen Teil möchten Sie genau ändern (und wie)? Die Ausgabe? – pasty

+0

Dies ist ein Tokenizer. Lexer Eingang: {} Ausgang: {Index = 0, Wert = {, Token = Brace} {Index = 1, Wert =}, Token = Brace} Aber ich will Output sein wie Ausgang: {Brace } Brace – Ali

+0

Das ist Ihr Code, aber Sie können die Ausgabeformatierung nicht ändern? – pasty

Antwort

1

Eine Möglichkeit, das anonyme Objekt zu ändern wäre - erstellen die Zeichenfolge des Key (= Brace) und die Value (= {oder}):

string input = "ali{}"; 
//Regex for Braces 
string BracesRegex = @"\{|\}"; 

Dictionary<string, string> dictionaryofBraces = new Dictionary<string, string>() 
{ 
    {"Brace", BracesRegex } 
}; 
var matches = dictionaryofBraces.SelectMany(a => Regex.Matches(input, a.Value) 
      .Cast<Match>() 
      .Select(b => String.Format("{0} {1}", b.Value, a.Key.ToUpper()))) 
      .OrderBy(a => a).ToList(); 

foreach (var match in matches) 
{ 
    Console.WriteLine(match); 
} 

Der Ausgang ist beliebig:

{ BRACE 
} BRACE 
+0

Gearbeiteter Kumpel. Danke – Ali

+0

Froh, dass die Antwort geholfen hat. – pasty