简体   繁体   English

用正则表达式java匹配字符串数组内容

[英]matching string array content with regular expression java

i am new to java. 我是Java新手。 i am trying to use java regular expression to compare two string arrays containing names and then use a nested for loop to print out the names that have a match and names that don't have a match. 我正在尝试使用Java正则表达式比较包含名称的两个字符串数组,然后使用嵌套的for循环打印出具有匹配项的名称和不具有匹配项的名称。 below is my code 下面是我的代码

public class Main {

    public static void main(String[] args) {

    // write your code here
        String regex;
        Pattern pattern;
        String stringToBeMatched;
        Matcher matcher;
        boolean b;
     String [] names1 = {"Peter","Harry","Potter","Mary","Jerry"};
        String [] names2 = {"Adam","Jerry","Potter","Martin","Chris", "Rose"};
        //try a loop with two variables for loop

                for (int x=0;x >= names2.length;x++) {
                     regex = names2[x];
                     pattern = Pattern.compile(regex);

                    for(int y = 0; y >= names1.length; y++){
                        stringToBeMatched = names1[y];
                        matcher = pattern.matcher(stringToBeMatched);
                        b = matcher.find();

                        if (b) {System.out.println(names1[y] +" has a match");}
                        else{System.out.println(names1[y] +" has no match");}
                    }
                }
    }
}

the code executes and returns the output Process finished with exit code 0 without displaying any message specified by any of the System.out.println() statements. 该代码将执行并返回Process finished with exit code 0结尾的输出Process finished with exit code 0而不会显示任何System.out.println()语句指定的消息。 please guys, what am i not doing right? 拜托,我在做什么不对?

Unless I am misunderstanding your intentions, you should not use Regex here. 除非我误解了您的意图,否则您不应在此处使用Regex。 My understanding is you want to compare two arrays for duplicate elements. 我的理解是您想比较两个数组中重复的元素。

You have your arrays: 您有数组:

String[] names1 = {"Peter", "Harry", "Potter", "Mary", "Jerry"};
String[] names2 = {"Adam", "Jerry", "Potter", "Martin", "Chris", "Rose"};

Method 1 - nested for loops. 方法1-嵌套循环。

for(int i = 0; i < names1.length; i++) {
  for(int j = 0; j < names2.length; j++) {
    if(names1[i].equals(names2[j])) System.out.println(names1[i] + " is in both arrays.");
  }
}

Method 2 - nested foreach loops. 方法2-嵌套的foreach循环。

for(String name1 : names1) {
  for(String name2 : names2) {
    if(name1.equals(name2)) System.out.println(name1 + " is in both arrays.");
  }
}

Method 3 - using a List . 方法3-使用列表

List<String> names2List = Arrays.asList(names2);

for(String name1 : names1) {
  if(names2List.contains(name1)) System.out.println(name1 + " is in both arrays.");
}

Method 4 - using a Set . 方法4-使用Set This works because HashSet#add() will NOT add duplicates, and returns false if requested. 之所以可行,是因为HashSet#add()不会添加重复项,如果需要,则返回false。

Set<String> names2Set = new HashSet<>(Arrays.asList(names2));

for(String name1 : names1) {
  if(!names2Set.add(name1)) System.out.println(name1 + " is in both arrays.");
}

Method 5 - using Streams . 方法5-使用 If you want to get fancy. 如果你想得到幻想。

for(String name1 : names1) {
  if(Arrays.stream(names2).anyMatch(name1::equals)) System.out.println(name1 + " is in both arrays.");
}

With lambda functions use "distinct" to not print duplicate values. 对于lambda函数,请使用“ distinct”不打印重复值。

        String [] names1 = {"Peter","Harry","Potter","Mary","Jerry"};
        String [] names2 = {"Adam","Jerry","Potter","Martin","Chris", "Rose", "Peter", "Peter"};
        for (String name1:
             names1) {
            Arrays.stream(names2).filter(name1::equals).distinct().forEach(System.out::println);
    }

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

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