• Post Reply Bookmark Topic Watch Topic
  • New Topic

Special characters in Java  RSS feed

 
Kiran Er
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am having problem with checking the special characters in java

I need to allow only a-z,A-Z, 0-9, space apostropie - dot   in my string

I tried with different string formats which i found in google but this expression [a-zA-Z.\\'\\- ] always throws false.

Could any one please provide me the regex for the same.




Regards,
K
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

The matches() method is for a complete match -- meaning the whole string must match. So, in your example, it can only match if your string is a single character too.

Henry
 
Carey Brown
Saloon Keeper
Posts: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In a regex character set a hyphen is always interpreted as a range indicator unless it appears as the first or last character (no backslashes necessary).
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you need to escape the full stop which appears in that regex?
 
Claudia Joseph
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, theses special characters are false in java scripts. So, they are very difficult to allow in java scripts.
 
Knute Snortum
Sheriff
Posts: 4287
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As Henry Wong said, your regex only matches one character.  You need a quantifier on the end, either "*", which matches zero or more characters (if an empty String is legal) or "+", which matches one or more characters.
 
Kiran Er
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am able to fix with below regex

([a-zA-Z0-9\\-\\s\\'\\.]+)

Regards,
K
 
Knute Snortum
Sheriff
Posts: 4287
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That looks good.

You don't have to escape the apostrophe or the dot in a character class, and a dash at the end is not a metacharacter, so the regex could look like this:

"[a-zA-Z0-9\\s'.-]+"
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, \s is defined as whitespace character: [ \t\n\x0B\f\r] so it includes more than just the space character. This may or may not be what you want.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!