I stumbled upon this nice blog post by nikic titled The true power of regular expressions. I saw that regex of him using a DEFINE assertion to create a regular expression that matches an email in a RFC conformant way. The named captures are nice, but what I'm really wondering about is some documentation on the DEFINE assertion. Simplified example:
/(?(DEFINE)(?<NUM>[0-9]))^(?&NUM)$/
A quick search through my Mastering Regular Expressions book did not yield any fast results. Google left me empty handed and a test online with ideone lead to the result, that DEFINE is most probably some keyword to define assertions as I can't replace the word with e.g. FOOBAR. ;-) Links to the ideone test: matching vs. non-matching
Any explanations, search keywords, hints or links to some nice documentation are greatly appreciated. Thanks in advance. :-)
(DEFINE)
works as a way of defining named subpatterns. In your example above, you are defining the NUM
subpattern, then using it (?&NUM)
. In the linked example by nikic, a whole bunch of named sub-patterns are defined using each other, which cumulates in ?&addr_spec
.
As you've found, searching for the word "define" isn't easy. I've found a couple of references to get you started: