[Python] regex to find a string contains words with both letters and digits

Discussão em 'Python' iniciado por Stack, Julho 12, 2025.

  1. Stack

    Stack Membro Participativo

    I am trying to write a regex to find a string contains words with both letters and digits (NOT A CHECK FOR ALPHANUMERIC because word should strictly contain both letters and digits).

    I have come up with this ^(?=.*[a-zA-Z])(?=.*[0-9])

    Here's the link to the above regex: https://regex101.com/r/Y97ykL/1

    Above regex works for simple scenarios like below


    1. a1b4cc -> true (contains both letters and digits)


    2. abc -> false (contains only letters)


    3. 123 -> false (contains only digits)

    But there are few scenarios which I am not able to cover with the above regex. How can I update my above regex to also work for the below examples?


    1. abc d77hv -> True ( Since efg123 contains both letter and digits)


    2. 798 h7ghd -> True (Since h7ghd contains both letters and digits)


    3. 798 abcd -> False (Since there is no word containing both letter and digits)

    I am not too familiar with regex but is it possible to write a regex to do the check for the above described scenarios without iterating over each word in a string? Thanks for the help!

    Continue reading...

Compartilhe esta Página