• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Password Regex

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm struggling to figure out a single regular expression which will match a password having the following rules:

o Passwords must be 6-16 characters.
o Allowed characters are alphanumeric, hyphen(-) and underscore(_)
o They must contain both numbers and letters

Currently the code uses 3 regular expressions to accomplish the goal:


And then to check the password:



What is the easiest way to construct a single regular expression combining these 3?
 
author & internet detective
Posts: 41967
911
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Scott,
The easiest thing to do is leave them separate. I can't think of any way to check all three of these things. Note your requirements say the max length is 16 and your reg exp is 24. I assume one is a typo, but I figured I would mention it.
 
Master Rancher
Posts: 5008
79
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can use lookahead or lookbehind operators to look ahead without advancing. Lookahead is probably a little simpler:
 
Scott McGhee
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Mike, that clears it up for me. The lookahead seems to be working, I tested on a few input strings and it looks good.

Also thanks Jeanne for pointing out the typo.
 
Jeanne Boyarsky
author & internet detective
Posts: 41967
911
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Mike,
Interesting! I was puzzled by doing the look ahead for two things. Double lookahead is a good technique to remember!
 
Evacuate the building! Here, take this tiny ad with you:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic