Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Regular expression - help

 
Chitra Jay
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot!! It worked
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic