Don't Forget The Caret^ and the $tick
secretGeek .:dot Nuts about dot Net:.
home .: about .: sign up .: sitemap .: secretGeek RSS

Don't Forget The Caret^ and the $tick

stupid cartoon

When using a regular expression for validation.... don't forget the caret^ and the $tick

Let's say, you want to make sure the user types in a 3 digit number.

Simple! Can't be simpler:

\d\d\d

Let's test if this suits the following:

"500" -- Yep!
"999" -- Yep!
"123" -- Yep!

All good so far. Let's check that it fails on the following:

"5" -- Yep!
"99" -- Yep!
"ABC" -- Yep!
"" -- Yep!

That's good too. I guess we're done, right?

stupid cartoon

Uh-oh! It looks like we forget the caret^ and the $tick!

Therefore the following would return a match!

"HEY I like the film 2001 a space odyssey!" -- Oops! matches our expression!

as would

"But 2010 was just junk right?" -- Oops! another match!

What if we use a caret "^" and a dollar sign "$" to anchor the expression to the start and end...

^\d\d\d$

stupid cartoon

Now we'll get the result we want!

So when using a regular expression for validation, don't forget to anchor them to the front and the back, by using... you guessed it...

the caret^ and the $tick.

(donkey image lifted from an article about Team Building)





'Wunar' on Fri, 12 Oct 2007 09:32:44 GMT, sez:

Why are you so sure that \d\d\d will fail on "9909"?



'lb' on Fri, 12 Oct 2007 09:50:12 GMT, sez:

Doh! Well spotted Wunar -- that kind of diminishes my point somewhat... i'll substitute with another counter example...
:-|



'Matt Blodgett' on Fri, 12 Oct 2007 11:45:04 GMT, sez:

I always just reflexively type the '^' and the '$' to start off any new regular expression I'm writing, and then put the "content" in the middle. :)



'Sensei Loco' on Fri, 12 Oct 2007 17:55:13 GMT, sez:

I bet u recently got bit by this one! ;)




name


website (optional)


enter the word:
 

comment (HTML not allowed)


All viewpoints welcome. But the right to delete any post for any reason is reserved. Don't make me do it. Comments may be republished, emailed to your loved ones or printed and used as toilet paper. Who reads this legal bit anyhow?

TimeSnapper is a life analysis system that stores and plays-back your computer use. It makes timesheet recording a breeze, helps you recover lost work and shows you how to sharpen your act.

TimeSnapper won last year's Developer Competition at Larkware.com, and is used by over 10,000 people.

Articles

21 tools used in our MicroISV 21 tools used in our MicroISV
Lost Treasures of the DOS World: tree! Lost Treasures of the DOS World: tree!
The Virtual Machine Machine and the Virtual Virtual Machine The Virtual Machine Machine and the Virtual Virtual Machine
Should Linq To Sql Go Should Linq To Sql Go "Open Source"?
Redux: New Synchronisation Idea Overlooked By Microsoft Redux: New Synchronisation Idea Overlooked By Microsoft
New Synchronisation Idea Overlooked By Microsoft Live team New Synchronisation Idea Overlooked By Microsoft Live team
Visual Studio UX Taskforce, Office UX Taskforce... etc. Visual Studio UX Taskforce, Office UX Taskforce... etc.
How to be Jeff Atwood How to be Jeff Atwood
Reuse good -- Abstractions better! Reuse good -- Abstractions better!
Word of the day: Upsert Word of the day: Upsert
awesome Atli tooltip awesome Atli tooltip
Leaky Abstractions? Try Asp.Net! Leaky Abstractions? Try Asp.Net!
A truly original thought A truly original thought
Throw New VirtualMachine(); Throw New VirtualMachine();
Undo, redo, predo, preundo Undo, redo, predo, preundo
A little A little "bit" of programming therapy
Coding Koan: the power of one Coding Koan: the power of one
Behavior Driven Development: As Human As Possible Behavior Driven Development: As Human As Possible
What To (Really) Do If You Find Out Your Parents Are Using Vista (redux) What To (Really) Do If You Find Out Your Parents Are Using Vista (redux)
What To Do If You Find Out Your Parents Are Using Vista What To Do If You Find Out Your Parents Are Using Vista
Sample Code From Text-Adventure Game Platforms Sample Code From Text-Adventure Game Platforms
TimeSnapper 3.0 -- an interactive, bubbling cauldron of possibilities TimeSnapper 3.0 -- an interactive, bubbling cauldron of possibilities
The laptop compubody sock The laptop compubody sock
Everything that's bad for you is suddenly good for you! Everything that's bad for you is suddenly good for you!
Everything I know about Code Reviews I learnt from Star Wars (and JCooney) Everything I know about Code Reviews I learnt from Star Wars (and JCooney)
Syntax highlighting of strings Syntax highlighting of strings
Google AppEngine: evil virus or viral evil? Google AppEngine: evil virus or viral evil?
Workflow software: I'm calling the bluff. Workflow software: I'm calling the bluff.

Archives .: secretGeek :: Complete Archives :.
25 steps for building a Micro-ISV 25 steps for building a Micro-ISV
3 minute guides -- babysteps in new technologies: powershell, JSON, watir, F# 3 Minute Guide Series
Top 10 SecretGeek articles Top 10 SecretGeek articles

Downloads

TimeSnapper -- Automated Screenshot Journal TimeSnapper.com    
Version 2.5: with password protection

ShinyPower (help with Powershell) ShinyPower
Now at CodePlex

Next Action NextAction
Managing the top of your mind



[powered by Google] 


Thai Erawan, Brisbane Restaurant, delicious thai food in paddington Thai Erawan, Brisbane Restaurant
World's Simplest Code Generator (html edition) World's Simplest Code Generator
Gradient Maker -- a tool for making background images that blend from one colour to another. Forget photoshop, this is the bomb. Gradient Maker
How to be depressed How to be depressed
You are not inadequate.



Recommended Reading

The Best Software Writing I
The Business Of Software (Eric Sink)

Recommended blogs

Jeff Atwood
Reginald Braithwaite
Joseph Cooney
Phil Haack
Scott Hanselman
Julia Lerman
Joel Pobar
Eric Sink
Joel Spolsky
Des Traynor

Aggregated Links

programming.reddit.com
dzone
dot net kicks

Human Link Machines

interesting finds
a continuous learner's weblog
arjan's world
n links today
new and notable
morning coffee
learning .net
weekly link post
(my del.icio.us account)

LinkedIn profile
 
home .: about .: sign up .: sitemap .: secretGeek RSS .: © Leon Bambrick 2006 .: privacy

home .: about .: sign up .: sitemap .: RSS .: © Leon Bambrick 2006 .: privacy