0

There is a string which I trying to parse by "|" symbol:

1-20|21-40|41-60|61-80|81-100|101-120|121-131

String[] arr = text.split("|");

for(int i = 0; i <arr.length; i++){
    System.out.println( arr[i] );
}

It parses to every character, like

1
-
2
0
|
2
1
...

How to parse the source string for elements like:

1-20

Mureinik
  • 277,661
  • 50
  • 283
  • 320
thinker
  • 382
  • 4
  • 15

4 Answers4

1

| is a special character in Java's regex syntax that means a logical "or" between two matching groups. If you want to match the | literal, you need to escape it:

String[] arr = text.split("\\|");
Mureinik
  • 277,661
  • 50
  • 283
  • 320
0

This | is a special character in regular expression(s), you need to escape it. Like,

String[] arr = text.split("\\|");
Elliott Frisch
  • 191,680
  • 20
  • 149
  • 239
0

| is a metacaracter in regex. Escape it:

String[] splitValues = text.split("\\|");
Rouliboy
  • 1,347
  • 1
  • 6
  • 21
0

escape the pipe using "\\|"

String[] arr = text.split("\\|");
Zoe stands with Ukraine
  • 25,310
  • 18
  • 114
  • 149
ΦXocę 웃 Пepeúpa ツ
  • 45,713
  • 17
  • 64
  • 91