Regular Expressions: Whitespace Exercise

Problem:

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

Output:







Solution:


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


1 comment :

  1. The new inotia 4 mod unlimited everything Hack is the perfect tool to get more resources, gold and items in your game. With this hack you can get unlimited resources, gold and items for free. Inotia 4 mod apk Hack is easy to use and does not require any jailbreak or root access. Just download the app and start hacking.

    ReplyDelete

Follow Me

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

Subscribe

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?

Upload

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