wood burning stoves
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
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Regular expression - help" Watch "Regular expression - help" New topic

Regular expression - help

Chitra Jay
Ranch Hand

Joined: May 02, 2002
Posts: 76
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
Ulf Dittmer

Joined: Mar 22, 2005
Posts: 42965
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.
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
It's not a secret anymore!