Regular Expressions: Matching specific characters

Problem:

Create a regular expression that matches the following words "can" , "man" , "fan" ; those words shouldn't be followed "dan" , "ran" , "pan".

Output:







Solution:


Now this could be solved in many ways; the easiest and laziest way to solve this regular expression would be simply using this:
can|man|fan

Now, this sounds too mediocre; at least for me! I would prefer to solve it like this:
[cmf]an

As you see, regular expressions analyzes stuff character-by-character. So first you tell the regular expression to only match either 'c', 'm' or 'f' as a first character. Then add 'an' to the rest; which works.

Alternatively, you could use:
[^drp]an



1 comment :

  1. body liposuction Korea's #1 Liposculpture Clinic. Lydian plastic surgery is the home of VIP patients. Celebrities, Influencers and Diplomats all know and trust Doctor An and Lydian plastic surgery clinic to provide detailed results.

    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