4

I'm fairly new to Java and I thought this worked the same as with other languages.

For a string:

String line = "3::Daniel::Louis||##2::Leon: the Professional::1994||6::Jean::Reno||7::Gary::Oldman||8::Natalie::Portman||##3::Scarface::1983||9::Al::Pacino||10::Michelle::Pfeiffer";

I want to split it at every ||##.

But:

for(String s : line.split("||##")) {
    System.out.println("|"+s+"|");
 }

returns:

||
|3|
|:|
|:|
|D|
|a|
|n|
|i|

... etc.

I was expecting:

3::Daniel::Louis

Leon: the Professional

... etc.

What am I doing wrong?

4

7 回答 7

13

You have to escape the | character since it's a regex metacharacter for logical OR

So I would use

line.split("\\|\\|##"))

Note that You have to escape the slash as well that is why I use

\\|

instead of

\|

To escape that metacharacter

于 2012-09-11T17:37:09.400 回答
2
public String[] split(String regex) 
于 2012-09-11T17:39:42.543 回答
1

It sounds like you want something like this:

Pattern p = Pattern.compile("\\|\\|##", Pattern.LITERAL)  
String[] result = p.split(myString)  

I know you can have multiple characters in your delimiter, and that you can exclude your delimiter from the output string.

I don't know if the example above will work exactly for your scenario; you might have to experiment a bit (for example, "escaping" regex "metacharacters" with "\").

Here's the Javadoc for Pattern.compile:

And here's more information on Java regex syntax:

于 2012-09-11T17:39:32.623 回答
0

You need to escape the bars: | is a special character in the regex.

Use:

for(String s : line.split("\\|\\|##")) {

Alternately, you can use \Q\E to force that the entire pattern be used literally:

for(String s : line.split("\\Q||##\\E")) {

This is probably the same pattern that you'll get from Pattern.quote.

| allows you to specify optional patterns in a regex. Your regex is equivalent to |##, or: nothing OR ##. This splits around the empty string, or between every character in the input.

See the javadoc for Pattern.

于 2012-09-11T17:37:27.180 回答
0

You should escape your | characters:

for (String s : line.split("\\|\\|##"))
于 2012-09-11T17:37:30.377 回答
0

You have to escape the '|' like this \|

于 2012-09-11T17:37:43.403 回答
0

Gilberto's solution will work just fine in this case, but you might want to check out guava. It has a lot of very useful utility classes including a String splitter. With it you could write:

Iterable<String> frags = Splitter.on("||##").split(line);
// Do whatever with the iterable...maybe you just want a list?
// List<String> fragList = Lists.newArrayList(frags);
于 2012-09-11T17:58:08.630 回答