Lets try some practical examples of Regular Expressions and see if you can work out what the actual Regular Expression should be.

The Questions

  • Captial P followed by seven digits.
  • The word tom in lowercase, but not tomato or atom or similar anywhere in the string.
  • Every occurance of alphabet anywhere in any case.
  • Three digits followed by a tab then any four letters in any case followed by a new line.
  • AB or CD - in upper case - followed by three digits with an optional z on the end.
  • What does the following pattern match - /^\d{3,5}((\d{3})|([A-Z]))\tJava(script)?\n$/g ?

The Answers

Here are the answers to the above questions, although you may have found a different way to get to the same end :
  • /^[P]\d{7}$/
  • /\btom\b/g
  • /alphabet/gi
  • /\d{3}\t([a-z])\n/i
  • /(([A][B])|([C][D]))\d{3}(z)?/
  • Three to five digits followed by either three digits or any capital letter followed by a tab, then Java or Javascript followed by a newline anywhere in the string.