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 7751539
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T11:34:28+00:00 2026-06-01T11:34:28+00:00

I am having an issue where I am using regex.Replace to replace part of

  • 0

I am having an issue where I am using regex.Replace to replace part of a string. The basic idea is that I want to capture the beginning of the string then replace the end of the string with a value from code. For an example pretend I have a string that says “Test Number ” followed by number and I want to increment that number. I capture the “Test Number ” but when I try to concatenate that capture with the new number it treats the capture ($1) as a literal and replaces the entire string with $1[new number].

Dim s as String = "We are on Test Number 1"
Dim newNumber as Integer = 2

s=Regex.Replace(s,"(Test Number )(\d)","$1" & newNumber)

This will output “We are on $12”. However, if I use a literal in the replace, the issue doesn’t happen.

Dim s as String = "We are on Test Number 1"

s=Regex.Replace(s,"(Test Number )(\d)","$1" & "2")

This will output “We are on Test Number 2”, as expected.

Does anyone have any thoughts on how I can use a variable in the replacement string portion of the Regex.Replace when including a captured group?

Thanks in advance.

  • 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-01T11:34:29+00:00Added an answer on June 1, 2026 at 11:34 am
    Dim s as String = "We are on Test Number 1"
    Dim newNumber as Integer = 2
    
    s=Regex.Replace(s,"(Test Number) (\d)","$1 " & newNumber)
    

    The problem is that when you concatenate, you end up specifying “$12” and of course there is no capture 12. In this case, we can take out the trailing space from “Test Number” and add it in the replacement, so our replacement string is “$1 2” and the capture replacement works as desired.

    A better, more generic solution, which can also be used when no space is present, is to use the Regex.Replace overload that accepts a MatchEvaluator:

    s = Regex.Replace(s, "(Test Number )(\d)",
       New MatchEvaluator(Function(m as Match)(m.Groups(1).Value & newNumber)))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am having a regex replace issue that i can't seem to figure out
I'm having a annoying issue with rewrite-using cherokee web-server. I want to convert: http://example.com/mypage.phtml?cmd=print
I think I'm having an issue since I'm using two key regex values in
Im having an issue using Java script to create a table that displays an
I'm having an issue using a new WPF app that is trying to display
I'm having some issues in doing a simple regex using sed. I've to do
I am having a issue using Google's SDC (Secure Data Connector) with my Google
I'm having an issue using the Cookie class of the Servlet API 2.5 on
I'm having an issue using the qTip plugin with jQuery. I've created a js
I believe I am having a memory issue using numpy arrays. The following code

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.