Follow @RoyOsherove on Twitter

Introducing LINQ To Regex

When I first saw Josh Flanagan's Readable Fluent Regex Api, I loved it at first sight. Now I'm working on a demo for the upcoming DevTeach in Toronto, where I'll present how to build custom LINQ providers to various things.

I thought it would be interesting to see if using the fluent Regex API Josh created along with a LINQ query syntax would "work" for me, so I set out to create LINQ to Regex.

a couple of hours later, I like it pretty much :)

Here's what you can do with it:


        public void FindEmailUsingPattern()


            var query = from match in


                        where match.Word.Repeat.AtLeast(1)








                        select match;

            foreach (var match in query)





you use the Regex.Query.Against(string), and you use the fluent API in the "where" part. but the results you get back are Regular Expression "Match" objects that give you the actual findings of instances that have passes the given criteria along with other data such as their index and such. it also works with grouping and other cool things done in the API.

the main work was to be able to parse the full expression tree that is created by the big "where" clause, and translate that into calls against, again, the fluent API classes Josh created. I also needed to add an "IsTrue()" method at the end of the where clause so that the lambda expression can be accepted by the compiler. it always returns true..


You can also download both the source and the binaries from the assembla homepage of this project.

Future possible steps:

  • make the underlying regex query a compiled query (run faster)
  • some API tweaks perhaps?


How you can take part in this project:

I've put the sources on assembla:  and if you're interested in contributing to the project send me an email to Roy at and I'll add you.

NullObject.For<T> - As simple as it gets, but no simpler

Addy Santo Blogs again