Results 1 to 16 of 16

Thread: [RESOLVED] Coloring Text in Between Tags

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Apr 2019
    Location
    Sabattus
    Posts
    234

    Resolved [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.
    Can't Type - Forgetful - Had Stroke = Forgive this old man!
    Website: https://pixelinkmedia.wixsite.com/frankhilton
    VSCOMM 2022 • LAZARUS 2.2.0 • Win10 • 16G RAM • Nvida GForce RTX 2060

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    Re: Coloring Text in Between Tags

    Try this...

    Code:
    Dim MyRegex As New Regex("/<S/>/w{1}/d+/<//S/>")

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Apr 2019
    Location
    Sabattus
    Posts
    234

    Re: Coloring Text in Between Tags

    Quote Originally Posted by .paul. View Post
    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
    Can't Type - Forgetful - Had Stroke = Forgive this old man!
    Website: https://pixelinkmedia.wixsite.com/frankhilton
    VSCOMM 2022 • LAZARUS 2.2.0 • Win10 • 16G RAM • Nvida GForce RTX 2060

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Apr 2019
    Location
    Sabattus
    Posts
    234

    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?
    Can't Type - Forgetful - Had Stroke = Forgive this old man!
    Website: https://pixelinkmedia.wixsite.com/frankhilton
    VSCOMM 2022 • LAZARUS 2.2.0 • Win10 • 16G RAM • Nvida GForce RTX 2060

  5. #5
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    Re: Coloring Text in Between Tags

    This is the pattern. I just tested it... "<S>[HG]{1}\d+</S>"

  6. #6
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    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)

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Apr 2019
    Location
    Sabattus
    Posts
    234

    Re: Coloring Text in Between Tags

    Quote Originally Posted by .paul. View Post
    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
    Can't Type - Forgetful - Had Stroke = Forgive this old man!
    Website: https://pixelinkmedia.wixsite.com/frankhilton
    VSCOMM 2022 • LAZARUS 2.2.0 • Win10 • 16G RAM • Nvida GForce RTX 2060

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Apr 2019
    Location
    Sabattus
    Posts
    234

    Re: Coloring Text in Between Tags

    Quote Originally Posted by .paul. View Post
    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.
    Can't Type - Forgetful - Had Stroke = Forgive this old man!
    Website: https://pixelinkmedia.wixsite.com/frankhilton
    VSCOMM 2022 • LAZARUS 2.2.0 • Win10 • 16G RAM • Nvida GForce RTX 2060

  9. #9
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    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...

  10. #10

    Thread Starter
    Addicted Member
    Join Date
    Apr 2019
    Location
    Sabattus
    Posts
    234

    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
    Can't Type - Forgetful - Had Stroke = Forgive this old man!
    Website: https://pixelinkmedia.wixsite.com/frankhilton
    VSCOMM 2022 • LAZARUS 2.2.0 • Win10 • 16G RAM • Nvida GForce RTX 2060

  11. #11
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    Re: Coloring Text in Between Tags

    Quote Originally Posted by pixelink View Post
    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

  12. #12

    Thread Starter
    Addicted Member
    Join Date
    Apr 2019
    Location
    Sabattus
    Posts
    234

    Re: Coloring Text in Between Tags

    Quote Originally Posted by .paul. View Post
    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
    Can't Type - Forgetful - Had Stroke = Forgive this old man!
    Website: https://pixelinkmedia.wixsite.com/frankhilton
    VSCOMM 2022 • LAZARUS 2.2.0 • Win10 • 16G RAM • Nvida GForce RTX 2060

  13. #13
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    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...

  14. #14
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    Re: Coloring Text in Between Tags

    If you read the code you quoted 2 posts before this, you’ll see the changes I made

  15. #15

    Thread Starter
    Addicted Member
    Join Date
    Apr 2019
    Location
    Sabattus
    Posts
    234

    Re: Coloring Text in Between Tags

    Quote Originally Posted by .paul. View Post
    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
    Can't Type - Forgetful - Had Stroke = Forgive this old man!
    Website: https://pixelinkmedia.wixsite.com/frankhilton
    VSCOMM 2022 • LAZARUS 2.2.0 • Win10 • 16G RAM • Nvida GForce RTX 2060

  16. #16
    PowerPoster ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    3,048

    Re: Coloring Text in Between Tags

    Quote Originally Posted by pixelink View Post
    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
    Name:  Regexpixel.jpg
Views: 118
Size:  30.6 KB
    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
  •  



Click Here to Expand Forum to Full Width