Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is PCRE-compatible syntax? And is C# PCRE-compatible?

Tags:

c#

regex

What is PCRE-compatible syntax? And is C# PCRE-compatible? From wikipedia I found this:

Perl Compatible Regular Expressions (PCRE) is a regular expression C library inspired by the regular expression capabilities in the Perl programming language, written by Philip Hazel, starting in summer 1997. PCRE's syntax is much more powerful and flexible than either of the POSIX regular expression flavors and many classic regular expression libraries. The name is misleading, because PCRE and Perl each have capabilities not shared by the other.

Source

like image 747
Mohamad Shiralizadeh Avatar asked Oct 22 '14 09:10

Mohamad Shiralizadeh


People also ask

What is PCRE matching?

PCRE tries to match Perl syntax and semantics as closely as it can. PCRE also supports some alternative regular expression syntax (which does not conflict with the Perl syntax) in order to provide some compatibility with regular expressions in Python, . NET, and Oniguruma.

What PCRE Perl Compatible Regular Expressions matching does?

The PCRE library is a set of functions that implement regular expression pattern matching using the same syntax and semantics as Perl 5. PCRE has its own native API, as well as a set of wrapper functions that correspond to the POSIX regular expression API.

What is PCRE for?

PCRE (Perl Compatible Regular Expressions) is a C library implementing regex. It was written in 1997 when Perl was the de-facto choice for complex text processing tasks. The syntax for patterns used in PCRE closely resembles Perl. PCRE syntax is being used in many big projects including PHP, Apache, R to name a few.

Is Python a PCRE?

Python includes pcre support. Many command line utils etc. have a flag where they accept pcre patterns.


1 Answers

C# regexes share some syntax with PCRE regexes. Most of the features overlap but both libraries keep their own specifics:

A couple examples:

PCRE

  • Supports recursion
  • Supports backtrack control verbs
  • Supports constructs like (?(DEFINE) ... )
  • Supports more options
  • Offers a DFA parsing mode
  • Supports partial matches
  • Supports \K
  • Supports X++ shorthand syntax (equivalent of (?>X+))

.NET

  • Supports capture stacks and duplicate named groups
  • Supports balancing groups
  • Supports variable length lookbehind

This list is not exhaustive. You can compare both flavours on this page and the sibling pages.

Given the differences, I wanted to be able to use PCRE regexes from .NET and recently started PCRE.NET, which is a wrapper project. It's not finished yet but is starting to be usable.

like image 54
Lucas Trzesniewski Avatar answered Oct 18 '22 20:10

Lucas Trzesniewski