Got more questions? Find advice on: ASP | SQL | XML | Windows
in Search
Welcome to RegexAdvice Sign in | Join | Help

Regex to allow few special characters only

Last post 03-05-2010, 3:37 AM by killahbeez. 3 replies.
Sort Posts: Previous Next
  •  03-04-2010, 8:55 AM 60344

    Regex to allow few special characters only

    Hi,

    I do have following regex currently used in String.matches(regex) of java application:-

    ((?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[\W])(?!.*[\s]).*)

    This regex is used to enforce password policy. But we have integrated new application which uses same LDAP but does not support all special characters in password.

    So we need to modify this regex to allow only few special characters ($ # and _) i.e. characters which supported by other application.

    I tried replacing \W with limited set of $ # and _ but have no success.

    Please help,

    Thanks,

    Archit K

  •  03-04-2010, 2:24 PM 60350 in reply to 60344

    Re: Regex to allow few special characters only

    ((?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[$#_])(?!.*[\s]).*)

    this means that your regexp will match on a string which contains at least  1 digit + 1 lowercase alpha + 1 uppercase alpha + 1 of any $ # _ + not having any space

     I hope I understand well your requirements, if not be more specific


    http://portal-vreme.ro
  •  03-04-2010, 11:02 PM 60387 in reply to 60350

    Re: Regex to allow few special characters only

    Hi,

    Requirements mentioned by you are correct and I am trying to implement these requirements.

    But problem with regex suggested by you is it matches "1aZ$" but also"1aZ$@".

    So in summary,my requirements are:-

    1) String has at least  1 digit + 1 lowercase alpha + 1 uppercase alpha + 1 of any $ # _

    2) String has no space and special characters other than $ # _

    3) String can have characters in any order/quantity as far as it is digit,lowercase/uppercase alpha, $,# and _ characters.

    Thanks,

    Archit K

  •  03-05-2010, 3:37 AM 60396 in reply to 60387

    Re: Regex to allow few special characters only

    So,

    Still not very specific.

    If you want that the string SHOULD only have those characters, you can do ^([\w$#]+)$

    If you want to make sure that it will contain only those characters, but insist to have at least one of them, you can do ^((?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[$#_])(?!.*\s)[\w$#]+)$


    http://portal-vreme.ro
View as RSS news feed in XML