• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Escape character for []

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 !
 
Rancher
Posts: 989
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Backslash itself is also special character in Java and so has to be escaped by another backslash.
 
reply
    Bookmark Topic Watch Topic
  • New Topic