Regular Expressions: Whitespace Exercise


Validate those texts "1.  abc" , "2.      abc" and "3.              abc" and skip this text "" in this regular expression problem.



Let's solve this step-by-step; as you know to solve this regular expression; you have to take it character-by-character:

Step 1: The first character is integer (or number)! This means that you need to tell the regular expression that the first character is a digit (number). To do that you have to add: \d - which designates a digit.

Step 2: You add the dot "." and you escape it: \.

Step 3: This is the trickiest part. First the white space could be normal space or a tab! To designate a tab you need to add \t and a normal space you add a _; since you might have both. You add \s which designates both tabs and normal spaces! Now \s designates one whitespace only; so you have to tell you the regular expression you might have more whitespaces which you indicate \s* but wait a second! The star means you might have at least zero whitespace and it goes into skip text; you have to tell the regular expression that you can't have zero whitespaces; one and more only! This is the where the Kleene "+" comes into place. To indicate one and more whitespace you add \s+.

Step 4: Really as easy as counting abc now ;) 

So, at the end: \d\.\s+abc

No comments :

Post a Comment

Follow Me

If you like our content, feel free to follow me to stay updated.


Enter your email address:

We hate spam as much as you do.

Upload Material

Got an exam, project, tutorial video, exercise, solutions, unsolved problem, question, solution manual? We are open to any coding material. Why not upload?


Copyright © 2012 - 2014 Java Problems  --  About  --  Attribution  --  Privacy Policy  --  Terms of Use  --  Contact