1

There are questions about converting "Perl regular expressions" (e.g., PCRE flavor) to .NET flavor (see e.g. .NET equivalent to Perl regular expressions) but isn't there a .NET library that would take a Perl regular expression as is and work with it directly? I.e., an alternative .NET RegEx library?

Community
  • 1
  • 1
Borek Bernard
  • 50,745
  • 59
  • 165
  • 240
  • 2
    It bears note that, in the question you link to, no conversion was necessary. The OP was asking basically "what do the `s/...//` and `m/.../` and `/.../` syntaxes mean in Perl?" The patterns themselves were identical between the two regex libraries. – ruakh Mar 05 '12 at 15:24
  • You can't run arbitrary Perl regular expression in anything else than Perl. You can use PCRE to get close tho. Using PCRE from .NET should not be impossible. – Qtax Mar 05 '12 at 15:39
  • I've updated the question to be more precise (I've used the term "Perl regular expressions" vaguely). Yes something like PCRE library for .NET would be great, can you suggest one? – Borek Bernard Mar 05 '12 at 17:24

2 Answers2

2

I know this answer comes really late but I thought you still may want to know...

I also wanted to be able to use PCRE regexes from .NET, and given that no easy solution was available, I thought it might be a good occasion to contribute something... So I started writing the PCRE.NET library.

At this stage it's not finished yet, but the most important features are there. Right now it passes a part of the PCRE test suite (roughly 1200 tests, it doesn't fail any attempted test but I didn't implement all test cases yet). I wouldn't recommend it for production use before it's done but it's stable enough for educational purposes.

Lucas Trzesniewski
  • 50,214
  • 11
  • 107
  • 158
  • 1
    Nice effort, it is much needed IMO. Good luck! – Borek Bernard Oct 27 '14 at 12:14
  • Well done Lucas, I'll be taking it out for a spin. :) – zx81 Oct 29 '15 at 03:18
  • @zx81 I'm glad to hear that. :) The lib is much more mature now than when I posted this answer, but I still left it at *v0.X* mailny because of the lack of any feedback. (oh and sorry for the lack of docs, that's my laziness at play ;) – Lucas Trzesniewski Oct 30 '15 at 11:03
  • @LucasTrzesniewski Docs are not a problem: this page has [the docs for most PCRE versions, the changelog and a curated list of additions](http://www.rexegg.com/pcre-documentation.html). I've been side-tracked but experimenting with your lib is on my todo list for this week. :) – zx81 Oct 30 '15 at 18:45
  • @zx81 yeah I guess anyone who would want to use such a lib must already be familiar with PCRE in some way ;) – Lucas Trzesniewski Oct 30 '15 at 19:59
  • @LucasTrzesniewski OK, tried it. :) Thought you'd like to know: I'm now [advertising PCRE.NET prominently](http://www.rexegg.com/regex-csharp.html#pcre.net) on this page (three spots in the text, then a dedicated section and sample program). Hopefully it will make it easy for anyone to get started. Will continue the convo on GitHub. – zx81 Oct 31 '15 at 22:00
0

PErl regular expressions will always have a broader feature set than most other regular expression libraries. However, if you keep your expressions on a somewhat sane level, i.e. not try to call code from inside them and actually try matching "regular" (as opposed to "context free") languages, the .NET and Perl features should be pretty much the same. Using C#'s raw string syntax (@"..."), you can even avoid the backslash duplication problem you have in other languages, such as Java or PHP.

Wormbo
  • 4,978
  • 2
  • 21
  • 41
  • Thanks but it doesn't really answer my question... Phalanger might be my hope - they are now spending more time converting PHP regexes to .NET ones than would probably take to implement the whole regexp engine so they may end up with their own one. – Borek Bernard Jun 04 '12 at 16:47
  • What kind of regexes are you working with that you need such very precise Perl support. PHP isn't that much closer to Perl than .NET, as far as I know. – Wormbo Jun 04 '12 at 17:02
  • PHP uses PCRE flavor, .NET does not. I am not interested in learning all the differences (there is quite a few of them, some are very subtle which probably makes things worse, not better) and that's why I'm looking for a PCRE-compatible regexp library for .NET. – Borek Bernard Jun 04 '12 at 20:49