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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T18:02:17+00:00 2026-05-23T18:02:17+00:00

NOTE : My problem is NOT that my links are not being replaced. But,

  • 0

NOTE : My problem is NOT that my links are not being replaced. But, it’s being NESTED.
eg, this is the comment

some string with www.google.com/blah/blah also something else www.google.com

by the time second string replace is done, part of first one is also valid (http://www.google.com/blah/blah) so it’s replacing that link twice.

I have a web app which lets users comment.
I am processing the input string and converting all links to html link format when I display it on the page. Original user input string stays in DB and nothing ever happens so it’s not corrupted over processing. Just when I show that on page, I do my function on it.

Now, this is the logic I am using to replace all links with their html formats

  1. Regex all links
  2. For each match, replace link with it’s html format version in the original string.
  3. Finally display string.

ex: www.google.com becomes <a href="http://www.google.com" target="_blank">www.google.com</a> just before it’s displayed on page.

This was working great until recently, one of my customer posted a content with two links from same domain.

the links were, say,

  1. http://www.google.com/images/blahblah
  2. http://www.google.com

My problem is, when the second time around, a string replace is done (I am using StringBuilder.Replace) the first link gets replaced as well!

so, firstly,

www.google.com/images/blahblah

becomes

<a href="http://www.google.com/images/blahblah" target="_blank">www.google.com/image/blahblah</a>

which is well. But the problem arises for second string replace, since replace is global, it does a replace on already processed link so the original (above) link becomes twisted as it sees http://www.google.com in there as well.

This is messing up so much that I actually get a mutilated abomination of a string.

How do I avoid this?

Does the Regex.Matches provide an index of matched element for me to work with? I couldn’t find it anywhere.

What’s the best way to deal with? any suggestions?

sorry for lengthy question.

I can prolly do this by manually traversing string but it’s long and painful there’s got to be a good way to do it…

edit adding extra info as someone asked:

My regex:

    string rPattern = @"(((http|ftp|https):\/\/)|www\.)[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&amp;:/~\+#!]*[\w\-\@?^=%&amp;/~\+#])?";

     Regex rLinks = new Regex(rPattern, RegexOptions.IgnoreCase);
     MatchCollection matches = rLinks.Matches(inputString);

then I am using

foreach(Match match in matches)
{
    if(match.value.StartsWith("www.youtube.com/watch"))
    {
         //logic to embed youtube video - this works fine.
    } 
}

//Here I replace all hyperlinks to their <a href> parts
  • 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-05-23T18:02:18+00:00Added an answer on May 23, 2026 at 6:02 pm

    Regex.Matches returns a MatchCollection. Match.Index Is what you’re looking for.

    string pattern = @"(https?://)?(?:www(?:\.\w+)+|(?:\w+\.)+(?:com|org|us|net|...))(/\w*)*"; // your pattern here.
    foreach (Match match in Regex.Matches (input, pattern))
    {
       // Use match.Index and match.Length;
    }
    

    But really, you’re probably looking for something more like this:

    string originalPost = 
       @"Ooh shiney: www.google.com/images/blahblah
       Look here: www.google.com";
    
    string html = Regex.Replace (
       originalPost, patternString, 
       "<a href='http://$1' target='_blank'>$1</a>");
    

    Or, you can use a matchEvaluator to do more advanced work (like ensure we don’t add a double http://.

    string html = Regex.Replace (
       originalPost, patternString, 
       m => 
          string.Format (
             "<a href='{0}{1}' target='_blank'>{1}</a>",
              m.Value.StartsWith ("http", StringComparison.IgnoreCase) ? "" : "http://",
              m.Value));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

[Note: There is another thread about this problem but it did not answer the
NOTE: I have solved the majority of this problem but have run into a
Has anyone seen this type of IE display problem? Example http://xs133.xs.to/xs133/08465/ie_problem910.jpg.xs.jpg Note that it
Note: this is a different problem to https - it's related to privacy security
NOTE: This is a solution for Project Euler Problem 14 . If you still
Not sure if the derived page is actually relevant to the problem here, but
I have such a basic problem in Delphi,I can't solve it. My Code: Note:DataR
Can anyone help with an odd Ruby Gem problem I'm having - note I've
NOTE: XMLIgnore is NOT the answer! OK, so following on from my question on
Note: This was posted when I was starting out C#. With 2014 knowledge, I

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.