Regular expression-like pattern matching on IEnumerable<T> instead of string chars in C#

380 views Asked by At

I am trying to modify a list of CIL code instructions in C#. Common tasks include finding groups or patterns of instructions and replacing them with other instructions while reusing some of the information from the original match. That sounds a lot like regular expressions except that my use case is on lists of instances a specific type instead of characters in a string.

I can write some basic code to search and match stuff and replace it but I wonder if there is a more general approach with regexp syntax like “x?” or “(a|b)+” or “(a+)b*(c+)” but with a, b, c not being characters but instances of a class. Preferably with references to captured parts in the result expression. I am aware that certain patterns like ‘start of word’ or ‘whitespace’ would make no sense.

In my open source patch library Harmony, I represent the opcodes and their operands in a custom tuple type and the pattern matching should operate on an IEnumerable and in the end produce a new IEnumerable as a result.

Is there a generic pattern matching algorithm that I can extend to archive that?

0

There are 0 answers