javaregexstring

Suggest a regex that matches a string with letters and (numbers or special characters)


I am developing a sign-up page. In that, I want user to input password with the following format:

Example:

Valid inputs:

abc123 
123abc 
abc!@#
!@#abc

Invalid inputs:

abc
123    
!@#    
123!@#

My work:

((([A-Z]|[a-z]|[0-9])*)(([0-9])|([[\]{}\\|;:'"_+=!@#$%$%^&*()-,<\.>/\?`~])))

It matches only abc123 and abc!@#.


Solution

  • Did you try the following ?

    ((([a-zA-Z]+)([0-9#]+))|(([0-9#]+)([a-zA-Z]+)))[a-zA-Z0-9#]*

    # being replaced by allowed special characters.

    It mandates an alphabet and a special character or a number followed by any number of allowed characters