- The first Regular Expression Library on the Web!

Please support RegExLib Sponsors


Regular Expression Details

Title Test Find Pattern Title
I wrote up this regular expression to fetch the href attribute found in <a> tags as well as a few other HTML tags.
href="" | href="http://localhost/blah/" | href="eek"
href="" | href=eek | href="bad example"
Author Rating: The rating for this expression. Andrew Lee
Source Andrew Lee
Your Rating
Bad Good

Enter New Comment

Spammers suck - we apologize. Please enter the text shown below to enable your comment (not case sensitive - try as many times as you need to if the first ones are too hard):

Existing User Comments

Title: Dashes
Name: Matt S.
Date: 12/11/2008 3:21:53 PM
Really good, however I noticed through personal experience that it doesn't include URLs with "-" dashes in them. Still, good job!

Title: A HREF Locator: Follow
Name: Pierre-Dominique Vienne
Date: 11/16/2005 8:24:28 AM
Bloody shit!!! Here is the end... [-a-z0-9_:&=+,.!~*%$]+)?)*\/?(?:\?[-a-z0-9_:@=+,.!~*%$]*=[-a-z0-9_:@=+,.!~*%$]*(?:&[-a-z0-9_:@=+,.!~*%$]*=[-a-z0-9_:@=+,.!~*%$]*)*)?)\s*['"][^>]*>

Title: A HREF Locator: Follow
Name: Pierre-Dominique Vienne
Date: 11/16/2005 8:22:05 AM
Here is the regex which has been cut: <\s*a[^>]href\s*=\s*['"]\s*((?:(?:http:\/\/(?:[-a-z0-9]+\.)*(?:ac|ad|ae|aero|af|ag|ai|al|am|an|ao|aq|ar|arpa|as|at|au|aw|az|ba|bb|bd|be|bf|bg|bh|bi|biz|bj|bm|bn|bo|br|bs|bt|bv|bw|by|bz|ca|cc|cd|cf|cg|ch|ci|ck|cl|cm|cn|co|com|coop|cr|cu|cv|cx|cy|cz|de|dj|dk|dm|do|dz|ec|edu|ee|eg|er|es|et|eu|fi|fj|fk|fm|fo|fr|ga|gb|gd|ge|gf|gg|gh|gi|gl|gm|gn|gov|gp|gq|gr|gs|gt|gu|gw|gy|hk|hm|hn|hr|ht|hu|id|ie|il|im|in|info|int|io|iq|ir|is|it|je|jm|jo|jobs|jp|ke|kg|kh|ki|km|kn|kr|kw|ky|kz|la|lb|lc|li|lk|lr|ls|lt|lu|lv|ly|ma|mc|md|mg|mh|mil|mk|ml|mm|mn|mo|mobi|mp|mq|mr|ms|mt|mu|museum|mv|mw|mx|my|mz|na|name|nc|ne|net|nf|ng|ni|nl|no|np|nr|nu|nz|om|org|pa|pe|pf|pg|ph|pk|pl|pm|pn|pr|pro|ps|pt|pw|py|qa|re|ro|ru|rw|sa|sb|sc|sd|se|sg|sh|si|sj|sk|sl|sm|sn|so|sr|st|su|sv|sy|sz|tc|td|tf|tg|th|tj|tk|tl|tm|tn|to|tp|tr|travel|tt|tv|tw|tz|ua|ug|uk|um|us|uy|uz|va|vc|ve|vg|vi|vn|vu|wf|ws|ye|yt|yu|za|zm|zw))|\.\/|\/|(?:../)+)?[-a-z0-9_:@&=+,.!~*%$]*(?:\/[-a-z0-9_:@&=+,.!~*%$]+(?:\.

Title: A HREF Locator
Name: Pierre-Dominique Vienne
Date: 11/16/2005 8:20:53 AM
The Andrew Lee's regex is quite good... but also quite limited. It won't match these: - relative links which begin by '../' - A HREF with spaces/tabs into it (not in URL) - URL with % (and other strange characters that should not be in URL (but could be, like '.' for directories) It will match any other href="[...]" like <link href='style.css' type='text/css'>. We don't want this to be done. I also added a test on the top level DNS if the 'http://' string is submitted. The DNS List cames from here: Here is my modified regex which could appear quite strong to understand. Take time, it's not a so hard work. <\s*a[^>]href\s*=\s*['"]\s*((?:(?:http:\/\/(?:[-a-z0-9]+\.)*(?:ac|ad|ae|aero|af|ag|ai|al|am|an|ao|aq|ar|arpa|as|at|au|aw|az|ba|bb|bd|be|bf|bg|bh|bi|biz|bj|bm|bn|bo|br|bs|bt|bv|bw|by|bz|ca|cc|cd|cf|cg|ch|ci|ck|cl|cm|cn|co|com|coop|cr|cu|cv|cx|cy|cz|de|dj|dk|dm|do|dz|ec|edu|ee|eg|er|es|et|eu|fi|fj|fk|fm|fo|fr|ga|gb|g

Copyright © 2001-2021, | ASP.NET Tutorials