Solved

Regular Expression

Posted on 2008-06-26
4
247 Views
Last Modified: 2010-04-15
Hi

Any body know a nice regular expression that matches and gets <a> tags

IE if i have a document i would like to extract all the <a> assuming that the <a> has an ending tag (</a>)

Thanks

Allan
0
Comment
Question by:acadenilla
  • 2
4 Comments
 
LVL 6

Expert Comment

by:Bruce_1975
Comment Utility
<(?<a>\w*)>(?<text>.*)</\k<a>>

Regards,
Bruce
0
 

Author Comment

by:acadenilla
Comment Utility
bruce

I fails when i tried a simple link

<a href='asdfasdf.com'>first tag text</a>

could you explain to me the expression

I might need to handle some crazy link ie

<a id='asdfas' onmouseclick='asdfasdf' href='asdfasdf'><font><b>asdfasdfas</b><font></a>

or

<a href='aasdfasdf'><img></img></a>

thanks
0
 
LVL 62

Accepted Solution

by:
Fernando Soto earned 250 total points
Comment Utility
Hi acadenilla;

This pattern will give you what you want.

' Test Data in a file
Dim sr As New StreamReader("HtmlData.htm")
' Read the data into a string
Dim input As String = sr.ReadToEnd()
' Find all the Matches for the pattern "<a.*?/a>"
Dim mc As MatchCollection = Regex.Matches(input, "<a.*?/a>")
For Each m As Match In mc
    ' Display the result in the output window of the IDE
    Console.WriteLine(m.Value)
Next


Fernando
0
 
LVL 6

Expert Comment

by:Bruce_1975
Comment Utility
Just leave away the ?<text> and use

<(?<a>\w*)>(.*)</\k<a>>

<(?<a>\w*)> check for <a followed by any alphanummeric value, hast to close with >
(.*)                any number or character is allowed, any number of repetition
</\k<a>>       has to end with </a>
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now