Skip to content

kobi/RecreationalRegex

Repository files navigation

What's this?

Regular expressions that do things!
.NET has a unique regular expression engine which captures strings into a stack and allow full access to it during the match using balancing groups.

How to use?

The easiest option is to run dotnet test, this will run all existing tests and write output to console.

So what's in there?

Maze-solving regex

Source: Mazes.cs
Blog post: Solving mazes using regular expressions

Implement PCRE recursive grammar in .NET regex balancing groups

Source: PcreGrammar.cs
For a Stack Overflow answer - which took me 3 years to figure out. I ended up writing code to generate a regex from an intermediate stack-based grammar. fun stuff: Converting PCRE recursive regex pattern to .NET balancing groups definition
How can we write this grammar using .NET's balancing groups?

Q -> \w | '[' A ';' Q* ','? Q* ']' | '<' A '>'
A -> (Q | ',')*
// match A

Rectangle puzzle solver

Source: SmallRectangles.cs Blog post: Filling a large rectangle with smaller rectangles using regular expressions

Regex to find comments in regex

Yep
Source: StackOverflowAsnwers.cs - FindRegexComments Stack Overflow answer: How to extract regex comment?

Match the Fibonacci sequence

O NO BAD REGEX MATCHING OBJECTIONABLE HUMUHUMUNUKUNUKUAPUAA
Source: Fibonacci.cs
Blog post: Match Words in Fibonacci Lengths
Random thought: If we're only matching the same character (x xx xxx xxxxx...), it's possible to write self-referencing capturing groups to match these - see Stack Overflow post by polygenelubricants.

Numeric

Match mixed balanced parentheses

Match balanced expressions with mixed kinds of parentheses, for example {3[5]([12]34)}. This is highly irregular.
Source: Parentheses.cs
Blog post: Matching Mixed Balanced Parentheses

Acronym finder

Match acronym and their meaning, for example Three Lettered Acronyms (TLA)
Source: [StackOverflowAsnwers.cs - FindAcronymsRegex]
Blog post: Finding Acronyms in text, and Reversing the Stack

Find cartesian product between letters of two words

Get two words as input and return all combinations of letters: 123 abcd: 1a 1b 1c 1d 2a 2b 2c 2d 3a 3b 3c 3d Source: CartesianProductTests.cs

Sudoku Validator

Source: SolutionValidator.cs - SudokuValidatorPattern