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?

4
  • Take your String answer and use a RegEx on it. Commented May 24, 2018 at 13:37
  • 3
    did you mean answer.split("\\s*\\|\\s*") Commented May 24, 2018 at 13:38
  • Use Matcher and \((.*?\|)+\) Commented May 24, 2018 at 13:41
  • Maybe helpful, stackoverflow.com/questions/986543/… Commented May 24, 2018 at 19:07

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

Comments

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 

Comments

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); } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.