My regex is matching too much. How do I make it stop? [duplicate]

I have this gigantic ugly string and I'm trying to extract pieces from it using regex. In this case, I want to grab everything after "Project Name" up to the part where it says "J0000011:" (the 11 is going to be a different number every time).

J0000000: Transaction A0001401 started on 8/22/2008 9:49:29 AM
J0000010: Project name: E:\
J0000011: Job name: MBiek Direct Mail Test
J0000020: Document 1 - Completed successfully

Here's the regex I've been playing with

Project name:\s+(.*)\s+J[0-9]{7}:

The problem is that it doesn't stop until it hits the J0000020: at the end.

How do I make the regex stop at the first occurrence of J[0-9]{7}?

22.08.2008 14:10:40
When in doubt, be lazy!
CinCout 18.03.2019 11:30:06
Project name:[^\n]*\n(J[0-9]{7})
Aphton 6.05.2019 20:36:54

Make .* non-greedy by adding '?' after it:

Project name:\s+(.*?)\s+J[0-9]{7}:
15.12.2016 09:27:19
That is the most awesome regex tip i've come across
Dr Manhattan 11.10.2019 08:00:10
Life-saving answer - never knew we could stop regex from being greedy until today!!
Jay Dadhania 16.04.2020 01:14:21

Using non-greedy quantifiers here is probably the best solution, also because it is more efficient than the greedy alternative: Greedy matches generally go as far as they can (here, until the end of the text!) and then trace back character after character to try and match the part coming afterwards.

However, consider using a negative character class instead:

Project name:\s+(\S*)\s+J[0-9]{7}:

\S means “everything except a whitespace and this is exactly what you want.

13.08.2018 18:11:24
When possible to implement, a greedy negative (or positive) character class will usually perform notably better than a lazy quantifier. Laziness requires the engine to forward-track character by character, checking the pattern that follows each time until it matches; a greedy character class can mindlessly repeat just the desired characters, which can be a lot quicker. So, you might consider making a stronger case for a negative character class, seeing as this is the greedy-vs-lazy canonical.
CertainPerformance 30.10.2018 09:26:16

I would also recommend you experiment with regular expressions using "Expresso" - it's a utility a great (and free) utility for regex editing and testing.

One of its upsides is that its UI exposes a lot of regex functionality that people unexprienced with regex might not be familiar with, in a way that it would be easy for them to learn these new concepts.

For example, when building your regex using the UI, and choosing "*", you have the ability to check the checkbox "As few as possible" and see the resulting regex, as well as test its behavior, even if you were unfamiliar with non-greedy expressions before.

Available for download at their site:

Express download:

22.08.2008 14:22:55
There are a few great websites out there already. I'd rather visit a bookmark than have another program on my computer.
Matt M. 18.11.2018 04:08:14

Well, ".*" is a greedy selector. You make it non-greedy by using ".*?" When using the latter construct, the regex engine will, at every step it matches text into the "." attempt to match whatever make come after the ".*?". This means that if for instance nothing comes after the ".*?", then it matches nothing.

Here's what I used. s contains your original string. This code is .NET specific, but most flavors of regex will have something similar.

string m = Regex.Match(s, @"Project name: (?<name>.*?) J\d+").Groups["name"].Value;
2.01.2017 16:36:48

(Project name:\s+[A-Z]:(?:\\w+)+.[a-zA-Z]+\s+J[0-9]{7})(?=:)

This will work for you.

Adding (?:\\w+)+.[a-zA-Z]+ will be more restrictive instead of .*

16.07.2018 10:44:30