Writing complex regular expressions

Regular expressions are usually hard to read and understand. Even if you have a lot of experience in the subject chances are that when you revisit one of these that you wrote some time ago, it is very difficult to catch up.

Several days ago, a very smart guy at work named Zolt├ín, recommended us to write complex regular expressions sepparating each logical part in a different line and also comment every single line.

I did not know that this was possible at all, but he shown us how the modifier "x" (see it at the end of the following example) makes the compiler to ignore any whitespaces (spaces, tabs, line breaks) and even comments!!

So, with this, you can write crazy regular expressions easy to parse and understand. Here there is a silly example, imagine it in a single line!:

(you better copy and paste this in your editor for readability)

if ( preg_match('/
    ^                  # Match the beginning
    (Can|May)\x20      # May is more formal but Can is also OK.
    [yY]ou\s           # Match space with "\ " or "\s"
    (please)?\x20      # "Please" is optional ;)
    (comment|doc)\x20  # Commenting = documenting
    this\ regexp\x20   # If UNICODE mode is on then
                       # you can match space with \x20
                       # (this is the modifier "u")
    (what|WTF)\x20     # WTF = World Taekwondo Federation
    it\ does\?         # Note the "x" modificator below
    $                  # END of string
    "Can you please comment this regexp to know what it does?" ) )
    echo "Thank you!";

Thank you Zoly!