Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9045037
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T11:08:56+00:00 2026-06-16T11:08:56+00:00

I am trying to find words and replace them using regex. I keep getting

  • 0

I am trying to find words and replace them using regex. I keep getting a stack overflow exception, I am geussing is due to a recursive loop. So I tried removing the for loop from the first block of code, and came up with the second block of code and still the same issue.

I am trying to find certain strings while ignoring case and automatically replace them with the right case of the same string. So an example would be that someone types in “vB” it would automatically replace it with “vb”. I know my issue has to due with being in the textchanged event so if someone could guide me in the right direction I would be very thankful.

Private Sub tb_textchanged(ByVal sender As System.Object, ByVal e As    TextChangedEventArgs)

    Dim pattern As String = "\<vb\>"
    Dim input As String = txt.Text
    For Each m As Match In Regex.Matches(input, pattern, RegexOptions.IgnoreCase)
        Dim caretpos As FastColoredTextBoxNS.Place = New Place(txt.Selection.Start.iChar, txt.Selection.Start.iLine)
        Dim replacement As String = "<vb>"
        Dim rgx As New Regex(pattern)
        Dim result As String = rgx.Replace(input, replacement, RegexOptions.IgnoreCase)
        txt.Text = result
        txt.Selection.Start = New Place(caretpos.iChar, caretpos.iLine)
    Next

End Sub

After replacing the For loop.

Private Sub tb_textchanged(ByVal sender As System.Object, ByVal e As TextChangedEventArgs)

    Dim pattern As String = "\<vb\>"
    Dim input As String = txt.Text
    Dim matches As MatchCollection = Regex.Matches(input, pattern, RegexOptions.IgnoreCase)
    If matches.Count > 0 Then
        Dim caretpos As FastColoredTextBoxNS.Place = New Place(txt.Selection.Start.iChar, txt.Selection.Start.iLine)
        Dim replacement As String = "<vb>"
        Dim rgx As New Regex(pattern)
        Dim result As String = rgx.Replace(input, replacement, RegexOptions.IgnoreCase)
        txt.Text = result
        txt.Selection.Start = New Place(caretpos.iChar, caretpos.iLine)
    End If

End Sub
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T11:08:58+00:00Added an answer on June 16, 2026 at 11:08 am
    Private isRecursive As Boolean
    Private Sub tb_textchanged(ByVal sender As System.Object, ByVal e As TextChangedEventArgs)
    
        If (isRecursive) Then
            Return
        End If
        isRecursive = True
    
        Dim pattern As String = "\<vb\>"
        Dim input As String = txt.Text
        Dim matches As MatchCollection = Regex.Matches(input, pattern, RegexOptions.IgnoreCase)
        If matches.Count > 0 Then
            Dim caretpos As FastColoredTextBoxNS.Place = New Place(txt.Selection.Start.iChar, txt.Selection.Start.iLine)
            Dim replacement As String = "<vb>"
            Dim rgx As New Regex(pattern)
            Dim result As String = rgx.Replace(input, replacement, RegexOptions.IgnoreCase)
            txt.Text = result
            txt.Selection.Start = New Place(caretpos.iChar, caretpos.iLine)
        End If
    
        isRecursive = False
    End Sub
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to find and replace a couple of words in a file
I am trying to find similar strings in feedback option texts using regex to
I am trying to find and replace line breaks in text using javascript. The
I'm trying to find all words with a hash in front if they are
I'm trying to find all element that contains all words in a phrase after
I'm trying to do a find and replace in an OpenXML word document which
I'm trying to find a block of text and replace it with a modified
I'm trying to use Function to define a recursive definition using a measure, and
Trying to replace certain words in HTML pages with the same word but as
I'm trying to do a find-and-replace algorithm across a huge batch of .doc files

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.