Answered Linq to XML in visual basic

  • Thursday, October 30, 2008 12:52 PM
     
     
    I'm wondering if its legal to add a static regex statement in the select statement of a linq query. Also is there an example and what the syntax for doing it?

    To strip <a> tags from HTML and return just a portion of the tag to my Generic.IEnumerable(Of String) list.


    Dim links = From link In Mytabledata...<a> _
                         Where InStr(link.@href, "[AC35]") > 0 _
                          Select ( get text between "code =" and "&" in a regex static statement from link.value)


    I could only find a couple of examples of comparing matches. None of using static regex calls to format the returned list in line. Can you only use matches?

    The one thing that I did find why searching around was a regex to linq project by the guy who wrote Regulator.

    http://weblogs.asp.net/rosherove/archive/2008/05/06/introducing-linq-to-regex.aspx







All Replies

  • Thursday, October 30, 2008 2:11 PM
     
     

    Does it have to be a static Match call? Here is an example with a Match call on a Regex instance:

    Code Snippet

            Dim pattern As New Regex("code=([^&]*?)&")
            Dim example As XElement = _
            <root>
                <a>foo code=bar&amp;whatever=baz</a>
            </root>
            Dim query As IEnumerable(Of String) = _
            From a In example...<a> _
            Select pattern.Match(a.Value).Groups()(1).Value

            For Each s As String In query
                Console.WriteLine(s)
            Next

     

     

    If you really want a static call:

    Code Snippet

            Dim pattern As String = "code=([^&]*?)&"
            Dim example As XElement = _
            <root>
                <a>foo code=bar&amp;whatever=baz</a>
            </root>
            Dim query As IEnumerable(Of String) = _
            From a In example...<a> _
            Select Regex.Match(a.Value, pattern).Groups()(1).Value

            For Each s As String In query
                Console.WriteLine(s)
            Next

     

    The first example should be more efficient.

     

    If the sample does not help then please provide your sample XML you want to process.

  • Thursday, October 30, 2008 6:41 PM
     
     
    Thank you very much, those are two great examples. The only problem I have now is how to change the Select so that if there is no match, do not to put a empty string into the resulting list. Just duplicated the Select in the Where clause for the moment, but there should be a more elegant way to only select if there is a positive result of the regex.


    Your example was far more readable and understandable than the Microsoft one at:

    http://msdn.microsoft.com/en-us/library/bb882639.aspx






  • Friday, October 31, 2008 12:37 PM
     
     Answered

    You can use a Let clause to store the Match result and then check Success in the Where clause:

    Code Snippet

            Dim pattern As New Regex("code=([^&]*?)&")
            Dim example As XElement = _
            <root>
                <a>foobar</a>
                <a>foo code=bar&amp;whatever=baz</a>
                <a>bar code=foo&amp;baz=whatever</a>
            </root>
            Dim query As IEnumerable(Of String) = _
            From a In example...<a> _
            Let m = pattern.Match(a.Value) _
            Where m.Success _
            Select m.Groups()(1).Value

            For Each s As String In query
                Console.WriteLine(s)
            Next

     

     

  • Saturday, November 01, 2008 1:52 PM
     
     
    Thanks thats exactly what I was looking for.