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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T08:49:02+00:00 2026-06-08T08:49:02+00:00

I am getting problem in Regex expression. I want to get all URL(s) from

  • 0

I am getting problem in Regex expression.

I want to get all URL(s) from the given string but don’t want to get URL(s) which is end with .jpg, .css, .js, .gif, etc.

Here is my ASP.NET C# code,

    using (var client = new WebClient())
    {
        client.Headers[HttpRequestHeader.UserAgent] = "Mozilla/5.0 (Windows; U;   Windows NT 6.1; en-US; rv:1.9.2.13) Gecko/20101203 Firefox/3.6.13";
        string result = client.DownloadString(strBasicUrl);

        Regex MyRegex = new Regex("http(s)?://([\\w+?\\.\\w+])+([a-zA-Z0-9\\~\\!\\@\\#\\$\\%\\^\\&\\*\\(\\)_\\-\\=\\+\\\\\\/\\?\\.\\:\\;\\'\\,]*)?", RegexOptions.Multiline | RegexOptions.CultureInvariant | RegexOptions.Compiled);
        MatchCollection matches = MyRegex.Matches(result);
        foreach (var item in matches)
        {
            litResult.Text += item.ToString() + "<br>";
        }
    }

I want to change this Regex expression….

If I request strBasicUrl "http://www.Microsoft.com", 
then it should not be result below URLs
e.g.
http://i.microsoft.com/en-us/homepage/shared/templates/components/hpSearch/images/searchSprite.ltr.gif
http://i.microsoft.com/global/ImageStore/PublishingImages/Asset/Header/logo_skype.png

Can anybody help me in that, much appreciated.

Thanks in Advance,
Amit Prajapati

  • 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-08T08:49:04+00:00Added an answer on June 8, 2026 at 8:49 am

    I think Mike has already answered your question, but I was thinking on this ever since you asked the question, and thanks to your question, I learnt look aheads, look behinds and negative look behinds in regular expressions.

    So here is one alternative, if you don’t want to fire regular expression in a loop.

    public Regex MyRegex = new Regex(
      "href=\"(?<URL>(?:(?!javascript)(?!#))[a-zA-Z0-9\\~\\!\\@\\#\\$"+
      "\\%\\^\\&amp;\\*\\(\\)_\\-\\=\\+\\\\\\/\\?\\.\\:\\;\\'\\,]+)"+
      "(?<!(?:\\.png|\\.js|\\.jpg|\\.jpeg|\\.css|\\.gif|\\.zip|\\.r"+
      "ar))\"(?:$|>|\\s)",
    RegexOptions.Multiline
    | RegexOptions.CultureInvariant
    | RegexOptions.Compiled
    );
    

    For readability, here is the regex (without escape sequence):

    href="(?<URL>(?:(?!javascript)(?!#))[a-zA-Z0-9\~\!\@\#\$\%\^\&amp;\*\(\)_\-\=\+\\\/\?\.\:\;\'\,]+)(?<!(?:\.png|\.js|\.jpg|\.jpeg|\.css|\.gif|\.zip|\.rar))"(?:$|>|\s)
    

    Assuming you are developing a crawler, your regex is not matching the relative links, and when we match relative links you should not match the links which start with javascript or #(anchors).

    Here you can see, we are capturing named group the name of the group is “URL”. So to get the url part you need to use (you might be already aware):

    match.Groups["URL"]
    

    Here is the explanation of the regex:

    ///      href="
    ///  [URL]: A named capture group. [(?:(?!javascript)(?!#))[a-zA-Z0-9\~\!\@\#\$\%\^\&amp;\*\(\)_\-\=\+\\\/\?\.\:\;\'\,]+]
    ///      (?:(?!javascript)(?!#))[a-zA-Z0-9\~\!\@\#\$\%\^\&amp;\*\(\)_\-\=\+\\\/\?\.\:\;\'\,]+
    ///          Match expression but don't capture it. [(?!javascript)(?!#)]
    ///              (?!javascript)(?!#)
    ///                  Match if suffix is absent. [javascript]
    ///                      javascript
    ///                          javascript
    ///                  Match if suffix is absent. [#]
    ///                      #
    ///          Any character in this class: [a-zA-Z0-9\~\!\@\#\$\%\^\&amp;\*\(\)_\-\=\+\\\/\?\.\:\;\'\,], one or more repetitions
    ///  Match if prefix is absent. [(?:\.png|\.js|\.jpg|\.jpeg|\.css|\.gif|\.zip|\.rar)]
    ///      Match expression but don't capture it. [\.png|\.js|\.jpg|\.jpeg|\.css|\.gif|\.zip|\.rar]
    ///          Select from 8 alternatives
    ///              \.png
    ///                  Literal .
    ///                  png
    ///              \.js
    ///                  Literal .
    ///                  js
    ///              \.jpg
    ///                  Literal .
    ///                  jpg
    ///              \.jpeg
    ///                  Literal .
    ///                  jpeg
    ///              \.css
    ///                  Literal .
    ///                  css
    ///              \.gif
    ///                  Literal .
    ///                  gif
    ///              \.zip
    ///                  Literal .
    ///                  zip
    ///              \.rar
    ///                  Literal .
    ///                  rar
    ///  "
    ///  Match expression but don't capture it. [$|>|\s]
    ///      Select from 3 alternatives
    ///          End of line or string
    ///          >
    ///          Whitespace
    ///  
    

    This way you don’t need to run second regular expression in the loop. And you will get both absolute and relative url.

    Hope it helps…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

hi everybody i want solution for this regular expression, my problem is Extract all
need to find an expression for the following problem: String given = { \questionID\
I am doing a string parsing problem in PHP. I want a regular expression
I have a regular expression that is getting some information from a string. I
We upgrated Nhibernate to 3.2 and getting problem in following scenarios: Public properties which
I have a problem getting password_Reset_confirm bit working. url: (r'^password_reset/$', 'django.contrib.auth.views.password_reset'), (r'^password_reset_done/$', 'django.contrib.auth.views.password_reset_done'), (r'^password_reset_confirm/(?P<uidb36>[0-9A-Za-z]+)-(?P<token>.+)/$',
I have this regex for getting the YouTube video ID: (?<=v=)[a-zA-Z0-9-]+(?=&)|(?<=[0-9]/)[^&\n]+|(?<=v=)[^&\n]+ I get it
I'm having a problem getting my RegEx to work with my Ruby script. Here
I have a strange problem with reg expression. I'm trying to getting out the
i am getting the following regex from a json from server regexval = ^[A-Za-z\\'\\s\\.\\-\\,]{1,50}$

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.