Search code examples
regexvalidationdelphidelphi-xe5

Validate ISO 639-2 language codes ​​- Delphi


How do I validate ISO 639-2 language codes with Delphi? I created my regular expression as:

^[a-zA-Z]{2,3}([-\/][a-zA-Z]{2,3})?

This works correctly in php, but in Delphi it does not validate the strings I pass.

Valid strings are:

XX
XXX    
XX-XX
XXX-XX
XXX-XXX
XX-XXX

XX/XX
XXX/XX
XXX/XXX
XX/XXX

What did I do wrong ?


Solution

  • You need to add an end of string anchor at the end:

    ^[a-zA-Z]{2,3}([-\/][a-zA-Z]{2,3})?$
    

    See demo