Regular Expression and color text

Wibs

Member
Joined
Jan 16, 2009
Messages
20
Programming Experience
Beginner
Hi,

I need some help to create a regular expression. It is a little like a search and replace, I want to replace any string that contains any substring in parentheses with the same string but the words in parentheses colored RED.

Eg

Take this text in TextBox1:

"The Prime Minister (that idiot from Scotland) made a speech today"

and convert to this text in RichTextBox1:

"The Prime Minister (that idiot from Scotland) made a speech today"

TIA

Wibs
 
Try this out...

VB.NET:
        Dim strText As String
        Dim match As Match = System.Text.RegularExpressions.Regex.Match(TextBox1.Text, "\((.*)\)")
        If match.Success = True Then
            strText = TextBox1.Text.Replace("(", "(<span style='color: red'>").Replace(")", "</span>)")
        Else
            strText = TextBox1.Text
        End If

basically it's looking for anytext between ( and ) and if there is a match replacing the ( and ) with <span style='color: red'> and </span>
 
Sorry Misread...

for some reason I misread your post i wasn't thinking about the RichTextBox control...

Try this

VB.NET:
        Dim match As System.Text.RegularExpressions.Match = System.Text.RegularExpressions.Regex.Match(TextBox1.Text, "\((.*)\)")
        If match.Success = True Then
            RichTextBox1.Text = TextBox1.Text
            Dim SelStart As Integer = RichTextBox1.Find("(") + 1
            Dim SelStop As Integer = RichTextBox1.Find(")") - 1
            Dim SelText As String = Nothing
            For i As Integer = SelStart  To SelStop
                SelText = SelText & TextBox1.Text.Chars(i).ToString()
            Next
            RichTextBox1.SelectionStart = RichTextBox1.Find(SelText)
            RichTextBox1.SelectionColor = Color.Red
        Else
            RichTextBox1.Text = TextBox1.Text
        End If
 
alliancejhall, RichTextBox don't support Html code, also remember * is greedy. edit: noticed you added a post, but you're still not using Regex.
Wibs said:
I want to replace any string that contains any substring in parentheses with the same string but the words in parentheses colored RED.
You don't replace text in RichTextBox to format it, you make a selection and apply formatting to the selection using one of the Selection... properties, for example SelectionColor. This code works:
VB.NET:
Me.RichTextBox1.Text = Me.TextBox1.Text
For Each m As Match In Regex.Matches(Me.TextBox1.Text, "\((.+?)\)")
    Me.RichTextBox1.Select(m.Groups(1).Index, m.Groups(1).Length)
    Me.RichTextBox1.SelectionColor = Color.Red
Next
Me.RichTextBox1.Select(Me.RichTextBox1.TextLength, 0)
The regex explained, since the paranthesis has a special meaning (grouping) these need to be escaped, then a group is defined which match any text, the text match is made lazy in order to find minimum number of chars that represent the start-paranthesis text end-paranthesis pattern for the whole match. Each Match will then return the matched text in second group, first group is always the entire matched text, ie including the paranthesis in this case. The captured Group has Index and Length properties needed here to make the RTB selection.
 
Very good...

Thanks for the correction that is much cleaner and nicer. I'm not strong on regex and for some reason i though they were working on a webapp so I guess i should have left it to the experts! :rolleyes:

edit: Yours also accounts for multiple groups of ( and ) mine did not! you learn something new everday.
 
Many thanks John. I did not realise that parentheses had a special meaning, is this the same for brackets [ ] and braces { } too? I actually wanted to color text in parentheses ( ) Red, in brackets [ ] blue, and braces { } yellow.

If "\((.+?)\)" is the Regular Expression for parentheses, could I ask what is the expression for brackets, and braces?

Thanks again,

Wibs
 
Or, in one:

Regex("\[(.+?)\]|\{(.+?)\}|\((.+?)\)")

(though for coloring diff colours, prob easier to do them one by one
 
Back
Top