• Post Reply Bookmark Topic Watch Topic
  • New Topic

Problem with getting Java Regular Expression to work  RSS feed

 
Russell West
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am attempting to generate a Java regular expression that validates input against alphanumeric (a-zA-Z0-9) and all special characters. After researching on the internet for suggested solutions and trying various options I have not been able to establish a regular expression that works for all the mentioned characters. Following is some related code that I have developed and am attempting to get to work:



Here are the issues experienced:

1) If I leave the dash character "-" n the regular expression String VALID_USERNAME_PATTERN attribute and input any data into the usernameText JTextField component an exception is thrown with the error message
Illegal character range near index 36
^[a-zA-Z0-9@!#$^%*()+=[{]}|:;<>,.?_-/"'\ ]+$

2) If I attempt to escape the dash character within the regular expression String VALID_USERNAME_PATTERN attribute as \- or \-\ the associated line is flagged to have a syntax error within the Eclipse DTE with the
following error:
Invalid escape sequence (valid ones are \b \t \n \f \r \" \' \\)

So in order to prevent the exception mentioned in item 1 above I have to remove the dash character from the regular expression String VALID_USERNAME_PATTERN attribute

3) If I input a back slash "\" character in the usernameText JTextField component it foreground color is set to RED. The escape sequence \\ toward the end of the regular expression String VALID_USERNAME_PATTERN attribute does
not allow the backslash to be a valid character.

Thanks for any assistance in resolving these issues since I have exhausted none options.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Remember that backslash is itself a special character in a string, so to get a backslash in a regular expression string, you need to use \\.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dumb question #1: since you seem to be allowing any character, why check at all?

Dumb question #2: why check at all and not let people use whatever they want? You know, because limiting the space actually descreases security.
 
Paul Clapham
Sheriff
Posts: 22835
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have lost count of the number of times I have not been allowed to use "Paul Clapham" as my user name on some web site.
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nobody likes a round of applause?
 
Campbell Ritchie
Marshal
Posts: 56581
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:. . . not been allowed to use "Paul Clapham" as my user name on some web site.
Do they know something we don't know about you?
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Russell West wrote:I am attempting to generate a Java regular expression that validates input against alphanumeric (a-zA-Z0-9) and all special characters.

So you mean I'm allowed a username of '#%@%$!!!FtangFtangBusStopOleBiscuitBarrel'?

There's nothing intrinsically wrong with it, but you might find yourself with some very odd user names if you don't rein in the "creativity" a bit.

Also: Allowing '@' in a username that isn't an e-mail address might cause you some difficulties later on.

HIH

Winston
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!