This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Help with a RegEx? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply locked New topic
Author

Help with a RegEx?

andi miami
Greenhorn

Joined: Oct 05, 2011
Posts: 10
Hi! I am almost done with the project y'all have been helping me on, (thanks!), but I can't seem to some up with a RegEx that works for this scenario. Can someone help me?

It must have at least one special character out of these:

!@#$%^

and it has to be made of either letters (any case), numbers, and the special characters !@#$%^&*()_-+<>?:

So, I think it should use the look ahead ?= !@#$%\\^ and then do I need the + sign for one or more instances?

and then any word character including newline is \\w* (0 or more instances)

and any digit is \\d* (0 or more instances)

and then just add in the special characters I am missing, but how do I string this all together? I've tried many ways and I keep getting either a syntax error or

it just doesn't match test passwords that meet these requirements.

Thanks for helping...again! Hahaha.
Harsha Smith
Ranch Hand

Joined: Jul 18, 2011
Posts: 287
Can you rephrase your problem? is it the password validation you are trying to do? what are the requirements(min and max number of characters, mandatory characters etc)
andi miami
Greenhorn

Joined: Oct 05, 2011
Posts: 10
It is sort of like a password validation, but it is just a regex matcher type thing. No need to worry about min or max chars. The requirements are simply that which I have listed:

It MUST have at least one special character out of these:

!@#$%^

and it can only be made of either letters (any case), numbers, and any of the the special characters !@#$%^&*()_-+<>?:

So, the letters, numbers and symbols above can be 0 or more instances.

If the test password was only !@#$%^ it would be valid, technically. Make sense? I might be phrasing it badly. I just can't seem to get it grouped correctly. Thanks for taking a look...

Harsha Smith
Ranch Hand

Joined: Jul 18, 2011
Posts: 287
Check this temporary solution while one of us will get back to you with regex soon.
Harsha Smith
Ranch Hand

Joined: Jul 18, 2011
Posts: 287
Try this code...hope it helps

fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11150
    
  16

Is there a requirement that is has to be done with a regex? and that it has to be done with a SINGLE regex?

Sometimes it's easier to make multiple passes over the candidate string, each time validating part of the requirements..


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37890
    
  22
fred rosenberger wrote:Is there a requirement that is has to be done with a regex? and that it has to be done with a SINGLE regex?

Sometimes it's easier to make multiple passes over the candidate string, each time validating part of the requirements..
That may mean that your expression does not arise from a regular grammar. You can only use regular expressions to parse something arising from a regular grammar.
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3415
    
  12
Harsha Smith wrote:Try this code...hope it helps


That won't work for the second part of the requirement. Neither will your temporary solution.
it can only be made of either letters (any case), numbers, and any of the the special characters !@#$%^&*()_-+<>?:

Try
String password = "#.,";

Joanne
Harsha Smith
Ranch Hand

Joined: Jul 18, 2011
Posts: 287
@Joanne Neal
would you mind retrying? I'll wait for the thread starter's response
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3415
    
  12
Harsha Smith wrote:@Joanne Neal
would you mind retrying? I'll wait for the thread starter's response

I ran this code and it printed out true, but that's an invalid password.
andi miami
Greenhorn

Joined: Oct 05, 2011
Posts: 10
Hi all... sorry for the delayed reponse... I had to drive out of town for my job and I just got to my destinantion. Ok, so I will test what y'all responded with and let you know. Thanks!
Harsha Smith
Ranch Hand

Joined: Jul 18, 2011
Posts: 287
I edited the temp. solution. Please tell me if it works.

For one final time, can you tell us what are all characters that are not allowed?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37890
    
  22
Harsha Smith wrote:I edited the temp. solution. . . .
Don’t. I have told you not to do that before. I shall reopen this thread when I get a PM with the exact changes you made, so I can undo them.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Help with a RegEx?
 
Similar Threads
Regular Expression Help
regarding regular expression
Regular expression - help
escape punctuation in string
Arabic unicode characters in HTML