Reputation: 5565
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?
Upvotes: 1
Views: 82
Reputation: 11909
Simply use this?
String dataArray[] = data.split("'");
->
[calls , create, using , POST, on some uri]
Upvotes: 1
Reputation: 9648
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
Upvotes: 1