• Post Reply Bookmark Topic Watch Topic
  • New Topic

Escape character for []  RSS feed

 
Gregers Svensson
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey !

I've a method that should split some parts of an String. If the string contains [ or ] it should split them. Like this:

String[] parts = tf.split("[");
String part1 = parts[0];
String part2= parts[1];


Why dosen't this code work?

It says" Invalid regular expression; Unclosed character Class." If i hover over the ("[") part....

Thanks in advance!
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
because in a regex, the bracket has special meaning - it is for a group of characters. So you can say [12378] and it treats them as "match any single one of these".

try using the backslash. I don't KNOW it will work, but I THINK it wil..
 
Gregers Svensson
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote:because in a regex, the bracket has special meaning - it is for a group of characters. So you can say [12378] and it treats them as "match any single one of these".

try using the backslash. I don't KNOW it will work, but I THINK it wil..


Ye, I tried with backslash, didn't work , ''


("\\[*") did it! , or atleast I think it did !
 
E Armitage
Rancher
Posts: 989
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Backslash itself is also special character in Java and so has to be escaped by another backslash.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!