如何在java中找到相应的字符串模式集?

时间:2022-09-22 23:51:44

I have a string

我有一个字符串

             "yes 12 /12 /yes /n
              yes 12 /12 /yes "

How do I check that whenever I have "yes" in the string, I have a corresponding "/yes" and similarly, whenever I have "12", I have a corresponding "/12"? For example, if the string as

如果字符串中的“是”,我怎么检查,我有一个相应的“/ yes”,同样,只要我有“12”,我就有一个相应的“/ 12”?例如,如果字符串为

             "yes 12 /12 /yes /n 
              yes 12 /12 "

It should give me an error saying error in line 2, and then move on reading the rest of the file.

它应该在第2行给出错误说错误,然后继续读取文件的其余部分。

4 个解决方案

#1


0  

First you need to get a list of all words. Then for each word, try to see if /word is contained.

首先,您需要获取所有单词的列表。然后对于每个单词,尝试查看是否包含/ word。

String s = "12 a /12";
List<String> list = Arrays.asList(s.split("\\s"));
for (String value : list) {
  if (list.contains("/" + value)) {
    System.out.println("Does contain");
  } else {
    System.out.println("Doesn't contain");
  }
}

#2


0  

You can use contains method in String class:

您可以在String类中使用contains方法:

String str = ""yes 12 /12 /yes /n";
boolean isOk = str.contains("yes") && str.contains("/yes"); // isOk = true

str = "yes 12 /12 ";
isOk = str.contains("12") && str.contains("/12") // isOk = false

#3


0  

use String.contains() method

使用String.contains()方法

String s= "yes 12 /12 /yes /n"
if(s.contains("yes") && s.contains("/yes")){
   sysout("yes");
}

do the same thing for any other pair :)

对任何其他对做同样的事情:)

#4


0  

You can use some recursion to find the tags and the corresponding closing tag:

您可以使用一些递归来查找标记和相应的结束标记:

public class TagMatching {
    public static void main(String[] args) {
        List<String> lines = Arrays.asList("yes 12 /12 /yes /n",
                                           "yes 12 /12 /yes",
                                           "yes 12 a b /12 /yes",
                                           "yes 12 c /12 /yes");
        for (String line : lines) {
            boolean valid = validate(Arrays.asList(line.split(" ")));
            System.out.println("valid = " + valid);
        }
    }

    public static boolean validate(List<String> tags) {
        if (tags.size() == 0) {
            return true;
        }

        String first = tags.get(0);
        String last = tags.get(tags.size() - 1);
        if (last.equals("/" + first)) {
            return validate(tags.subList(1, tags.size()-1));
        }

        return false;
    }
}

#1


0  

First you need to get a list of all words. Then for each word, try to see if /word is contained.

首先,您需要获取所有单词的列表。然后对于每个单词,尝试查看是否包含/ word。

String s = "12 a /12";
List<String> list = Arrays.asList(s.split("\\s"));
for (String value : list) {
  if (list.contains("/" + value)) {
    System.out.println("Does contain");
  } else {
    System.out.println("Doesn't contain");
  }
}

#2


0  

You can use contains method in String class:

您可以在String类中使用contains方法:

String str = ""yes 12 /12 /yes /n";
boolean isOk = str.contains("yes") && str.contains("/yes"); // isOk = true

str = "yes 12 /12 ";
isOk = str.contains("12") && str.contains("/12") // isOk = false

#3


0  

use String.contains() method

使用String.contains()方法

String s= "yes 12 /12 /yes /n"
if(s.contains("yes") && s.contains("/yes")){
   sysout("yes");
}

do the same thing for any other pair :)

对任何其他对做同样的事情:)

#4


0  

You can use some recursion to find the tags and the corresponding closing tag:

您可以使用一些递归来查找标记和相应的结束标记:

public class TagMatching {
    public static void main(String[] args) {
        List<String> lines = Arrays.asList("yes 12 /12 /yes /n",
                                           "yes 12 /12 /yes",
                                           "yes 12 a b /12 /yes",
                                           "yes 12 c /12 /yes");
        for (String line : lines) {
            boolean valid = validate(Arrays.asList(line.split(" ")));
            System.out.println("valid = " + valid);
        }
    }

    public static boolean validate(List<String> tags) {
        if (tags.size() == 0) {
            return true;
        }

        String first = tags.get(0);
        String last = tags.get(tags.size() - 1);
        if (last.equals("/" + first)) {
            return validate(tags.subList(1, tags.size()-1));
        }

        return false;
    }
}