Java string separated by multi-character delimiter

I am new to Java and I thought this worked the same way as with other languages.

For the 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 break it on each ||## .

But:

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

returns:

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

... etc.

I expected:

 3::Daniel::Louis Leon: the Professional 

... etc.

What am I doing wrong?

+4
source share
7 answers

You need to avoid | character since it is a regular expression metacharacter for a logical OR

Therefore i would use

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

Please note that you also need to avoid the slash, so I use

 \\| 

instead

 \| 

To avoid this metacharacter

+12
source
 public String[] split(String regex) 
+2
source

You need to avoid lines: | - a special character in regular expression.

Using:

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

Alternatively, you can use \Q\E to force the use of the whole pattern:

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

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

| allows you to specify additional patterns in the regular expression. Your regex is equivalent to |## , or: nothing OR ##. It splits around an empty string or between each character in the input.

See javadoc for Pattern .

0
source

You should avoid your characters | :

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

You need to escape from '|' how is it | |

0
source

It looks like you want something like this:

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

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

I don’t know if the above example will work specifically for your scenario; you may need to experiment a bit (for example, "escape" the regular expression metacharacters with "\").

Here's the Javadoc for Pattern.compile:

And here is some more info on Java regex syntax:

0
source

Gilberto's solution will work fine in this case, but you can check guava . It has many useful utility classes, including a line separator. With it, you can 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); 
0
source

Source: https://habr.com/ru/post/1433630/


All Articles