0

我有一个向量向量(没有指定长度)。在此,我想找到一个要搜索的字符串以及它的后续和前置。我已经尝试过了。mainstr 是我转换为字符串的向量。

  String mainstr = "[[data link control], [communication, []], [computer, [applications     of computer, number of computer]], [world wide web], [lesson, [covered in lesson]], [access to remote], [marketing and sale], [electronic fund transfer], [network, [network of network, wide area network, communication network, computer network, [area network, [local area network, metropolitan area network]]]]]";

String search = "communication network";

if (mainstr.contains(search)) {
            if (mainstr.charAt(mainstr.indexOf(search) + search.length()) == ']' && mainstr.charAt(mainstr.indexOf(search) - 2) == '[') {
                System.out.println("single term");
            } else {
                int indexSearch = str.indexOf(search) + search.length();
                String followers = str.substring(indexSearch, str.length());
                if (!followers.equals("")) {
                    System.out.println("No FOLLOWERS");
                } else {
                    System.out.println("followers = " + followers.substring(0, followers.indexOf("]")));
                }
                if (mainstr.charAt(mainstr.indexOf(search) - 4) == ']') {
                    System.out.println("No pre found");
                } else {
                    System.out.println("preups are present");
                    String preup = mainstr.split(search)[0].substring(0, mainstr.split(search)[0].length() - 1);
                    String finalPreup = preup.substring(preup.lastIndexOf("[") + 1, preup.lastIndexOf(","));
                    System.out.println("final : " + finalPreup);
                }
                System.out.println("found...");
            }
        } else {
            System.out.println("Not Found");
        }

在这种情况下,输出看起来像这样 -

No FOLLOWERS
preups are present
final : network of network, wide area network
found...

我已将此向量转换为字符串,然后我执行了搜索,但我得到了某些字符串的正确输出,在这种指定的情况下,我没有得到所需的输出。我对适用于向量中存在的任何字符串的通用代码感兴趣。提前致谢。

更新::

实际上,这是我放入向量向量中的树结构。

-data link control
-communication
-computer
    - applications of computer
    -number of computer
-world wide web
-lesson
    -covered in lesson
-access to remote
-marketing and sale
-electronic fund transfer
-network
    -network of network
    -wide area network
    -communication network
    -computer network
    -area network
           -local area network
           -metropolitan area network

所以我希望搜索将按树进行。例如,如果 search=="wide area network" 那么它的追随者=没有追随者,因为它下面没有层次结构意味着它里面没有元素。及其 Pre up=netwok 因为它是头节点网络的子元素。

提前致谢。

4

1 回答 1

0

我相信您的代码不会输出您想要的内容,因为您需要找到 的倒数第二个索引[,而不是最后一个。而且,一旦修复,我认为您的代码将无法跨越带有子节点的节点。为此,我建议使用带有括号计数器的 while 循环。

下面是我的尝试。它只是一个基本程序 - 没有边界检查等,但它应该让你知道该怎么做。

  String mainstr = "[[data link control], [communication, []], [computer, [applications     of computer, number of computer]], [world wide web], [lesson, [covered in lesson]], [access to remote], [marketing and sale], [electronic fund transfer], [network, [network of network, wide area network, communication network, computer network, [area network, [local area network, metropolitan area network]]]]]";
  String search = "communication network";
  int start = mainstr.indexOf(search);
  if (start != -1)
  {
     int end = start + search.length();
     int count = 0;
     int pos = end;
     if (mainstr.charAt(end+2) == '[')
     {
        while (count != -1)
           if (mainstr.charAt(++pos) == ']')
              count--;
           else if (mainstr.charAt(++pos) == '[')
              count++;
        System.out.println("Ancestors = " + mainstr.substring(end+2, pos-1));
     }
     count = 0;
     pos = start;
     int lastComma = -1;
     while (count != 2)
        switch (mainstr.charAt(--pos))
        {
           case ']': count--; break;
           case '[': count++; break;
           case ',': lastComma = pos;
        }
     System.out.println("Parent = " + mainstr.substring(pos+1, lastComma));
  }

当您要查找的节点是字符串中较早出现的另一个节点的子字符串时,这有点问题。

为此,我认为正则表达式可能会很好地工作:

代替

int start = mainstr.indexOf(search);
if (start != -1)
{

Pattern p = Pattern.compile("(?:^|, |\\[)(" + search + ")(?:]|, |$)");
Matcher m = p.matcher(mainstr);
if (m.find())
{
   int start = m.start(1);

测试

Java 正则表达式参考

于 2013-07-11T14:15:27.257 回答