Programming – Regex, studied hard in Python now PHP benefits.

Taking Python seriously a few months ago upgraded all my coding skills and its showing. Regular Expressions aka Regex is used in many languages, I have taken courses on it before. Unfortunately I never used it besides a simple or (word|this|cool) manner, this made me sadface. So when Regex came up in Python learning I got into it, trying all sorts of patterns, obsessively.

Well here is something super amazing, once you learn it on one language the pattern syntax is virtually the same for most languages.You will however have many different language functions to search, match, do other things with your patterns like re.match() in Python is preg_match() in PHP. So I had an immediate use case, but not in Python in PHP. Basically I had an if conditional that checks the membership names of all active ones in a foreach loop from an array.

I had this huge line if ($membership->plan->name === ‘Member’ || $membership->plan->name === ‘Membership 1’ || $membership->plan->name === ‘AOS Membership’ || $membership->plan->name === ‘AOS Membership S’ )

This line was very long and stupidly too hardcoded, I wanted something that could except either a strict set of names, or even better future proof any new possible names I come up with later. Below some PHP test code that removes that huge if line and uses preg_match() with a smart regex pattern.

Look how much smaller and way more flexible that second $pattern is. The only must have is AOS Membership or aos membership aka a pattern, the other names were for built in system names and don’t matter in this test code really.

// Below works for all test cases for 'AOS Membership' or 'AOS Membership S' strict version case sensitive, spaces matter, exact naming. Disabled because the next $pattern below is better imo.

//$pattern = "/^(Member|Membership 1|AOS Membership(\s?|\sS?))$/";

// Below modified to accept 'AOS Membership' or 'AOS Membership Anything' anything must end the name and be text or number characters. Spaces matter, case insensitive, more flexible.

$pattern = "/^(Member|Membership 1|AOS Membership(\s?|\s\w+))$/i";

// To test many patterns this $name variable string can be changed
$name = "AOS Membership 99";
echo (preg_match($pattern,$name)) ? 'Valid Name' : 'Invalid Name ';

To sum it up

This below:

$membership->plan->name === ‘Member’ || $membership->plan->name === ‘Membership 1’ || $membership->plan->name === ‘AOS Membership’ || $membership->plan->name === ‘AOS Membership S’

case sensitive, hard coded, gets bigger, must be updated more if new names are added, HUGE

Became this below:

/^(Member|Membership 1|AOS Membership(\s?|\s\w+))$/i

smaller, quicker, less repeating, excepts future new names.


Wait how does that crap work? Some explaining, but its hard to read this way,

/pattern/i forward slash starts the php regex pattern and ends it, a modifier to the entire pattern is added at end after last / i for case insensitive.

Full pattern

/^(Member|Membership 1|AOS Membership(\s?|\s\w+))$/i

^ Membership name must start with any of the names inside the or group ()

(name|or other name|or this one) the | means or

The third name is where the magic is AOS Membership(\s?|\s\w+)

Can just be AOS Membership

Next part is tricky in another group () inside third or name is \s? = one or no whitespace | or \s\w+ = whitespace and one or more text or number characters . So ‘AOS Membership’ and ‘AOS Membership Plus’ would work, even ‘AOS Membership 69’ works.

What will not work are ‘AOS Membership Plus Plus’ ‘AOS MembershipPlus’ ‘AOSMembership’

to end the pattern $ meaning no more characters or spaces can be added

/i delimiter i means case insensitive so ‘aos membership coool’ even works

This was first advanced regex I completely figured out on my own and put to actual use, am quite happy about this.

/^(Member|Membership 1|AOS Membership(\s?|\s\w+))$/i

Oh but there is more, this could be made even shorter, maybe real short. See how ‘Member’ is in all the names and there are three names between the | or separators. I think its possible to have just one /^Name$/i with a very complicated regex to except more variations. Basically build a bunch of regex before and after ‘Member’.

Damn now I feel a challenge coming on, I just want to see how complicated it looks. Now making it more complicated is good for learning it will make it harder to read, the versions I have now is perfectly good to use as is. There could be other ways to improve it too.

Leave a Comment