Title: How do I print output in a certain way
Tags: c#;linq;token;cc
Question: I have written this code using LINQ query

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

        Dictionary<string, string&gt; dictionaryofBraces = new Dictionary<string, string&gt;()
        {
            //{"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 =&gt; Regex.Matches(input, a.Value)
        .Cast<Match&gt;()
        .Select(b =&gt;
                new
                {
                    Index = b.Index,
                    Value = b.Value,
                    Token = a.Key
                }))
        .OrderBy(a =&gt; 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 &gt; matches[(i + 1)].Index)
                {
                    matches.RemoveAt(i + 1);
                    i--;
                }
            }
        }
        foreach (var match in matches)
        {
            Console.WriteLine(match);
        }

    }
```

it's output is something like this
    {Index=0, Value= {, Token=Brace}

But I want Output be like
{    BRACE

Comment: This is a tokenizer. Lexical Analyser
Input: {}
Output: {Index=0, Value= {, Token=Brace}
             {Index=1, Value= }, Token=Brace}

But I want Output be like
Output:  {     Brace
              }     Brace

Comment: I have tried a lot. i'm still learning LINQ query. So not much into it! Sorry! Could use help!

Comment: Could you elaborate a littble bit more on your problem? Can you supply an example of your input? Which part exactly do you want to change (and how)? The output?

Comment: This is your code, but you can't change the output formatting?

Here is the accepted answer: One possibility would be to modify the anonymous object - create the string from the ```Key```(=Brace) and the ```Value```(={ or }):

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

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

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

The output is as desired:

```{ BRACE
} BRACE 
```

Comment for this answer: Glad that the answer helped.


{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 4, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2016:00:03/25/16', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<c#><linq><token><cc>', 'Title': 'How do I print output in a certain way', 'ViewCount': 46, 'id': 36220040, 'user_AboutMe': None, 'user_AccountId': 6866128.0, 'user_DisplayName': 'Ali', 'user_Reputation': 71.0, 'user_Views': 24.0, 'user_id': 5278524.0}}