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

Development Tools Introduction to Regular Expressions Regular Expressions Finding Repeated Characters

karan Badhwar
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
karan Badhwar
Web Development Techdegree Graduate 18,135 Points

help understanding it

I did not get it how this worked *, + {}, can somebody please elaborate this to me with example? How * and + matches and what it matches Please

3 Answers

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,441 Points

Given an preceding element such as a character “a”, a character list “[a-z]” or a group “(bar)”, the characters mean:

  • + one or more of the preceding element
  • * zero or more of the preceding element
  • {} is a range of repeating the preceding element

Post back if you need more help. Good luck!!!

karan Badhwar
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
karan Badhwar
Web Development Techdegree Graduate 18,135 Points

Sir I did not get it now after these videos am exhausted and confused completely?

I am unable to understand how it is comparing

ex- toy\w* vs toy\w+

over here what is being compared (\w) only or the full set and how the further stuff gets matched

toy
toyboat
toycar 
  • / * how they are comparing and what they are comparing with what?
Chris Freeman
Chris Freeman
Treehouse Moderator 68,441 Points
  • toy\w* means “toy” followed by zero or more word characters.

So this would match

  • “toy” (zero following characters)
  • “toys” (one following character)
  • “toycar” (more than one following character)

  • toy\w+ means “toy” followed by one or more word characters.

So this would match

  • “toys” (one following character)
  • “toycar” (more than one following character)

It would not match

  • “toy” because the minimum of one following characters is not met.
Chris Freeman
Chris Freeman
Treehouse Moderator 68,441 Points

The regular expression (regex) is used as a “recipe template” to compare to a string. A string is “consumed” one character at a time and checked against the regex to see if the regex pattern still holds true. If the string is exhausted before the regex end or if the regex end is reached before the sting is fully consumed, then the is no matching result.

So given the regex toy\w+ and the “toycar”:

 t o y \w+
 | | | |
 o o o x
 k k k x fail 
 | | | |
 t o y

this fails because at lease on more character was expected after the “y”

In the case of the \w* no extra charters are expected after the “y” so the regex will match the string.