Results 1 to 13 of 13

Thread: Regex Pattern for comment lines of C# code?

  1. #1

    Thread Starter
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083

    Regex Pattern for comment lines of C# code?

    I want to colorize comments shown in RTB in the three formats :

    1-
    PHP Code:
    /*
     *This is a comment 
    */ 
    2-
    PHP Code:
    //This is comment 
    // 

    3-
    PHP Code:
    ///This is function description 
    I want only the regex pattern . Thanks guys .
    Last edited by Pirate; May 29th, 2004 at 12:36 PM.

  2. #2
    Frenzied Member dynamic_sysop's Avatar
    Join Date
    Jun 2003
    Location
    Ashby, Leicestershire.
    Posts
    1,142
    check this link pirate mate , it's perl ( Syntax highlighting C# code ) , but it's not to hard to follow ...
    Using Perl to Syntax Color C#
    ~
    if a post is resolved, please mark it as [Resolved]
    protected string get_Signature(){return Censored;}
    [vbcode][php] please use code tags when posting any code [/php][/vbcode]

  3. #3

    Thread Starter
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Thanks , I'll check it out and see if I can do something with it .

  4. #4

    Thread Starter
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    I couldn't figure out the pattern for comment in that link.

  5. #5

    Thread Starter
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    *bump*

  6. #6
    Frenzied Member dynamic_sysop's Avatar
    Join Date
    Jun 2003
    Location
    Ashby, Leicestershire.
    Posts
    1,142
    ok here's a quick example i knocked up for the /* comment */ type of comments @"[/*]\s*([^"">]+)\s*[*/]\s*[/]" being the pattern ...
    VB Code:
    1. [color=blue]private void[/color] richTextBox1_TextChanged([color=blue]object[/color] sender, System.EventArgs e)
    2.         {
    3.             Regex rgx=[color=blue]new[/color] Regex(@"[/*]s*([^"">]+)s*[*/]s*[/]" , RegexOptions.IgnoreCase);
    4.             MatchCollection mCol=rgx.Matches(richTextBox1.Text);
    5.             [color=blue]foreach[/color](Match m [color=blue]in[/color] mCol)
    6.             {
    7.                 richTextBox1.HideSelection=[color=blue]true[/color];
    8.                 richTextBox1.Select(m.Index , m.Length);
    9.                 richTextBox1.SelectionColor=Color.Green;
    10.                 richTextBox1.SelectionStart=m.Index + m.Length;
    11.                 richTextBox1.SelectionLength=0;
    12.             }
    13.         }
    for the // comments it's @"[//]\s*([^"">]+)\s*[\n]" ...
    VB Code:
    1. [color=blue]private void[/color] richTextBox1_TextChanged([color=blue]object[/color] sender, System.EventArgs e)
    2.         {
    3.             Regex rgx=[color=blue]new[/color] Regex(@"[//]s*([^"">]+)s*[\n]" , RegexOptions.IgnoreCase);
    4.             MatchCollection mCol=rgx.Matches(richTextBox1.Text);
    5.             [color=blue]foreach[/color](Match m [color=blue]in[/color] mCol)
    6.             {
    7.                 richTextBox1.HideSelection=[color=blue]true[/color];
    8.                 richTextBox1.Select(m.Index , m.Length);
    9.                 richTextBox1.SelectionColor=Color.Green;
    10.                 richTextBox1.SelectionStart=m.Index + m.Length;
    11.                 richTextBox1.SelectionLength=0;
    12.             }
    13.         }
    hope it helps mate
    ~
    if a post is resolved, please mark it as [Resolved]
    protected string get_Signature(){return Censored;}
    [vbcode][php] please use code tags when posting any code [/php][/vbcode]

  7. #7

    Thread Starter
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    That did the trick 80% correct except this :

    line of code here //this is comment
    another code here


    the second line "another code here" is considered to be comment also but actually it's code .
    1-How can I fix this ?
    2-It skip these "#region" and this " /// <summary>....<summary/>.

    Thanks so far , dude .

  8. #8
    Frenzied Member dynamic_sysop's Avatar
    Join Date
    Jun 2003
    Location
    Ashby, Leicestershire.
    Posts
    1,142
    you need to set the selectioncolor back to it's default ( eg: Color.Black ) after highlighting , also it's better under a button click or a void rather than the text changed area.
    PHP Code:
            private void button1_Click(object senderSystem.EventArgs e)
            {
                
    Regex rgx=new Regex(@"[//]\s*([^"">]+)\s*[\n]" RegexOptions.IgnoreCase);
                
    MatchCollection mCol=rgx.Matches(richTextBox1.Text);
                foreach(
    Match m in mCol)
                {
                    
    richTextBox1.HideSelection=true;
                    
    richTextBox1.Select(m.Index m.Length);
                    
    richTextBox1.SelectionColor=Color.Green;
                    
    richTextBox1.SelectionStart=m.Index m.Length;
                    
    richTextBox1.SelectionLength=0;
                    
    richTextBox1.SelectionColor=Color.Black// revert the text back to it's default color.
                
    }
            } 
    ~
    if a post is resolved, please mark it as [Resolved]
    protected string get_Signature(){return Censored;}
    [vbcode][php] please use code tags when posting any code [/php][/vbcode]

  9. #9

    Thread Starter
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    I'll check this when I get home....

  10. #10

    Thread Starter
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Thanks a lot , that seems working now . One more

    2-It skip these "#region" and this " /// <summary>....<summary/>.

    ??

  11. #11
    Fanatic Member brown monkey's Avatar
    Join Date
    Jun 2004
    Location
    Cebu
    Posts
    552
    hello mate. i just make something similar but won't work with
    PHP Code:
    /* this is a comment
     */ 
    this is what i've got
    PHP Code:
          private void button1_Click(object senderSystem.EventArgs e)
          {
             
    Regex rgx=new Regex(@"(//.*[\n])|(///.*[\n])|(#region.*[\n])|(/\*.*?\*/)" ,
                
    RegexOptions.IgnoreCase);
             
    MatchCollection mCol=rgx.Matches(richTextBox1.Text);
             foreach(
    Match m in mCol)
             {
                
    richTextBox1.HideSelection=true;
                
    richTextBox1.Select(m.Index m.Length);
                
    richTextBox1.SelectionColor=Color.Green;
                
    richTextBox1.SelectionStart=m.Index m.Length;
                
    richTextBox1.SelectionLength=0;
                
    richTextBox1.SelectionColor=Color.Black// revert the text back to it's default color.
             
    }
          } 
    still missing C style comment [multiline]. any ideas?

  12. #12
    Fanatic Member brown monkey's Avatar
    Join Date
    Jun 2004
    Location
    Cebu
    Posts
    552
    halu mate. got it. but still i don't know if this efficient. lol
    Code:
             Regex rgx=new Regex(@"(//.*[\n])|(///.*[\n])|"+
                @"(#region.*[\n])|(/\*.*?\*/)|(/\*[^""]*\*/)" ,
                RegexOptions.IgnoreCase);
    Last edited by brown monkey; Jun 25th, 2004 at 10:58 PM.

  13. #13

    Thread Starter
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Originally posted by brown monkey
    halu mate. got it. but still i don't if this efficient. lol
    PHP Code:
             Regex rgx=new Regex(@"(//.*[\n])|(///.*[\n])|"+
                @
    "(#region.*[\n])|(/\*.*?\*/)|(/\*[^""]*\*/)" ,
                
    RegexOptions.IgnoreCase); 
    I tried to include the xor case in the regex pattern and it slowed things incredibly . When I get home I'll try to work it out!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width