Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Regular Expressions: Non Greedy Matching

DZone's Guide to

Regular Expressions: Non Greedy Matching

· Java Zone
Free Resource

What every Java engineer should know about microservices: Reactive Microservices Architecture.  Brought to you in partnership with Lightbend.

I was playing around with some football data earlier in the week and I wanted to try and extract just the name ‘Rooney’ from the following bit of text:

Rooney 8′, 27′

My initial regular expression was the following which annoyingly captures the time of the first goal:

> "Rooney 8′, 27′".match(/(.*)\s\d(.*)/)[1]
=> "Rooney 8,"

It works fine if the player has only scored one goal…

> "Rooney 8′".match(/(.*)\s\d(.*)/)[1]
=> "Rooney"

…but since the second part of the regex (“\s\d(.*)”) appears twice only the last instance of it is matched and the rest of the text gets captured by the first part of the regex.

One way around this is to make the first part of the regex non greedy/lazy so that it will make as little as possible rather than as much as possible.

We can do that by using the lazy version of ‘*’ which is ‘*?’:

> "Rooney 8′, 27′".match(/(.*?)\s\d(.*)/)[1]
=> "Rooney"

Of course you could also argue that I’m being very lazy by using “.*” in the first place and you probably have a point! The following more explicit regular expression achieves the same thing:

> "Rooney 8′, 27′".match(/([A-Za-z\s-]+)\s\d(.*)/)[1]
=> "Rooney"

As a side note I find that when I’m playing around with regular expressions it really makes sense to have a bunch of test cases that I can run after each change to make sure I haven’t inadvertently broken everything.

regexper.com is also really helpful for visualising what the regular expression is actually doing!

Microservices for Java, explained. Revitalize your legacy systems (and your career) with Reactive Microservices Architecture, a free O'Reilly book. Brought to you in partnership with Lightbend.

Topics:

Published at DZone with permission of Mark Needham, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}