---
title: "Balanced matching using .NET Regex"
url: "https://osherove.com/blog/2005/6/19/balanced-matching-using-net-regex.md"
canonical: "https://osherove.com/blog/2005/6/19/balanced-matching-using-net-regex.html"
published: "2005-06-19T15:12:00"
updated: "2005-06-19T15:12:00"
tags: ["Regex"]
author: "Roy Osherove"
description: ".NET Regular expressions have an extended ability to match \"balanced items\". For example, in the string \"a + (b+(c+d))\" it can find correct pairs of matching open and closing brackets. It's not the most simple solution…"
usage: "Licensed under CC BY 4.0 — reuse, quote and adapt with attribution (Roy Osherove, link to the original page)."
license: "https://creativecommons.org/licenses/by/4.0/"
---

# Balanced matching using .NET Regex

By Roy Osherove June 19, 2005 In [Regex](https://osherove.com/blog/category/regex)

**Historical article** · Originally published June 19, 2005; technical details may be outdated.

.NET Regular expressions have an extended ability to match "balanced items". For example, in the string

_"a + (b+(c+d))"_ it can find correct pairs of matching open and closing brackets.

It's not the most simple solution in the world, so be ready for a small mind-melt when you [read this from the BCL team](http://blogs.msdn.com/bclteam/archive/2005/03/15/396452.aspx) (Base class library team at MS)

## More in Regex

[All posts in Regex](https://osherove.com/blog/category/regex)

- Nov 26, 2005

    ### [Announcing Regex Kit: Regular Expression Visualizers for VS 2005](https://osherove.com/blog/2005/11/26/announcing-regex-kit-regular-expression-visualizers-for-vs-2.html)

- Jul 8, 2005

    ### [Sessions done at TechEd](https://osherove.com/blog/2005/7/8/sessions-done-at-teched.html)

- Jun 5, 2005

    ### [The Regex phrase of death](https://osherove.com/blog/2005/6/5/the-regex-phrase-of-death.html)

- Jun 2, 2005

    ### [Announcing: The Regulator Logo Contest](https://osherove.com/blog/2005/6/2/announcing-the-regulator-logo-contest.html)


---

## About the author

**Roy Osherove**: Senior Solutions Architect at AWS working on AI for production and AI-driven development; author of The Art of Unit Testing and Elastic Leadership, with about 20 years of consulting and training on unit testing, TDD, team leadership and continuous delivery.

### Sites

- [RobotPaper](https://robotpaper.ai/): This blog: AI patterns and anti-patterns, AI and coding agents, and engineering leadership.
- [osherove.com](https://osherove.com): Roy's personal site. Author of The Art of Unit Testing and Elastic Leadership; consulting and training on unit testing, TDD, team leadership and continuous delivery, plus about 20 years of blog posts.
- [The Art of Unit Testing](https://www.artofunittesting.com): The Art of Unit Testing, 3rd Edition (Manning): readable, maintainable, trustworthy unit tests, plus TDD workshops in C#, JavaScript, Java and Kotlin.
- [5whys.com](https://www.5whys.com): Roy's writing on software team leadership: self-organizing teams, surviving chaos, learning mode and the Elastic Leadership model.
- [Pipeline Driven](https://pipelinedriven.org): Continuous delivery and DevOps practices: build patterns, pipeline-friendly testing, DevOps metrics and enterprise DevOps.
- [Elastic Leadership](https://www.elasticleadership.com): Elastic Leadership, Roy's framework for software team leaders (survival, learning and self-organizing modes).

### Social

- X: https://x.com/royosherove
- LinkedIn: https://www.linkedin.com/in/osherove/

### Contact

- Contact form: https://forms.gle/jQSx5KoXvrVA1JCK7

### License

Licensed under CC BY 4.0 — reuse, quote and adapt with attribution (Roy Osherove, link to the original page). License: https://creativecommons.org/licenses/by/4.0/
