Click here to Skip to main content
15,031,687 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Ive been trying to figure out a RegEx for the following:

XML
<td width="50%" height="19">
<a href="sample_weblink.htm">
Sample Weblink</a></td>
<td width="50%" height="19">



But I can't figure out the RegEx to match only the weblink. In other words I only want to match what's between:

<a href="
and
">

Any help would be appreciated.
The regex needs to be Global.

Thanks!
Posted

(?<=\<a href\=\")(.+)(?=\")
   
Comments
Sundeepan Sen 23-Sep-10 22:25pm
   
Thanks! I have tested this with http://gskinner.com/RegExr/ and it works. But when I use http://regexpal.com/ it does not work. I am wondering if this will work with C# .NET?
Try non-greedy evaluation, that is, add a ? after the + or *.

E.g.

C#
class Program

{

    static void Main(string[] args)
    {
        string strInput = @"
<td width=""50%"" height=""19"">
<a href=""sample_weblink.htm"">
Sample Weblink</a></td>
<td width=""50%"" height=""19"">
";
        var objMatch = Regex.Match(strInput, @"<a\s+href=[""'](.*?)[""']\s*>");
        if (objMatch.Success)
        {
            Console.WriteLine("Match: {0}", objMatch.Groups[1].Value);
        }
    }
}


Note: the doubling of the quotes is needed to have embedded quotes in a @"..." string.

Cheers

Andi
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900