-
Sep 1st, 2020, 12:32 PM
#1
Thread Starter
Addicted Member
[RESOLVED] Coloring Text in Between Tags
Hi,
I know what Regex is, but I am not skilled in it.
Using a Richtextbox
All I need to do is color the text in between tags (these are not html tags) like this...
<S>H430</S> or <S>G7805</S>
All I need colored is the letter and the numbers. I will only have one H or G for the letter.
This is my code, but doesn't work.. I am sure I have the pattern wrong
Code:
Imports System.Text.RegularExpressions
Dim MyRegex As New Regex("<S>,</S>")
Dim MC As MatchCollection = MyRegex.Matches(txtChapter.Text)
Dim OldSel As Integer = txtChapter.SelectionStart
For Each M As Match In MC
txtChapter.SelectionStart = M.Index
txtChapter.SelectionLength = M.Value.Length
txtChapter.SelectionColor = Color.Red
Next
txtChapter.SelectionStart = OldSel
I am not sure how to setup my regex pattern for <S> or </S> to color all the text in between these two tags.
Any help to point me in the right direction would be appreciated.
-
Sep 1st, 2020, 02:05 PM
#2
Re: Coloring Text in Between Tags
Try this...
Code:
Dim MyRegex As New Regex("/<S/>/w{1}/d+/<//S/>")
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Sep 1st, 2020, 02:31 PM
#3
Thread Starter
Addicted Member
Re: Coloring Text in Between Tags
Originally Posted by .paul.
Try this...
Code:
Dim MyRegex As New Regex("/<S/>/w{1}/d+/<//S/>")
Thanks for replying.
Code block doesn't work.
Debugging... never enters the for next loop. Completely skips it
Code:
Dim MyRegex As New Regex("/<S/>/w{1}/d+/<//S/>")
Dim MC As MatchCollection = MyRegex.Matches(txtChapter.Text)
Dim OldSel As Integer = txtChapter.SelectionStart '' Old Selection Start
txtChapter.SelectAll()
txtChapter.SelectionColor = Color.Gray
txtChapter.DeselectAll()
For Each M As Match In MC '<--- NEVER ENTERS THE LOOP
txtChapter.SelectionStart = M.Index
txtChapter.SelectionLength = M.Value.Length
txtChapter.SelectionColor = Color.Red '' The Color That You Want
Next
-
Sep 1st, 2020, 02:35 PM
#4
Thread Starter
Addicted Member
Re: Coloring Text in Between Tags
I noticed that the "M" is not declared... what Dim statement do I use for this.
What is "M" in my for each loop supposed to be?
-
Sep 1st, 2020, 02:58 PM
#5
Re: Coloring Text in Between Tags
This is the pattern. I just tested it... "<S>[HG]{1}\d+</S>"
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Sep 1st, 2020, 03:15 PM
#6
Re: Coloring Text in Between Tags
To get just the text between the tags, use groups...
Code:
MsgBox(Regex.Match("<S>H430</S>", "<S>([HG]{1}\d+)</S>").Groups(1).Value)
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Sep 1st, 2020, 03:32 PM
#7
Thread Starter
Addicted Member
Re: Coloring Text in Between Tags
Originally Posted by .paul.
This is the pattern. I just tested it... "<S>[HG]{1}\d+</S>"
Great... closer.... except the tags are being colored to red too.
I would like to just color the text between the tags red only.
Thanks
-
Sep 1st, 2020, 03:33 PM
#8
Thread Starter
Addicted Member
Re: Coloring Text in Between Tags
Originally Posted by .paul.
To get just the text between the tags, use groups...
Code:
MsgBox(Regex.Match("<S>H430</S>", "<S>([HG]{1}\d+)</S>").Groups(1).Value)
Cool, this works to get the value. I may use that too.
However, I just need to be able to color that value red too. as per the code using Regex with the tags (previous post)
Last edited by pixelink; Sep 1st, 2020 at 03:42 PM.
-
Sep 1st, 2020, 03:43 PM
#9
Re: Coloring Text in Between Tags
I’m not at my workstation at the moment. It’ll probably be best (in this case) to get the index of the complete match and add 3 to the start position and subtract 7 from the length. Other than that your original code will work with the pattern I gave you...
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Sep 1st, 2020, 03:47 PM
#10
Thread Starter
Addicted Member
Re: Coloring Text in Between Tags
Actually... simple fix... I just removed the Tags...
Code:
Dim MyRegex As New Regex("[HG]{1}\d+")
Thanks... I am getting closer to this working
-
Sep 1st, 2020, 04:11 PM
#11
Re: Coloring Text in Between Tags
Originally Posted by pixelink
Actually... simple fix... I just removed the Tags...
Code:
Dim MyRegex As New Regex("[HG]{1}\d+")
Thanks... I am getting closer to this working
That won’t capture only text and numbers enclosed in <S></S> tags
If you use the first working pattern I gave you...
Code:
For Each M As Match In MC
txtChapter.SelectionStart = M.Index + 3
txtChapter.SelectionLength = M.Value.Length - 7
txtChapter.SelectionColor = Color.Red
Next
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Sep 1st, 2020, 04:21 PM
#12
Thread Starter
Addicted Member
Re: Coloring Text in Between Tags
Originally Posted by .paul.
That won’t capture only text and numbers enclosed in <S></S> tags
If you use the first working pattern I gave you...
Code:
For Each M As Match In MC
txtChapter.SelectionStart = M.Index + 3
txtChapter.SelectionLength = M.Value.Length - 7
txtChapter.SelectionColor = Color.Red
Next
Well... it does.... I doesn't color the tags, only the text in between
So removing them from the Regex works.
It doesn't work with the tags in the Regex, it colors the tags and text... only want the text
Thanks
-
Sep 1st, 2020, 04:25 PM
#13
Re: Coloring Text in Between Tags
It works until your text contains some similar text to the correct match, but not enclosed in tags. When that happens you’ll get an incorrect match. If that’s not a problem that is likely to happen, then no problem...
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Sep 1st, 2020, 04:26 PM
#14
Re: Coloring Text in Between Tags
If you read the code you quoted 2 posts before this, you’ll see the changes I made
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Sep 1st, 2020, 04:31 PM
#15
Thread Starter
Addicted Member
Re: Coloring Text in Between Tags
Originally Posted by .paul.
If you read the code you quoted 2 posts before this, you’ll see the changes I made
Ahh.... see it. That works perfectly... Thanks
-
Sep 2nd, 2020, 01:36 AM
#16
Re: Coloring Text in Between Tags
Originally Posted by pixelink
Well... it does.... I doesn't color the tags, only the text in between
So removing them from the Regex works.
It doesn't work with the tags in the Regex, it colors the tags and text... only want the text
Thanks
if you want to color the text between the tags no matter what is in there, then give this a try
Code:
Imports System.Text.RegularExpressions
Public Class Form1
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Mark(RichTextBox1, "\<(S)>(.*?)\</(S)>", Color.Red) 'color found items
Mark(RichTextBox1, "\<(.*?)\>", Color.Black) 'color the tags black
End Sub
Private Sub Mark(ByVal rtb As RichTextBox, ByVal word As String, ByVal color As Color)
Dim matchc As MatchCollection = Regex.Matches(rtb.Text.ToLower(), word.ToLower())
For Each m As Match In matchc
rtb.Select(m.Index, m.Length)
rtb.SelectionColor = color
rtb.Select(rtb.Text.Length, 0)
rtb.SelectionColor = Drawing.Color.Black
Next
End Sub
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
RichTextBox1.LoadFile("E:\Testsearch.txt", RichTextBoxStreamType.PlainText)
End Sub
End Class
here a Image
to hunt a species to extinction is not logical !
since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.
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
|