---
title: "Introducing LINQ To Regex"
url: "https://osherove.com/blog/2008/5/6/introducing-linq-to-regex.md"
canonical: "https://osherove.com/blog/2008/5/6/introducing-linq-to-regex.html"
published: "2008-05-06T17:02:25"
updated: "2008-05-06T17:02:25"
tags: [".NET", "General Software Devel..."]
author: "Roy Osherove"
description: "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…"
usage: "Licensed under CC BY 4.0 — reuse, quote and adapt with attribution (Roy Osherove, link to the original page)."
license: "https://creativecommons.org/licenses/by/4.0/"
---

# Introducing LINQ To Regex

By Roy Osherove May 6, 2008 In [.NET](https://osherove.com/blog/category/net), [General Software Devel...](https://osherove.com/blog/category/general-software-devel)

**Historical article** · Originally published May 6, 2008; technical details may be outdated.

When I first saw [Josh Flanagan's Readable Fluent Regex Api](http://flimflan.com/blog/ReadableRegularExpressions.aspx), I loved it at first sight. Now I'm working on a demo for the [upcoming DevTeach in Toronto](http://www.devteach.com/), 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:**

  \[Test\]

        public void FindEmailUsingPattern()

        {

            var query \= from match in

               RegexQuery.Against("sdlfjsfl43r3490r98\*(\*\[email removed\]\_dakj3j")

                        where match.Word.Repeat.AtLeast(1)

                            .Literal("@")

                            .Word.Repeat.AtLeast(1)

                            .Literal(".")

                            .Choice.Either(

                                 Pattern.With.Literal("com"),

                                 Pattern.With.Literal("net"))

                            .IsTrue()

                        select match;

            foreach (var match in query)

            {

                Assert.AreEqual("\[email removed\]",match.Value);

            }

        }

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..

**Downloads:**

- [Source](http://www.assembla.com/spaces/LinqToRegex/documents/amJ-2sg8yr3znfab7jnrAJ/download/LinqToRegex0.1.src.ZIP) along with tests and the readable regex library port I did
- [Binaries in release](http://www.assembla.com/spaces/LinqToRegex/documents/dZ9ytQg8ur3yGPab7jnrAJ/download/LinqToRegex0.1.Bin.ZIP) for the project

You can also download both the source and the binaries [from the assembla homepage of this project.](http://www.assembla.com/spaces/LinqToRegex/)

**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: [http://www.assembla.com/spaces/LinqToRegex/](http://www.assembla.com/spaces/LinqToRegex/ "http://www.assembla.com/spaces/LinqToRegex/")  and if you're interested in contributing to the project send me an email to \[email removed\] and I'll add you.

## More in .NET

[All posts in .NET](https://osherove.com/blog/category/net)

- May 15, 2008

    ### [Typemock Isolator 4.2.4 is out](https://osherove.com/blog/2008/5/15/typemock-isolator-424-is-out.html)

- May 10, 2008

    ### [NullObject.For<T> - As simple as it gets, but no simpler](https://osherove.com/blog/2008/5/10/nullobjectforlttgt-as-simple-as-it-gets-but-no-simpler.html)

- May 1, 2008

    ### [Please don't go (work for MS or Google)](https://osherove.com/blog/2008/5/1/please-dont-go-work-for-ms-or-google.html)

- Apr 29, 2008

    ### [Don't miss out on Spec#](https://osherove.com/blog/2008/4/29/dont-miss-out-on-spec.html)


---

## About the author

**Roy Osherove**: Senior Solutions Architect at AWS working on AI for production and AI-driven development; author of The Art of Unit Testing and Elastic Leadership, with about 20 years of consulting and training on unit testing, TDD, team leadership and continuous delivery.

### Sites

- [RobotPaper](https://robotpaper.ai/): This blog: AI patterns and anti-patterns, AI and coding agents, and engineering leadership.
- [osherove.com](https://osherove.com): Roy's personal site. Author of The Art of Unit Testing and Elastic Leadership; consulting and training on unit testing, TDD, team leadership and continuous delivery, plus about 20 years of blog posts.
- [The Art of Unit Testing](https://www.artofunittesting.com): The Art of Unit Testing, 3rd Edition (Manning): readable, maintainable, trustworthy unit tests, plus TDD workshops in C#, JavaScript, Java and Kotlin.
- [5whys.com](https://www.5whys.com): Roy's writing on software team leadership: self-organizing teams, surviving chaos, learning mode and the Elastic Leadership model.
- [Pipeline Driven](https://pipelinedriven.org): Continuous delivery and DevOps practices: build patterns, pipeline-friendly testing, DevOps metrics and enterprise DevOps.
- [Elastic Leadership](https://www.elasticleadership.com): Elastic Leadership, Roy's framework for software team leaders (survival, learning and self-organizing modes).

### Social

- X: https://x.com/royosherove
- LinkedIn: https://www.linkedin.com/in/osherove/

### Contact

- Contact form: https://forms.gle/jQSx5KoXvrVA1JCK7

### License

Licensed under CC BY 4.0 — reuse, quote and adapt with attribution (Roy Osherove, link to the original page). License: https://creativecommons.org/licenses/by/4.0/
