Reputation: 61
I have this text tokenized as follows:
∅habbaz∅abdelkrim∅habbaz∅abdelkrim∅habbaz∅abdelkrim
I want to get every string between the character ∅
. I have tried the following:
ArrayList<String> ta = new ArrayList();
String test=t2.getText();
String str = test;
Pattern pattern = Pattern.compile("∅(.*?)∅");
Matcher matcher = pattern.matcher(str);
while (matcher.find()) {
ta.add(matcher.group(1));
}
t3.setText(ta.toString());
It's supposed to give me:
[habbaz,abdelkrim, habbaz,abdelkrim, habbaz,abdelkrim]
But it's giving me only:
[habbaz, habbaz, habbaz]
Upvotes: 1
Views: 54
Reputation: 56
If you want to go with the regex solution, try this:
Pattern pattern = Pattern.compile("∅([^∅]*)");
This pattern will match a ∅ followed by any number of non-∅, which should do the trick.
Upvotes: 1
Reputation: 11740
Use split
:
String input = "∅habbaz∅abdelkrim∅habbaz∅abdelkrim∅habbaz∅abdelkrim";
String[] tokens = input.split("∅");
This will produce an array of those strings that are between your delimiter. Note that the first string in the array will be ""
, the empty string, because your input string starts with the delimiter ∅
. To avoid this, take a substring of the input right before you split (if (input.startsWith("∅")) {input = input.substring(1);}
), or process the resulting tokens to exclude any empty strings.
To turn the tokens into your ArrayList
, use the following:
ArrayList ta = new ArrayList<Element>(Arrays.asList(tokens))
Or you could just write:
List ta = Arrays.asList(input.split("∅"));
Upvotes: 0