File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Regular expression - help Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Regular expression - help" Watch "Regular expression - help" New topic
Author

Regular expression - help

Chitra Jay
Ranch Hand

Joined: May 02, 2002
Posts: 76
Hi,
I need a regular expression that will search in a string for valid characters. The only allowed (but not mandatory to appear)characters in this string are letters,digits, # and . (period) and they can appear in any order within the string. Any other character would be invalid. All i want to do is check for valid characters and i dont care where it appears. Some valid expressions are abc123.#, abc,123#.a etc..I started off with this regex "^*[a-zA-Z0-9#*.]", but if i enter "@#$%^", this regex returns true. I also tried "^*![a-zA-Z0-9#*.]", but this forces me to have all the above characters in that string.
What am i missing?
I appreciate any help
Thanks
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39547
    
  27
The dot (".") is a meta-character that matches any character. To use the literal dot in a regexp, it needs to be escaped by a backslash. I think "^[a-zA-Z0-9#\.]*$" should do the trick. Note the $ character at the end. It is the opposite of the ^ at the beginning, and ensures that you are matching the whole string, not just a part of it at the beginning. This regexp will not match letter-like symbols like umlauts and accented letters, but I assume that's what you want.


Ping & DNS - updated with new look and Ping home screen widget
Chitra Jay
Ranch Hand

Joined: May 02, 2002
Posts: 76
Thanks a lot!! It worked
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Regular expression - help
 
Similar Threads
Regular Expression
Email regEx- should not start with dot(.)
Regex problem
regexp on email address
Seemingly simple regex making my head hurt