my dog learned polymorphism*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Clarification on Regular Expression Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Clarification on Regular Expression" Watch "Clarification on Regular Expression" New topic
Author

Clarification on Regular Expression

P Bala
Greenhorn

Joined: Apr 09, 2007
Posts: 9
Hi

I have the follow regex pattern as
private Pattern pattern = Pattern.compile("^[a-zA-Z_0-9 -@#$]*$");

when i tried matching this pattern to a string "test@test#asd$!com", it passes when i expected to fail.

Could anyone please help me undersand.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18138
    
  39

when i tried matching this pattern to a string "test@test#asd$!com", it passes when i expected to fail.


The issue is the "[a-zA-Z_0-9 -@#$]" part of the pattern. You are trying to match... a to z, A to Z, the underscore, 0 to 9, space to @ sign, the hash, and the dollar sign.

I am assuming that the reason you "expected [it] to fail" is because you believe that the "!" character is not on that list. Unfortunately, it is. It is in the "space to @ sign" range.

Henry
[ December 20, 2007: Message edited by: Henry Wong ]

Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8764
    
    5
FYI - that's a MUCH harder expression then you'll find on the real exam!


Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
P Bala
Greenhorn

Joined: Apr 09, 2007
Posts: 9
Oh... Thanks Henry. I was wanting to include space, dash(-),@,#,$ signs. I want to exclude !,^, (),<>,;,:,* and words like (SELECT),(TABLE). I seek help in framing such a regular expression.

I sincerely thank you in advance.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18138
    
  39

Originally posted by P Bala:
Oh... Thanks Henry. I was wanting to include space, dash(-),@,#,$ signs. I want to exclude !,^, (),<>,;,:,* and words like (SELECT),(TABLE). I seek help in framing such a regular expression.

I sincerely thank you in advance.


If you want the dash as a literal character, and not as a range designator, then you have two choices. You can escape it with a backslash. Or you can make it the first or the last character between the square brackets -- as it can't be a range designator when there isn't a range.

Henry
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Clarification on Regular Expression
 
Similar Threads
not able to call the servlet
Regular expression
mapping a servlet to multiple urls.
Spring 3 @PathVariable problem with slash
backslash is removed from the parameter when javascript function get the parameter from java call