---
title: "Q&A - Greedy matching in regular expressions"
url: "https://osherove.com/blog/2005/1/10/qampa-greedy-matching-in-regular-expressions.md"
canonical: "https://osherove.com/blog/2005/1/10/qampa-greedy-matching-in-regular-expressions.html"
published: "2005-01-10T17:37:00"
updated: "2005-01-10T17:37:00"
tags: ["Regex"]
author: "Roy Osherove"
description: "This came in the mail, thought other folks might be interested. Hi Roy. I need to check a line of html and make the value of the style attribute lowercase. I've tried to come up with a regex that will work but I keep…"
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/"
---

# Q&A - Greedy matching in regular expressions

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

**Historical article** · Originally published January 10, 2005; technical details may be outdated.

This came in the mail, thought other folks might be interested.

_Hi Roy. I need to check a line of html and make the value of the style attribute lowercase.  I've tried to come up with a regex that will work but I keep making the entire line of html lowercase instead of just the stuff in the style value.  I can't get the match to end with the correct quote, instead it goes to the last quote on the line.  So something like this:_

_\[Tag style="**WIDTH**:20px; color:blue;" href="blah.com/PageTWO"\] I want to change to this:
\[Tag style="**width**:20px; color:blue;" href="blah.com/PageTWO"\]_

_But instead I get this:
\[Tag style="**width:20px; color:blue;" href="blah.com/pagetwo**"\]_

_Because the match ends with the end quote of the href._

_If you can point me in the right direction (or having something like this laying around), I would GREATLY appreciate it.
_

**Answer:**

It's called "greedy matching" - because it looks for the \*last\* character.
Try to add a "?" after the quanitiy specifier (probably '\*'). That makes the match end on the \*first\* match.

For example, given the following string as input:
**"abcdfgdrbdtargd"
**The following greedy regex (greedy by default) will match up until the lasd 'd':
**(.\*d)**

However, this regex will find several matches, the first one is "abcd":
**(.\*?d)**
(you can do without the braces if you want).

I'd also suggest adding two good regex mailing list to your arsenal instead of sending help messages to various people:
[http://groups.yahoo.com/group/dotnetregex/](http://groups.yahoo.com/group/dotnetregex/)
[http://lists.aspadvice.com/SignUp/list.aspx?l=68&c=16](http://lists.aspadvice.com/SignUp/list.aspx?l=68&c=16)

There are people there that know a whole lot more than me on regular expressions.

## More in Regex

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

- Jan 16, 2005

    ### ["Buy first, ask questions later"](https://osherove.com/blog/2005/1/16/quotbuy-first-ask-questions-laterquot.html)

- Jan 11, 2005

    ### [The Regulator now hosted at Regex.osherove.com](https://osherove.com/blog/2005/1/11/the-regulator-now-hosted-at-regexosherovecom.html)

- Jan 7, 2005

    ### [Full source for The Regulator released](https://osherove.com/blog/2005/1/7/full-source-for-the-regulator-released.html)

- Jan 7, 2005

    ### [Using Regex to return the first N words in a string](https://osherove.com/blog/2005/1/7/using-regex-to-return-the-first-n-words-in-a-string.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/
