在列表中搜索单词,然后获取列表中两点之间的所有字符串

发布于 2024-12-11 20:00:13 字数 424 浏览 0 评论 0原文

我有一个巨大的名称列表,我需要在该列表中搜索单词或数字,然后取出单独列表中的所有字符串。

名单是这样的。

|玛丽·简,1990 年,棕色|,|亨利·罗林斯,1974 年,红色|

例如,如果我搜索“Mary”,那么应该将 | 之间的所有内容取出并放入单独的列表中。如果此列表中还存在一位玛丽,则也需要将其删除。当找到一个名字时,我首先想到使用 subStringSplit() ,但我惨败了。

如果有人想阅读我的劣质编码,那么 http://data.fuskbugg.se/skalman02/6acb2a0c_DATABASGENERATOR .txt

提前致谢!

I have a huge list of names, I need to search for a word or number within this list and then take out all the strings in a seperate list.

The list is like this.

|Mary Jane, 1990, Brown|,|Henry Rollings, 1974, Red|

If I for example search "Mary", then everything should be taken out between the |'s and put in a seperate list. If one more Mary exists in this list then that needs to be taken out aswell. I first thought of using subStringSplit() when a name was found, but I failed miserably.

If anyone wants to read my shoddy coding, then http://data.fuskbugg.se/skalman02/6acb2a0c_DATABASGENERATOR.txt

Thanks in Advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

月棠 2024-12-18 20:00:13

我推荐使用正则表达式匹配器来完成此类任务。

这是一个应该可以解决问题的代码示例:

String inputList = "|Mary Jane, 1990, Brown|,|Henry Rollings, 1974, Red|,|Mary Mary Mary, 1974, Red|,|Someone Else, 1974, Red|";
        StringBuffer listWithMatchesRemoved = new StringBuffer();
        StringBuffer matchedItems = new StringBuffer();
        String searchString = "Mary";

        Pattern p = Pattern.compile("([|][^|]*?"+searchString+"[^|]*?[|]),?");
        Matcher m = p.matcher(inputList);

        while(m.find())
        {
            if(matchedItems.length()!=0)
                matchedItems.append(",");
            matchedItems.append(m.group(1));

            m.appendReplacement(listWithMatchesRemoved, "");
        }
        m.appendTail(listWithMatchesRemoved);

        System.out.println("Matches:" + matchedItems);
        System.out.println("The rest:" + listWithMatchesRemoved);

I recommend the regular expression Matcher for such tasks.

Here is a code sample which should do the trick:

String inputList = "|Mary Jane, 1990, Brown|,|Henry Rollings, 1974, Red|,|Mary Mary Mary, 1974, Red|,|Someone Else, 1974, Red|";
        StringBuffer listWithMatchesRemoved = new StringBuffer();
        StringBuffer matchedItems = new StringBuffer();
        String searchString = "Mary";

        Pattern p = Pattern.compile("([|][^|]*?"+searchString+"[^|]*?[|]),?");
        Matcher m = p.matcher(inputList);

        while(m.find())
        {
            if(matchedItems.length()!=0)
                matchedItems.append(",");
            matchedItems.append(m.group(1));

            m.appendReplacement(listWithMatchesRemoved, "");
        }
        m.appendTail(listWithMatchesRemoved);

        System.out.println("Matches:" + matchedItems);
        System.out.println("The rest:" + listWithMatchesRemoved);
为你鎻心 2024-12-18 20:00:13

我没有测试,但这应该有效。告诉我这是否行不通。

编辑:我使代码变得更好/更干净。

List<String> searchFor(String search) {
    StringBuilder builder = new StringBuilder(YOUR_HUGE_LIST);
    List<String> list = new LinkedList<String>();

    for (int index = 0; (index = builder.indexOf(search, index)) != -1; ){
        final int start = builder.lastIndexOf("|", index);

        index = builder.indexOf("|", index);

        list.add(builder.substring(start + 1, index));
        builder.delete(start, index + 1);
    }

    return list;
}

I did not test, but this should work. Tell me if this does not work.

EDIT: I made the code a bit better/cleaner.

List<String> searchFor(String search) {
    StringBuilder builder = new StringBuilder(YOUR_HUGE_LIST);
    List<String> list = new LinkedList<String>();

    for (int index = 0; (index = builder.indexOf(search, index)) != -1; ){
        final int start = builder.lastIndexOf("|", index);

        index = builder.indexOf("|", index);

        list.add(builder.substring(start + 1, index));
        builder.delete(start, index + 1);
    }

    return list;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文