|
-
May 29th, 2004, 03:19 AM
#1
Thread Starter
Sleep mode
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.
-
May 29th, 2004, 04:04 AM
#2
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]
-
May 29th, 2004, 04:40 AM
#3
Thread Starter
Sleep mode
Thanks , I'll check it out and see if I can do something with it .
-
May 29th, 2004, 08:08 AM
#4
Thread Starter
Sleep mode
I couldn't figure out the pattern for comment in that link.
-
May 29th, 2004, 09:52 PM
#5
Thread Starter
Sleep mode
-
May 30th, 2004, 05:42 PM
#6
ok here's a quick example i knocked up for the /* comment */ type of comments @"[/*]\s*([^"">]+)\s*[*/]\s*[/]" being the pattern ...
VB Code:
[color=blue]private void[/color] richTextBox1_TextChanged([color=blue]object[/color] sender, System.EventArgs e)
{
Regex rgx=[color=blue]new[/color] Regex(@"[/*]s*([^"">]+)s*[*/]s*[/]" , RegexOptions.IgnoreCase);
MatchCollection mCol=rgx.Matches(richTextBox1.Text);
[color=blue]foreach[/color](Match m [color=blue]in[/color] mCol)
{
richTextBox1.HideSelection=[color=blue]true[/color];
richTextBox1.Select(m.Index , m.Length);
richTextBox1.SelectionColor=Color.Green;
richTextBox1.SelectionStart=m.Index + m.Length;
richTextBox1.SelectionLength=0;
}
}
for the // comments it's @"[//]\s*([^"">]+)\s*[\n]" ...
VB Code:
[color=blue]private void[/color] richTextBox1_TextChanged([color=blue]object[/color] sender, System.EventArgs e)
{
Regex rgx=[color=blue]new[/color] Regex(@"[//]s*([^"">]+)s*[\n]" , RegexOptions.IgnoreCase);
MatchCollection mCol=rgx.Matches(richTextBox1.Text);
[color=blue]foreach[/color](Match m [color=blue]in[/color] mCol)
{
richTextBox1.HideSelection=[color=blue]true[/color];
richTextBox1.Select(m.Index , m.Length);
richTextBox1.SelectionColor=Color.Green;
richTextBox1.SelectionStart=m.Index + m.Length;
richTextBox1.SelectionLength=0;
}
}
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]
-
May 31st, 2004, 12:11 AM
#7
Thread Starter
Sleep mode
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 .
-
May 31st, 2004, 07:26 AM
#8
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 sender, System.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]
-
May 31st, 2004, 03:49 PM
#9
Thread Starter
Sleep mode
I'll check this when I get home....
-
May 31st, 2004, 10:00 PM
#10
Thread Starter
Sleep mode
Thanks a lot , that seems working now . One more
2-It skip these "#region" and this " /// <summary>....<summary/>.
??
-
Jun 25th, 2004, 02:08 AM
#11
Fanatic Member
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 sender, System.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?
-
Jun 25th, 2004, 03:48 AM
#12
Fanatic Member
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.
-
Jun 25th, 2004, 02:15 PM
#13
Thread Starter
Sleep mode
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|