PHP Regex

I’ve been using a regular expression (PHP’s preg_match function) to parse email addresses. The addresses have a consistent pattern that look like:

The XXXXXX section is a random set of characters: a-z, A-Z, 0-9 and -. The regular expression’s job was to extract that random set of characters, which is easy to do:

123$email_addr = '';preg_match( '|anamehere\+(.*)@.*$|', $email_addr, $match );$email_code = $match[1];

Since the format of the email address is consistent it was easy to pull out the section I was interested in. Well, it was easy until it broke when the email was sent in from a specific email client. Turns out this client set the value of the email address to something different:

"" <>

After getting over my frustration at this email client I looked at what the regular expression matched:" <anamehere+XXXXXX

Well that was no good. Instead of just matching the XXXXXX it was slurping up other portions of the email address as well. Enter the greed factor of Perl Compatible Regular Expressions (PCRE). If you aren’t familiar with greedy regular expressions it simply means that they try to match as much as the can. Fortunately there is a way to turn off the greediness using the U pattern modifier. By adding a U to the end of the regular expression things got much better:

123$email_addr '';preg_match( '|anamehere\+(.*)@.*$|U'$email_addr$match );$email_code $match[1];

Now even the odd ball email address was extracting just the XXXXXX in the regular expression.

Using the U at the end turns off greediness for the entire regular expression. You can turn off the greediness of a single quantifier (the * in this case) by following it with a ?. Using that technique the regular expression is anamehere\+(.*?)@.*$ which works as well.

If you find that your regular expressions are matching more that you want remember that they are greedy by default.

Published by theirishduck

I love the ability where I can plan, prioritise, design, develop and deliver blended learning solutions for a variety of markets. I love to consult and advise about the best use of digital learning and improve design standards in line with evolving ways of working. I also enjoy to keep up to date with, understand and regularly recommend emerging technologies and practice to improve individuals, teams and organisational capabilities. With over 12 years digital learning experience and demonstrated experience in both instructional design and eLearning development, I have also a lot of experience conducting training needs analysis including how to leverage principles such as design thinking and root cause analysis to understand and address performance gaps. I also have led many facilitation workshops and even presented at iDesignX and Game Developers Conferences both here in Australia, Wales and in the United States. I bring loads of coding experience in Java, ASM, C++, HTML, JavaScript, SCORM and xAPI, as well as proven capability using Adobe Captivate, Trivantis Lectora and Articulate Storyline 360 Studio authoring programs. Of course, Adobe Creative Cloud is also part of my toolbox which I also use daily. Being taught traditional and advanced 3D animation techniques, I love hand drawing and polymer clay sculpture, but can also use the Blender, 3D Studio Max, Maya and Softimage applications. With strong multimedia, training and programming backgrounds, I understand modern learner behaviour including micro and social learning, I am very familiar with most LMSes and app-based (XCode and Android Studio), adult learning models and e-solutions. I also possess the ability to manage multiple projects simultaneously, whilst being pro-active in delivering work independently with minimal supervision, but enjoy working in teams. I've been told I am a resilient, relationship focused guy which can manage and navigate conflicting views and stakeholders/subject matter experts.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: