简体   繁体   English

嵌套字数的java正则表达式

[英]java regular expression on nested word count

I have String of text that i need to find does it have the nested loop or not Can i use regular expression to find the nested loop counts ?我有我需要查找的文本字符串它是否具有嵌套循环我可以使用正则表达式来查找嵌套循环计数吗?

So my example of text is :所以我的文本示例是:

Library-Bookshelf-Book-Book-EndBookShelf-BookShelf-Book-EndBookShelf-Library

I want to search for: Whether the String belongs to the Library that has more than 1 Bookshelf in one at least has more than 1 books ?我想搜索: Whether the String belongs to the Library that has more than 1 Bookshelf in one at least has more than 1 books ? . . Would it be possible with regular expression.是否可以使用正则表达式。 I appreciate any other ideas.我很欣赏任何其他想法。 thanks.谢谢。

So input is the above example string.所以输入是上面的示例字符串。 Required output would be first match found.所需的输出将首先找到匹配项。 I need to split these for the first occurrence.我需要在第一次出现时拆分这些。

Bookshelf-Book-Book-EndBookShelf
BookShelf-Book-EndBookShelf

But my regular expression returning:但我的正则表达式返回:

Bookshelf-Book-Book-EndBookShelf-BookShelf-Book-EndBookShelf
BookShelf-Book-EndBookShelf

Test case 2:测试案例2:

Library-Bookshelf-Book-author-author-location-Book-author-EndBookShelf-BookShelf-Book-author-EndBookShelf-Library

with the regular expression i need to find whether the bookshelf in a library has more than 2 books and in that for one book it has more than one author.使用正则表达式,我需要查找图书馆中的书架是否有超过 2 本书,而对于一本书,它的作者是否超过一位。 Tescase 2 should return one match because the first part Bookshelf-Book-author-author-location-Book-author-EndBookShelf satisfy the condition. Tescase 2 应该返回一个匹配项,因为第一部分 Bookshelf-Book-author-author-location-Book-author-EndBookShelf 满足条件。

Thanks..谢谢..

how about something like this这样的事情怎么样

public static void main(String[] args) {
    // TODO Auto-generated method stub

    String x = "Library-BookShelf-Book-author-author-location-Book-author-EndBookShelf-BookShelf-Book-author-EndBookShelf-Library";
    List<String> bookShelves = fun(x, "BookShelf", "EndBookShelf");
    for (String book : bookShelves)
    {
        List<String> authors = null;
        if(book.indexOf("Book") == book.lastIndexOf("Book"))
            authors = fun(book, "Book", "Book");
        else
            authors = fun(book, "Book", "EndBookShelf");
    }
}

public static List<String> fun(String input, String startTag, String endTag){

    String [] c = input.split("-");
    List<Integer> shelfIndices = getIndices(c, startTag, endTag);

    List<String> answer = new ArrayList<String>();
    int i=0;
    while (i<shelfIndices.size()-1)
    {
        int start = shelfIndices.get(i);
        int end = shelfIndices.get(i+1);
        StringBuilder res = new StringBuilder();
        res.append(startTag+"-");
        for (int j=start+1; j<end; j++)
        {
            res.append(c[j]+"-");
        }
        res.append(endTag);
        answer.add(res.toString());
        i+=2;
    }
    for(String r: answer)
        System.out.println(r);
    return answer;
}

public static List<Integer> getIndices(String [] c, String startTag, String endTag){

    List<Integer> indices = new ArrayList<Integer>();

    for (int i=0; i<c.length; i++)
    {
        if (c[i].equals(startTag) || c[i].equals(endTag))
        {
            indices.add(i);
        }
    }

    return indices;
}

this prints out这打印出来

BookShelf-Book-author-author-location-Book-author-EndBookShelf
BookShelf-Book-author-EndBookShelf
Book-author-author-location-EndBookShelf
Book-author-EndBookShelf

Try Regex: Bookshelf-(?:Book-)*EndBookShelf尝试正则表达式: Bookshelf-(?:Book-)*EndBookShelf

Demo演示

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

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