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 Regular Expressions in Java

Andre Kucharzyk
Andre Kucharzyk
4,479 Points

[A-Za-z] vs [A-Za-z]*

Hi, can anybody explain what is the difference between [A-Za-z] vs [A-Za-z]*
I mean - is there any? The way Craig explains it is bit too vague, or maybe I'm just being thick

3 Answers

Hi Andre. The asterisk (*) means "zero or more". So:

  • [A-Za-z] is "one letter from the A-Z alphabet, upper or lower case"
  • [A-Za-z]* is "zero or more letters from the A-Z alphabet, upper or lower case"

The asterisk should not be confused with the plus (+) sign, which means "one or more". So if there is a chance there might be none, use the asterisk instead (*).

Hope it clarifies things :)

Hello guys. Well it's not actually the same. The asterisk also matches the instances that a letter isn't there. For example if you have a string "Hello World" the regular expression with the asterisk will also find the space between the two words. The asterisk means zero or more times. So when it encounters the space is like saying "OK I found a letter zero times in this position" whereas the pattern without the asterisk will skip the whitespace. You can see that in the documentation for regex on Oracle https://docs.oracle.com/javase/tutorial/essential/regex/quant.html

as far as i know they are the same. A-Z is letters starts from A to Z in upper case and a-z in lower case.