Svideo (21) [Avatar] Offline
#1
I have only found two examples of regex statements in a select statement, and they didn't pick a easy one at that. I don't want to compare matches but select a segment of text to return.

http://msdn.microsoft.com/en-us/library/bb882639.aspx
http://schotime.net/blog/index.php/2008/03/10/linq-and-regular-expressions/

Are there any simple examples of static regex statements in the linq select statements? I would like to get a string between two strings. A portion of the href is really where I'm going.

The only other thing I see is something cool but it didn't help me at the moment and that is the guy that wrote Regulator (Roy Osherive) has a Linq to Regex project that might be very cool.

http://weblogs.asp.net/rosherove/archive/2008/05/06/introducing-linq-to-regex.aspx
jwooley (123) [Avatar] Offline
#2
Re: Regex static statements in a Select statemen in Visual Basic
First, I assume you are working against an in memory structure/XML. I've used regex in the where clause of a LINQ to Objects query. It won't translate to LINQ to SQL, however there is special handling in the final projection that may allow it there.

In this case, I suspect you're running into the issue of the RegEx.Matches returns a list of matches rather than a single match value. Thus a LINQ query would potentially return an IEnumerable(Of Matches) thus a collection of collections. You could possibly use Regex.Matches("filter", source)(0). Alternatively, Matches may offer FirstOrDefault. Give it another try and feel free to show us some code so we can see if we can spot anything.

You may get better results posting to the LINQ form on MSDN as this forum is intended to support questions from the book.

Jim