Search code examples
regexoracleplsqlfqdn

Validate FQDN Name without negative lookahead


I have found this link(www.regextester.com/103452) to validate the based on REGEX.

(?=^.{4,253}$)(^((?!-)[a-zA-Z0-9-]{0,62}[a-zA-Z0-9]\.)+[a-zA-Z]{2,63}$)

But there's one problem, Oracle doesn't recognize Positive/Negative Lookahead.

The first positive lookahead (?=^.{4,253}$) is easy, I can just verify the length of the string, but for the negative lookahead (?!-) I'm having some trouble figuring it out.


Solution

  • The ((?!-)[a-zA-Z0-9-]{0,62}[a-zA-Z0-9]\.)+ means 1 or more repetitions of:

    • (?!-)[a-zA-Z0-9-]{0,62} - 0 to 62 letters, digits or - with the first char of the sequence not equal to -
    • [a-zA-Z0-9] - a letter or digit
    • \. - a dot.

    Rephrasing: there can be 1 to 63 letters, digits or - with no - as first or last char of the sequence before ..

    Use ([a-zA-Z0-9]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?\.)+ instead, it will match 1 or more repetitions of:

    • [a-zA-Z0-9] - a letter or digit
    • ([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])? - an optional sequence of 0 to 61 letters, digits or - and then 1 obligatory letter or digit should follow
    • \. - a dot.

    So, it matches again 1 to 63 chars with no - at the start or end of the digit-letter-hyphen sequence.