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.
The easiest option is to run dotnet test
, this will run all existing tests and write output to console.
Source: Mazes.cs
Blog post: Solving mazes using regular expressions
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
Source: SmallRectangles.cs
Blog post: Filling a large rectangle with smaller rectangles using regular expressions
Yep
Source: StackOverflowAsnwers.cs - FindRegexComments
Stack Overflow answer: How to extract regex comment?
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.
- Compare binary and decimal numbers - match pairs like
4:100
and1342:10100111110
.
Source:ParseNumbers.cs
Blog post: Using the Stack State to Understand Numeric Values - Check if a number is divisible by 3 - stack-based approach.
Source:/DivisibleByThree.cs
Blog post: Finding Decimal Numbers that are Divisible by Three
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
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
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