简体   繁体   English

如何在两个字符串或两个字符之间获得许多字符串

[英]how to get many strings between two strings or two characters

I have this text tokenized as follows: 我将此文本标记如下:

∅habbaz∅abdelkrim∅habbaz∅abdelkrim∅habbaz∅abdelkrim ∅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] [哈巴兹,哈巴兹,哈巴兹]

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. 此模式将匹配∅,然后再加上任意数量的non-∅,这可以解决问题。

Use split : 使用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. 为了避免这种情况,请在分割之前( if (input.startsWith("∅")) {input = input.substring(1);} )使用输入的子字符串,或者处理结果标记以排除任何空字符串。

To turn the tokens into your ArrayList , use the following: 要将标记转换为ArrayList ,请使用以下命令:

ArrayList ta = new ArrayList<Element>(Arrays.asList(tokens))

Or you could just write: 或者您可以这样写:

List ta = Arrays.asList(input.split("∅"));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM