Matching Backlinks and Anchor Text with a Regular Expression
Posted on | October 6, 2008 |
Hi Guys,
If anyone reads this, well done you. You’re about to become witness to my first ever published regular expression, so go easy on the critique!
I needed this to show what pages were linking into other pages (backlinks) and what was the associated anchor text used by the link. I spent a morning or so playing around with the following regular expression to try and match all backlinks to a specific URL and its anchor text.
<a [^/>]*href=["\'](your-url-here).*["\'][^/>]*/?>(.*)
This does the job very nicely, I’m sure it could be speeded up slightly to avoid being overtly greedy. But for now I’m happy with it.
Notes
- You must quote the URL you use. There is a way in ExtJS to do this and PHP.
- Ext.escapeRe() and preg_quote() in PHP should get the job done!
Comments
One Response to “Matching Backlinks and Anchor Text with a Regular Expression”
Leave a Reply
October 29th, 2008 @ 8:44 am
Good for people to know.