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
  • 297,002
  • 52
  • 306
  • 350
thinker
  • 402
  • 1
  • 6
  • 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
  • 297,002
  • 52
  • 306
  • 350
0

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

String[] arr = text.split("\\|");
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
0

| is a metacaracter in regex. Escape it:

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

escape the pipe using "\\|"

String[] arr = text.split("\\|");
Zoe
  • 27,060
  • 21
  • 118
  • 148
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97