0

i want to delimite "/*" in my string:

for example

String str="6666 /* 555 / 777 * ttt";

// And result should be
result= 6666 ,  555 / 777 * ttt

i wrote the program with

String[] line_split=line_str.split("/*");

and i get this error:

Exception in thread "main" java.util.regex.PatternSyntaxException: Dangling meta character '*' near index 0 *

anyone has a solution for me?

Rohit Jain
  • 209,639
  • 45
  • 409
  • 525
user1720281
  • 35
  • 1
  • 2
  • 6
  • 1
    One fairly obvious solution is to google for 'java.util.regex.PatternSyntaxException: Dangling meta character '*' near '... – home Nov 07 '12 at 19:59

3 Answers3

5

String.split takes Regex for splitting. And * is a meta-character in Regex which has special meaning - matches 0 or more of pattern preceding it.

You need to escape that: -

String[] line_split=line_str.split("/\\*");
Rohit Jain
  • 209,639
  • 45
  • 409
  • 525
2

The error message says:

Dangling meta character '*' near index 0 *

* is a regex metacharacter, you need to escape it with \\.


In such a situation you can use Pattern class to build your regex as follows:

String regex = Pattern.quote("/*");
System.out.println(regex);

The output is

\Q/*\E

which is your regex.

Bhesh Gurung
  • 50,430
  • 22
  • 93
  • 142
0

So yes, it's a meta. But the expression in question, /* is a valid regex. This actually works, only doesn't give the expected result:

System.out.println(Arrays.toString("aoeuaoue".split("/*")));

Therofore you have not pasted code that reproduces your problem. But I suppose the real string to replace is something similar, but illegal as a regex.

Marko Topolnik
  • 195,646
  • 29
  • 319
  • 436