Validating The Same Text With Multiple Patterns Using Lookaround

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.

  1. Click the New Formula button on the top toolbar to clear out all settings on the Samples, Match, and Action panels.
  2. On the Samples panel, paste in one new sample:
    Short7!
    25IsDefinitelyFarTooLong!
    lowercase
    UPPERCASE
    NoDigit!
    0SpecialChars
    1ActualPa$$word
  3. Set the subject scope to “line by line” to tell RegexMagic to treat each line in our sample as if it were a separate sample. This way we don’t have to provide each sample password separately.
  4. On the Match panel, set “begin regex match at” to “start of text”, and set “end regex match at” to “end of text”.
  5. Click the Add First Field button to add field 1.
  6. Enter “Require lowercase letter” as the name for field 1.
  7. Select “look around” in the “kind of field” drop-down list. When you do this, RegexMagic adds a new field 2 as the pattern for the lookaround.
  8. Select “positive lookahead” in the “kind of lookaround” drop-down list for field 1.
  9. In the “pattern to match field” drop-down list, select “basic characters”.
  10. Tick “uppercase letters”, “digits”, and “punctuation and symbols”.
  11. Set the “repeat this field” spinner controls for field 2 to 0 and 19.
  12. With field 2 still selected, click Add Next Field button Add Next Field to add field 3 as the second field inside the lookaround field 1.
  13. Select “basic characters” as the pattern to match field 3.
  14. Tick “lowercase letters” and make sure the other options are unticked. This completes the first lookahead which skips over up to 19 printable ASCII characters that are not lowercase letters to look for one lowercase letter.
  15. Click on the colored rectangle for field 1 to make the field buttons work on that field.
  16. Click Add Next Field button Add Next Field to add field 4 after field 1.
  17. Enter “Require uppercase letter” as the name for field 4.
  18. Change it to a lookaround field as positive lookahead.
  19. Select “basic characters” as the pattern to match the automatically added field 5.
  20. Tick “lowercase letters”, “digits”, and “punctuation and symbols”. Make sure “uppercase letters” is not ticked.
  21. Set field 5 to repeat between 0 and 19 times.
  22. With field 5 still selected, click Add Next Field button Add Next Field to add field 6 as the second field inside the lookaround field 4.
  23. Select “basic characters” as the pattern to match field 6.
  24. Tick “uppercase letters” and make sure the other options are unticked.
  25. Click on the colored rectangle for field 4 to make the field buttons work on that field.
  26. Click Add Next Field button Add Next Field to add field 7 after field 4.
  27. Enter “Require digit” as the name for field 7 and make it a positive lookahead.
  28. Select “basic characters” as the pattern to match the automatically added field 8.
  29. Tick “lowercase letters”, “uppercase letters”, and “punctuation and symbols”. Make sure “digits” is not ticked.
  30. Set field 8 to repeat between 0 and 19 times.
  31. Click Add Next Field button Add Next Field to add field 9 as the second field inside the lookaround field 6.
  32. Configure field 9 to match a digit.
  33. Click on the colored rectangle for field 7 to make the field buttons work on that field.
  34. Click Add Next Field button to add field 10 after field 7.
  35. Enter “Require other character” as the name for field 10 and make it a positive lookahead.
  36. Select “basic characters” as the pattern to match the automatically added field 11.
  37. Tick “lowercase letters”, “uppercase letters”, and “digits”. Make sure “punctuation and symbols” is not ticked.
  38. Set field 11 to repeat between 0 and 19 times.
  39. Click Add Next Field button to add field 12 as the second field inside the lookaround field 6.
  40. Configure field 12 to match a punctuation character or symbol.
  41. Click on the colored rectangle for field 10 to make the field buttons work on that field.
  42. Click Add Next Field button to add field 13 after field 10.
  43. Enter “Password” as the name for field 13.
  44. Set field 13 to repeat between 8 and 20 times to enforce the password length.
  45. Select “basic characters” as the pattern for field 13.
  46. Tick all four of “lowercase letters”, “uppercase letters”, “digits”, and “punctuation and symbols”.
  47. We now have four lookaround fields 1, 4, 7, and 10 that will all be tried at the start of the text. When all four succeed, field 13 actually matches the password.



  48. On the Regex panel, select “Python 3.13” as your application, turn on free-spacing, and turn off mode modifiers. Click the Generate button, and you’ll get this regular expression:
    \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}
    \Z
    Required options: Case sensitive; Free-spacing.
    Unused options: Dot doesn’t match line breaks; ^$ don’t match at line breaks.
  49. The Samples panel now highlights the only string that meets all the requirements.
    Short7!
    25IsDefinitelyFarTooLong!
    lowercase
    UPPERCASE
    NoDigit!
    0SpecialChars
    1ActualPa$$word

Related Examples

Reference