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

  • Home
  • SEARCH
  • 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 7551623
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T10:32:40+00:00 2026-05-30T10:32:40+00:00

Team: I need some help with some regular expressions. The goal is to be

  • 0

Team:

I need some help with some regular expressions. The goal is to be able to identify three different ways that users might express links in a note, and those are as follows.

<a href="http://www.msn.com">MSN</a>

possibilities

    http://www.msn.com     OR
    https://www.msn.com    OR
    www.msn.com

Then by being able to find them I can change each one of them to real A tags as necessary. I realize the first example is already an A tag but I need to add some attributes to it specific to our application — such as TARGET and ONCLICK.

Now, I have regular expressions that can find each one of those individually, and those are as follows, respective to the examples above.

<a?\w+((\s+\w+(\s*=\s*(?:".*?"|'.*?'|[^'">\s]+))?)+\s*)/?>
(http|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&amp;:/~\+#]*[\w\-\@?^=%&amp;/~\+#])?
[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&amp;:/~\+#]*[\w\-\@?^=%&amp;/~\+#])?

But the problem is that I can’t run all of them on the string because the second one will match a part of the first one and the third one will match a part of both the first and second. At any rate — I need to be able to find the three permutations distinctly so I can replace each one of them individually — because the third expression for example will need http:// added to it.

I look forward to everybodys assistance!

  • 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-30T10:32:41+00:00Added an answer on May 30, 2026 at 10:32 am

    Assuming that the link starts or ends either with a space or at beginnd/end of line (or inside an existing A tag) I came up with the following code, which also includes some sample texts:

    string regexPattern = "((?:<a (?:.*?)href=\")|^|\\s)((?:http[s]?://)?(?:\\S+)(?:\\.(?:\\S+?))+?)((?:\"(?:.*?)>(.*?)</a>)|\\s|$)";
    string[] examples = new string[] {
        "some text <a href=\"http://www.msn.com/path/file?page=some.page&subpage=9#jump\">MSN</a>  more text",
        "some text http://www.msn.com/path/file?page=some.page&subpage=9#jump more text",
        "some text http://www.msn.com/path/file?page=some.page&subpage=9#jump more text",
        "some text https://www.msn.com/path/file?page=some.page&subpage=9#jump more text",
        "some text www.msn.com/path/file?page=some.page&subpage=9#jump",
        "www.msn.com/path/file?page=some.page&subpage=9#jump more text"
    };
    
    Regex re = new Regex(regexPattern);
    foreach (string s in examples) {
        MatchCollection mc = re.Matches(s);
        foreach (Match m in mc) {
            string prePart = m.Groups[1].Value;
            string actualLink = m.Groups[2].Value;
            string postPart = m.Groups[3].Value;
            string linkText = m.Groups[4].Value;
            MessageBox.Show(" prePart: '" + prePart + "'\n actualLink: '" + actualLink + "'\n postPart: '" + postPart + "'\n linkText: '" + linkText + "'");
        }
    }
    

    As this code uses groups with numbers it should be possible to use the regular expression in JavaScript too.

    Depending on what you need to do with the existing A tag you need to parse the particular first group as well.

    Update:
    Modified the regex as requested so that the link Text becomes group no. 4

    Update 2:
    To better catch malformed links you might try this modified version:

    pattern = "((?:<a (?:.*?)href=\"?)|^|\\s)((?:http[s]?://)?(?:\\S+)(?:\.(?:[^>\"\\s]+))+)((?:\"?(?:.*?)>(.*?)</a>)|\\s|$)";
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Need some help gathering thoughts on this issue. Our team is moving ahead with
I need some help I have been scouring the web and haven't been able
I need some help with a rails development that I'm working on, using rails
I need to involve some markup and javascript people to help development team on
I'm a relative sql notice, eager to learn, but I need some help putting
Could someone offer up some advice or point out some blogs/articles that could help
I need some help with an RSS feed I'm working on. This is the
Multi-Test TestNG.xml file results in java.lang.NullPointerException Hello Quality Team, I need help getting TestNG
Or they (team members) need someone to keep pushing? Edit: The above line was
I need to reproduce a bug, and a guy from the other team has

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.