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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T18:39:32+00:00 2026-05-14T18:39:32+00:00

I’ve written a regular expression that automatically detects URLs in free text that users

  • 0

I’ve written a regular expression that automatically detects URLs in free text that users enter. This is not such a simple task as it may seem at first. Jeff Atwood writes about it in his post.

His regular expression works, but needs extra code after detection is done.

I’ve managed to write a regular expression that does everything in a single go. This is how it looks like (I’ve broken it down into separate lines to make it more understandable what it does):

1   (?<outer>\()?
2   (?<scheme>http(?<secure>s)?://)?
3   (?<url>
4       (?(scheme)
5           (?:www\.)?
6           |
7           www\.
8       )
9       [a-z0-9]
10      (?(outer)
11          [-a-z0-9/+&@#/%?=~_()|!:,.;čšžćđ]+(?=\))
12          |
13          [-a-z0-9/+&@#/%?=~_()|!:,.;čšžćđ]+
14      )
15  )
16  (?<ending>(?(outer)\)))

As you may see, I’m using named capture groups (used later in Regex.Replace()) and I’ve also included some local characters (čšžćđ), that allow our localised URLs to be parsed as well. You can easily omit them if you’d like.

Anyway. Here’s what it does (referring to line numbers):

  • 1 – detects if URL starts with open braces (is contained inside braces) and stores it in “outer” named capture group
  • 2 – checks if it starts with URL scheme also detecting whether scheme is SSL or not
  • 3 – start parsing URL itself (will store it in “url” named capture group)
  • 4-8 – if statement that says: if “sheme” was present then www. part is optional, otherwise mandatory for a string to be a link (so this regular expression detects all strings that start with either http or www)
  • 9 – first character after http:// or www. should be either a letter or a number (this can be extended if you’d like to cover even more links, but I’ve decided not to because I can’t think of a link that would start with some obscure character)
  • 10-14 – if statement that says: if “outer” (braces) was present capture everything up to the last closing braces otherwise capture all
  • 15 – closes the named capture group for URL
  • 16 – if open braces were present, capture closing braces as well and store it in “ending” named capture group

First and last line used to have \s* in them as well, so user could also write open braces and put a space inside before pasting link.

Anyway. My code that does link replacement with actual anchor HTML elements looks exactly like this:

value = Regex.Replace(
    value,
    @"(?<outer>\()?(?<scheme>http(?<secure>s)?://)?(?<url>(?(scheme)(?:www\.)?|www\.)[a-z0-9](?(outer)[-a-z0-9/+&@#/%?=~_()|!:,.;čšžćđ]+(?=\))|[-a-z0-9/+&@#/%?=~_()|!:,.;čšžćđ]+))(?<ending>(?(outer)\)))",
    "${outer}<a href=\"http${secure}://${url}\">http${secure}://${url}</a>${ending}",
    RegexOptions.Compiled | RegexOptions.CultureInvariant | RegexOptions.IgnoreCase);

As you can see I’m using named capture groups to replace link with an Anchor tag:

"${outer}<a href=\"http${secure}://${url}\">http${secure}://${url}</a>${ending}"

I could as well omit the http(s) part in anchor display to make links look friendlier, but for now I decided not to.

Question

I would like my links to be replaced with shortenings as well. So when user copies a very long link (for instance if they would copy a link from google maps that usually generates long links) I would like to shorten the visible part of the anchor tag. Link would work, but visible part of an anchor tag would be shortened to some number of characters. I could as well append ellipsis at the end of at all possible (and make things even more perfect).

Does Regex.Replace() method support replacement notations so that I can still use a single call? Something similar as string.Format() method does when you’d like to format values in string format (decimals, dates etc…).

  • 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-14T18:39:33+00:00Added an answer on May 14, 2026 at 6:39 pm

    You can split ${url} to two capturing groups – urlhead, with the number of characters you want to display, and urltail with the rest. Here’s an example with 10 characters; this is somewhat simplfied to remove the condition, the last (?<ending>(?(outer)(?=\)))) should take care of that – it backtracks and captures the last ) when needed:

    (?<outer>(?<=\())?
    (?<scheme>http(?<secure>s)?://)?
    (?<url>
        (?(scheme)
            (?:www\.)?
            |
            www\.
        )
        [a-z0-9]
        [-a-z0-9/+&@#/%?=~_()|!:,.;čšžćđ]{1,10}
    )
    (?<urltail>[-a-z0-9/+&@#/%?=~_()|!:,.;čšžćđ]+)
    (?<ending>(?(outer)(?=\))))
    

    Note that I’ve also changes outer and ending to be lookarounds, so they are not captured and replaced. The replace string in this case looks like:

    <a href=\"http${secure}://${url}${urltail}\">http${secure}://${url}</a>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 417k
  • Answers 417k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The biggest problem you have to deal with is having… May 15, 2026 at 9:50 am
  • Editorial Team
    Editorial Team added an answer Catch WM_DEVICECHANGE from any window handle by registering for device… May 15, 2026 at 9:50 am
  • Editorial Team
    Editorial Team added an answer Using COM you can save a PPT as a PDF… May 15, 2026 at 9:50 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.