Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Split using a bracket

Tags:

java

regex

split

How can I split a string using [ as the delimiter?

String line = "blah, blah [ tweet, tweet";

if I do

line.split("[");

I get an error

Exception in thread "main" java.util.regex.PatternSyntaxException: Unclosed character class near index 1 [

Any help?

like image 772
Julio Diaz Avatar asked Nov 15 '11 19:11

Julio Diaz


Video Answer


1 Answers

The [ is a reserved char in regex, you need to escape it,

line.split("\\[");
like image 110
Andrew Avatar answered Sep 23 '22 21:09

Andrew