Modern regular expression engines have the ability to “look around”. They can look ahead or look behind to see whether some text can be matched at the position the regex engine has reached in the subject string, without actually including that text in the regex match. This makes it possible to validate the same text with multiple RegexMagic patterns. One pattern is placed in a lookahead which checks that the pattern can be matched, without actually “consuming” the text that is matched. The other pattern follows the lookahead. It starts its match from the same position as the lookahead and produces the actual regex match. If either pattern fails to match then the overall regex fails.
An easy to understand example is testing a string for password complexity rules. The sample rules require the password to consist entirely of printable ASCII characters and contain at least one lowercase letter, one uppercase letter, one digit, and one other character. It must be between 8 and 20 characters long. In procedural code, it’s easy enough to iterate over all the characters in the string and track which ones are included. But we’ll assume we need to do this in an environment where a single regular expression is all that is available to perform this test.
Without lookaround, this would require a very long regular expression. We have 4 types of characters to check for, and they can appear in any order. That requires a regex with 24 alternatives to allow for all the permutations. The length restriction multiplies that with another 13 permutations.
With lookaround, we can make the actual regex match enforce the length. Four lookaheads can check for the presence of each of the the required characters. They must be placed at the start of the regex so that they can see all the text that the regex will (possibly) end up matching. Because a lookahead is only tried at one position, it needs to skip over any characters that are also allowed in the password but that it isn’t checking the requirement for. You can find this example as “Fields: overlap lookaround” in the RegexMagic library.
In principle, we could achieve the same thing with four lookbehinds at the end of the regex. But many applications impose restrictions on lookbehind. The variable quantifier that we need to skip over the other characters is not allowed in lookbehind by many applications.
Short7! 25IsDefinitelyFarTooLong! lowercase UPPERCASE NoDigit! 0SpecialChars 1ActualPa$$word
button to add field
.
.
as the pattern for the lookaround.
.
to 0 and 19.
still selected, click Add Next Field button
to add field
as the second field inside the lookaround field
.
.
to make the field buttons work on that field.
to add field
after field
.
.
.
to repeat between 0 and 19 times.
still selected, click Add Next Field button
to add field
as the second field inside the lookaround field
.
.
to make the field buttons work on that field.
to add field
after field
.
and make it a positive lookahead.
.
to repeat between 0 and 19 times.
to add field
as the second field inside the lookaround field
.
to match a digit.
to make the field buttons work on that field.
after field
.
and make it a positive lookahead.
.
to repeat between 0 and 19 times.
as the second field inside the lookaround field
.
to match a punctuation character or symbol.
to make the field buttons work on that field.
after field
.
.
to repeat between 8 and 20 times to enforce the password length.
.
,
,
, and
that will all be tried at the start of the text. When all four succeed, field
actually matches the password.
\A # 1. Require lowercase letter: Look ahead to assert that fields 2 to 3 can be matched starting at this position (?= # 2. Basic characters []!"#$%&'()*+,./0-9:;<=>?@A-Z\[\\_`{|}~^-]{0,19} # 3. Basic characters [a-z] ) # 4. Require uppercase letter: Look ahead to assert that fields 5 to 6 can be matched starting at this position (?= # 5. Basic characters []!"#$%&'()*+,./0-9:;<=>?@\[\\_`a-z{|}~^-]{0,19} # 6. Basic characters [A-Z] ) # 7. Require digit: Look ahead to assert that fields 8 to 9 can be matched starting at this position (?= # 8. Basic characters []!"#$%&'()*+,./:;<=>?@A-Z\[\\_`a-z{|}~^-]{0,19} # 9. Basic characters [0-9] ) # 10. Require other character: Look ahead to assert that fields 11 to 12 can be matched starting at this position (?= # 11. Basic characters [0-9A-Za-z]{0,19} # 12. Basic characters []!"#$%&'()*+,./:;<=>?@\[\\_`{|}~^-] ) # 13. Password: Basic characters []!"#$%&'()*+,./0-9:;<=>?@A-Z\[\\_`a-z{|}~^-]{8,20} \ZRequired options: Case sensitive; Free-spacing.
Short7!
25IsDefinitelyFarTooLong!
lowercase
UPPERCASE
NoDigit!
0SpecialChars
1ActualPa$$word