Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community!

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java Regular Expressions in Java

Cassie Wilson
Cassie Wilson
1,827 Points

Why didn’t the regular expression: (\w*(sh|ti|su)\w*) not capture the first “ti” in the word procrastination?

I’m watching the tutorial video of Regular Expressions in Java and we’re using the Visual Regex Tester to learn regular expressions.

The target text is “Procrastination is surely not a destination, should we talk about shiny things?”

The regular expression is (\w*(sh|ti|su)\w*), capturing all the words with “sh”, “ti”, or “su”.

However, it doesn’t capture the first “ti” in the word “procrastination”. Why?

2 Answers

John Gile
John Gile
3,165 Points

\w* is considered "greedy" and will try to return as much as possible, which will make your first \w* return as many characters as possible before it enters the second capture group. if you change your first \w* to \w*? it will allow it to pick up the minimum number of characters and it will capture the first "ti" in the word.

i believe the "greedy" patterns cause it to keep searching for the "best" match, as opposed to the first match.

Andre Kucharzyk
Andre Kucharzyk
4,479 Points

Hmm it matches the 'ti' in the word Procrastination in my code...

Are we doing something different?

import java.util.regex.Pattern; import java.util.regex.Matcher;

class Main { public static void main(String[] args) {

    String shStrings = "Procrastination is surely not the destination," +
            "should we talk about shiny things?";

    Pattern pattern = Pattern.compile("(\\w*(shi|ti|su)\\w*)",
    Matcher matcher = pattern.matcher(shStrings);
    while (matcher.find()) {
        System.out.printf("%s is a shushy word because of %s.   %n",