-
Jan 24th, 2020, 09:24 PM
#1
Thread Starter
Lively Member
[RESOLVED] Color HTML tags <> in RichTextBox
I am using this code and it works, but it seems like I should be able to match all tags using the pattern "< >" without creating a list?
Code:
Dim current_cursor_position As Integer = Me.rtbCode.SelectionStart
'This is useful to get a hold of where is the current cursor at
'this will be needed once all coloring is done, and we need to return
Dim html() As String = {"<!DOCTYPE html>", "<!DOCTYPE html>", "<html>", "</html>", "<head>", "</head>", "<style>", "</style>",
"<body>", "</body>", "pre>", "</pre>", "<!DOCTYPE>", "<title>", "<ul>", "</ul>", "<li>", "</li>",
"</title>", "<a>", "</a>", "<abbr>", "<address>", "<area>", "</area>", "<article>", "<b>", "</b>", "<p>", "</p>",
"<aside>", "<audio>", "<acronym>", "<applet>", "<b>", "<base>", "<div>", "</div>", "<span>", "</span>",
"<blockquote>", "<br>", "<button>", "</button>",
"<basefont>", "<bgsound>", "<big>", "<blink>", "<img>", "</img>",
"<input>", "</input>"}
Dim pattern As String = "<(.)*?>"
Dim matches As MatchCollection = Regex.Matches(Me.rtbCode.Text, pattern)
For Each match In matches
Me.rtbCode.Select(match.index, match.length)
Dim lookFor As String = match.ToString
If match.ToString.Contains(" ") Then 'Checking if tag contains properties
lookFor = match.ToString.Substring(0, match.ToString.IndexOf(" ")) & ">"
'This line will strip away any extra properties, values, and will
' close up the tag to be able to look for it in the allowed array
End If
If html.Contains(lookFor.ToString.ToLower) Then
'The tag is part of the allowed tags, and can be colored blue.
Me.rtbCode.SelectionColor = Color.Blue
Else
'This tag is not recognized, and shall be colored black..
Me.rtbCode.SelectionColor = Color.Black
End If
Next
Me.rtbCode.SelectionStart = current_cursor_position
'Returning cursor to its original position
Me.rtbCode.SelectionLength = 0
'De-Selecting text (if it was selected)
Me.rtbCode.SelectionColor = Color.Black
'new text will be colored black, until
'recognized as HTML tag.
I'm not understanding this part...
Code:
Dim pattern As String = "<(.)*?>"
Everything I've looked up seems to say that you need to match a known number or range of characters. Is there a way to match anything contained within the "< >" tags?
I may not know anything, but I know it well!
-
Jan 24th, 2020, 09:52 PM
#2
Re: Color HTML tags <> in RichTextBox
The pattern is a regular expression, so if you want to know more about that, you can research that topic. It's a big topic but the basics aren't too hard. That code is already matching everything inside angle brackets. That's what the regular expression does. The list is then being used to compare each of those matches to see whether it's allowed or not. If you don't care about that, don't do that part. The code shows you how to select the matched text and colour it. You have all you need.
-
Jan 24th, 2020, 10:04 PM
#3
Thread Starter
Lively Member
Re: Color HTML tags <> in RichTextBox
The pattern is a regular expression, so if you want to know more about that, you can research that topic
Yes, def need to research more on that.
That code is already matching everything inside angle brackets.
I wasn't sure because I couldn't understand the "(.)*?" part.
So removing the list and the check to see if it's in there will fix the problem.
Now I just need to research the "(.)*?" regular expression part. It doesn't look that regular to me.
Thanks for the help.
It works well...
Code:
Dim current_cursor_position As Integer = Me.rtbCode.SelectionStart
'This is useful to get a hold of where is the current cursor at
'this will be needed once all coloring is done, and we need to return
Dim pattern As String = "<(.)*?>"
Dim matches As MatchCollection = Regex.Matches(Me.rtbCode.Text, pattern)
For Each match In matches
Me.rtbCode.Select(match.index, match.length)
Me.rtbCode.SelectionColor = Color.Blue
Next
Me.rtbCode.SelectionStart = current_cursor_position
'Returning cursor to its original position
Me.rtbCode.SelectionLength = 0
'De-Selecting text (if it was selected)
Me.rtbCode.SelectionColor = Color.Black
'new text will be colored black, until
'recognized as HTML tag.
And just to show you that I did my home work...
< matches the character < literally (case sensitive)
.*? matches any character (except for line terminators)
*? Quantifier — Matches between zero and unlimited times, as few times as possible, expanding as needed (lazy)
> matches the character > literally (case sensitive)
Last edited by VisualBrian; Jan 24th, 2020 at 10:24 PM.
Reason: Show working code
I may not know anything, but I know it well!
Tags for this Thread
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
|