Perl Training Logo: Praying Mantis
Regular Expressions

Regular Expression (also known as regex or regexp) are probably the part of any programming language with the most power to express what you mean.
The patterns described by regular expressions are used to search strings, extract desired parts of strings, and to do search and replace operations.

In order to show the power of regular expression, we'll show a few problems here that would be easy to solve with regular expressions. In some cases we also show a solution as well.

To learn Regular Expression you can take several approaches.

  • You can go over the excellent site of Jan Goyvaerts, even buy his book.
  • Mastering Regular Expressions by Jeffrey Friedl is also an excellent book but it is very dense and it is not really for the beginners.
  • Java Regular Expressions by Mehran Habibi is a beginners book aimed at Java programmers. While one could certainly learn Regular Expressions from this book, the pace of the book is very slow and many pages are wasted by explaining regular expressions character-by-character instead of subexpressions. Besides, one of the stated philosophies of the author is to prefer Java constructs over complex regular expressions. In my opinion this is slightly counter productive in a book teaching Regular Expressions.
The above solutions are cheap but are usually time consuming. Many people cannot afford the time it takes to read a book. They might also want to be focused on the most important parts of the subject and want to ask questions from a real person.
For these people we offer several courses:

Search for a string

  • Search and replace a part of a string based on content (as opposed to based on location).
  • Search for not exactly defined substrings or for a generalized substring.
  • Print out the lines of a file that have 'Foo' in them
    
            
  • Does the string have a substring which is at least 5 characters long and is repeated more than twice?
    
            

Input validation

  • Is the given string a valid phone number?
    Accept any of these: 03-1234567 or +972-3-1234567 or +972-(0)3-1234567 or even 03-123 4567
    
            
  • Does the given string represent a number?
            ^[+-]?\d+(\.\d+)?$
            

String replacement

  • Replace all occurrence of the stand-alone string R1 by R2, R2 by R3 and R3 by R1
            MOV R1, A
            MOV R3, A
            MOV B, R2
            MOV R11, R1
            
  • Remove all white spaces from the beginning of a string, end of a string.
  • Compact all white spaces to one space in the middle of the string.

String manipulations

  • split up a string into words