Search code examples
pythonregexpython-refindall

regex for strings that are Uppercase letters with numbers of minlength 6, and Uppercase letters with numbers of minlength 6 and hyphen and only number


Currently i have this regex:

(?:\d+[A-Z]|[A-Z]+\d)[A-Z-\d]{6,}|[0-A9-Z]{6,}|[0-9]{6,}

I want this regex to match any "string" in a paragraph that is:

Uppercase alphabets with numbers and min length 6

Uppercase alphabets with numbers with hyphen and min length 6

Only numbers with min length 6.

This regex above works, however it still matches only alphabets and i want to exclude this how?

A1A1A1A1A1A1A1A1 --> should match
12222222222222DE --> should match
'PRODUKT', 'PRODUKT', 'NICKNAME', 'PRODUKTNAME' --> shouldn't match any of this but its matching
123456 --> should match
6203-5458 --> shouldn't match
234SS-4NNNAA --> should match

Solution

  • You can use

    (?<![\dA-Z-])(?=[\dA-Z-]{6,})(?:[\d-]+[A-Z]|[A-Z-]+\d)[A-Z\d-]*|[0-9]{6,}
    

    See the regex demo. Details:

    • (?<![\dA-Z-]) - immediately to the left, there should be no digit, uppercase letter or -
    • (?=[\dA-Z-]{6,}) - immediately to the right, there must be 6 or more digits, uppercase letters or -
    • (?:[\d-]+[A-Z]|[A-Z-]+\d)[A-Z\d-]* - one or more digits or - and then an uppercase letter or one or more uppercase letters or hyphens and then a digit and then zero or more uppercase letters, digits or hyphens
    • | - or
    • [0-9]{6,} - six or more digits.