Imagine if you can use a language that does not need an else , but you want to imitate it. Instead of writing
if (condition) { yes part } else { no part }
You need to write
if (condition) { yes part } if (!condition) { no part }
Well, here is what you need to do here, but in the template. What you do in Java without conventions, you repeat the condition, but deny it, in the ELSE block, which is actually an OR block.
So, for example, instead of writing this in Perl with conditional support in a template:
In Java, write:
You can recognize this as a definition of \b . Here, then, it is similar for the definition of \b s, first using the conventions:
And now, repeating the (now denied) condition in the OR branch:
Note how it doesn't matter how you roll them, that the corresponding definitions of \b and \b same for defining only \w , never on \w , not to mention \s .
The ability to use conditional expressions not only allows you to type, but also reduces the likelihood of misuse. They can also be cases where you do not want to evaluate the condition twice.
Here I use this to define a few regular expression routines that the Greek atom and its boundaries provide to me:
(?(DEFINE) (?<greeklish> [\p{Greek}\p{Inherited}] ) (?<ungreeklish> [^\p{Greek}\p{Inherited}] ) (?<greek_boundary> (?(?<= (?&greeklish)) (?! (?&greeklish)) | (?= (?&greeklish)) ) ) (?<greek_nonboundary> (?(?<= (?&greeklish)) (?= (?&greeklish)) | (?! (?&greeklish)) ) ) )
Note that borders and non-borders are used only (&?greeklish) , never (?&ungreeklish) ? You never need anything but borders. Instead, you do not put in your images, as \b and \b .
Although in Perl it is probably easier (albeit less general) to simply define a new, custom property \p{IsGreeklish} (and therefore its complement \p{IsGreeklish} ):
sub IsGreeklish { return <<'END'; +utf8::IsGreek +utf8::IsInherited END }
You will not be able to translate any of them into Java, although not so much because of insufficient Javas support for conditional expressions, but because its template language does not allow you to execute calls (DEFINE) or regular expression routines like (?&greeklish) - and indeed, your templates cannot even be written in Java. You also cannot define custom properties in Java, such as \p{IsGreeklish} .
And, of course, conditional expressions in Perl-regular expressions can be more than search strings: they can even be blocks of code to execute, so you certainly do not want the same condition to evaluate them twice, so as not to have side effects. This does not apply to Java because it cannot do this. You cannot mix patterns and code that limits you more than you might think before getting used to it.
There are so many things you can do with the Perl regex engine that you can do in no other language, and these are just a few. Not surprisingly, the significantly expanded Regexes chapter in the new 4th edition of Perl programming, combined with the completely rewritten Unicode chapter, which now immediately follows the Regexes chapter (which has been promoted to part of the internal kernel), has a combined page count of something like 130 pages, so double the length of the old chapter according to the template compared to the 3rd edition.
What you just saw above is part of the new 4th edition, due to be published next month or so.