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! While you're at it, check out some resources Treehouse students have shared here.

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 Java Data Structures - Retired Organizing Data Splitting Strings

Anthony Logsdon
Anthony Logsdon
9,786 Points

Expected 11 words but returned 12 with ("[\\s+]"), passed with ("\\s+")

In the splitting strings code challenge, when I used brackets around the regex it returned an extra word. I took the brackets out and the code passed. What is up with that?

1 Answer

Florian Tönjes
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Florian Tönjes
Full Stack JavaScript Techdegree Graduate 50,856 Points

Hey Anthony,

if you put something in square brackets in a regex it will try to match each character in the square brackets. The plus inside the square brackets means that the regex will search for a plus sign. It's not using the "+" as a quantifier as it is in "\\s+".

For example, "[a+]" will match "aaaa" four times, while "a+" will match "aaaa" just once.

Kind Regards, Florian

Anthony Logsdon
Anthony Logsdon
9,786 Points

Thank you, that cleared things up for me!

Mladen Jovanovic
Mladen Jovanovic
4,662 Points

Thanks, Florian! Would have been nice if Craig had explained this before just telling us to use a regex expression that was not even mentioned in the lesson. I kept getting the same issue.