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:
[Test]
public void FindEmailUsingPattern()
{
var query = from match in
RegexQuery.Against("sdlfjsfl43r3490r98*(*Email@somewhere.com_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@somewhere.com",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 along with tests and the readable regex library port I did
- Binaries in release for the project
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: http://www.assembla.com/spaces/LinqToRegex/ and if you're interested in contributing to the project send me an email to Roy at osherove.com and I'll add you.