Rure is the Rust Regular Expression crate. This repo provides a set of bindings so that it can be used from .NET.
IronRure targets the .NET Standard runtime. It's available to install on NuGet:
> Install-Package IronRure
If you're targeting Linux, Windows or macOS then it's batteries included. If not then you'll need to supply your own compiled version of rure
.
Walkthroughs and example code to get to know the basic features of IronRure.
IronRure is open source. Pull requests are welcome. See the Contributing Guidelines and Code of Conduct for more information.
The simplest operation is to check if a pattern matches anywhere in a given string:
using IronRure;
var re = new Regex(@"\d+");
Assert.True(re.IsMatch("I have 1 number"));
Assert.False(re.IsMatch("I don't"));
All Rust regular expression patterns are unanchored by default. This means that if you want to check if a pattern matches the entire string you'll need to add ^
and $
yourself:
var re = new Regex(@"^\w+$");
Assert.True(re.IsMatch("word"));
Assert.False(re.IsMatch("two words"));
To find the extent of the next match in a string Regex::Find
can be used:
var re = new Regex(@"world");
var match = re.Find("hello world");
Assert.True(match.Matched);
Assert.Equal(6U, match.Start);
Assert.Equal(11U, match.End);
To get information about the extent of each capture group in a regex the Regex::Captures
method can be used. This method is slower than Regex::Find
or Regex::IsMatch
; only use it if you must retrieve capture information.
var dates = new Regex(@"(?P<day>\d{2})/(?P<month>\d{2})/(?P<year>\d{4})");
var haystack = "The first satellite was launched on 04/10/1957";
var caps = dates.Captures(haystack);
Assert.True(caps.Matched);
Assert.Equal("04", caps[dates["day"]].ExtractedString);
Assert.Equal("10", caps[dates["month"]].ExtractedString);
Assert.Equal("1957", caps[dates["year"]].ExtractedString);
In general IronRure out-performs .NET Regex, although milage may vary depending on the exact pattern and text. For more details checkout the benchmarks.
For more information about the pattern syntax see the underlying Rust crate documentation.
The windows build of IronRure-Batteries
requires the vc140
redistributable to work. This means you need to have Visual Studio 2015 or the Visual Studio 2015 C++ Runtime installed for it to load. If not you'll get an exception which claims that rure.dll
can't be found.