1

I was working on a task to parse a cucumber feature string where I need to split string into 5 like the following.

String data = "calls 'create' using 'POST' on some uri";

I was implementing the basic split functionality multiple times (without any regex which is very tedious) to generate the data into the following.

String dataArray[] = {"calls '","create","' using '","POST", "' on some uri"};

I wanted to obtain the names of dataArray[1] and dataArray[3]. Is there a way to generate the above dataArray using regex and split or some other straight forward method?

1
  • 1
    So why "calls '" is one token but `"create'" is not? Commented Feb 27, 2016 at 12:20

2 Answers 2

1

Simply use this?

String dataArray[] = data.split("'");
->
[calls , create,  using , POST,  on some uri]
Sign up to request clarification or add additional context in comments.

Comments

1

Here is a solution which use Regex:

public static void main (String[] args) {
  String data = "calls 'create' using 'POST' on some uri";
  String[] dataArray = new String[2];
  Matcher matcher = Pattern.compile("'[a-zA-Z]+'").matcher(data);
  int counter = 0;
  while (matcher.find()) {
    String result = matcher.group(0);
    dataArray[counter++] = result.substring(1, result.length() - 1);
  }
}

Output:

dataArray[0] --> create
dataArray[1] --> POST

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.