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 trialnfs
35,526 PointsWhat's the answer to practice no.2 in regex excluding characters?
We need to match these
34522
72379
& exclude these:
1234k
5784k
5784k
5 Answers
Steven Parker
231,248 PointsIn regex, there's nearly always multiple ways to perform a task. And here, much of the job is defining exactly what the task is.
But one solution is to define this particular task as identifying "any number of digits that end on a word boundary", which would be:
\d+\b
Doron Geyer
Full Stack JavaScript Techdegree Student 13,897 PointsI had the same. It is probably not the optimal situation when within other context but it works just fine here.
nfs
35,526 PointsAh, I see. That \b
is new to me...
Thank you.
Steven Parker
231,248 PointsI wasn't sure what had been introduced in the course. Another way might be "digits that occupy an entire line":
^\d+$
Magnus Martin
44,123 PointsWhat about \d+[^\dk] ?
Joseph Lander
Full Stack JavaScript Techdegree Graduate 27,765 PointsAs the question suggests, the lesson introduces us to the [^]
method of excluding characters and offers these questions to practice. It also used the inverse tokens \D, \W, \S.
So far the closest I can get does use the exclusion token but unfortunately still uses another token not yet provided:
\w+[^k]$
From research, I found that the $ token denotes the end of the test string.
- If anyone manages this one with the exclusion or inverse token and only tokens introduced to this point, please do add your solution. A reminder so far videos have shown: [abc], ?, *, +, \d, \w, \s, . , {3}, {3, }, {3,5}
EDIT
Ah! I think I've got it....
[^\D]{5}
Technically I think you'd use Sam Munter's solution or Steven Parker's as this says "not, not a digit" but at least this now uses what has been shown in the videos.