0

I have a string #JSGF V1.0;grammar numbers;public <accion> = (one| two| three); I want the numbers: one, two and three.

I did this String answer = res.substring(res.indexOf("(")+1,res.indexOf(")")); and obtain one| two| three, but Im having trouble in this part.

Ideas?

lololo wote
  • 113
  • 1
  • 1
  • 6

3 Answers3

2

You can get the numbers as array using

String numbers[] = answer.split("\\s*\\|\\s*");

\\s*\\|\\s*: 0 or more spaces then | symbol and 0 or more spaces

Pavneet_Singh
  • 36,884
  • 5
  • 53
  • 68
0
String res = "(one| two| three);";
String answer = res.substring(res.indexOf("(")+1,res.indexOf(")"));
for(String str : answer.split("\\s*\\|\\s*")) {
    System.out.println(str);
}
kavai77
  • 6,282
  • 7
  • 33
  • 48
Sree
  • 374
  • 2
  • 10
0

split the answer on non-word characters:

public static void main(String[] args) {
    String res = "JSGF V1.0;grammar numbers;public <accion> = (one| two| three);";
    String answer = res.substring(res.indexOf("(") + 1, res.indexOf(")"));
    String[] numbers = answer.split("[^\\w]+"); // split on non-word character
    for (String number : numbers) {
        System.out.println(number);
    }
}

output:

one
two
three
xingbin
  • 27,410
  • 9
  • 53
  • 103