有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

regex Java regualr表达式匹配并放入数组不起作用

我有一个这样的列表

1300000
1300001
1300002
1300006
1300008
1300010
1300011
1300012
1300013
1300014
1300015
1300016
1300017
1300018
1300019
1300050
1300051
1300052
1300053
1300054
1300055
1300056
1300057
1300058
1300059
1300190
1300191
1300192
1300193
1300194
1300195
1300196
1300197
1300198
1300199

作为^130019.|^130005.这样的正则表达式,应该匹配以下项:

System.out.println( p2 +"--"+ AllHdList.get(i) + "->" + m2.find());

^130019.|^130005.--1300000->false
^130019.|^130005.--1300001->false
^130019.|^130005.--1300002->false
^130019.|^130005.--1300006->false
^130019.|^130005.--1300008->false
^130019.|^130005.--1300010->false
^130019.|^130005.--1300011->false
^130019.|^130005.--1300012->false
^130019.|^130005.--1300013->false
^130019.|^130005.--1300014->false
^130019.|^130005.--1300015->false
^130019.|^130005.--1300016->false
^130019.|^130005.--1300017->false
^130019.|^130005.--1300018->false
^130019.|^130005.--1300019->false
^130019.|^130005.--1300050->true
^130019.|^130005.--1300051->true
^130019.|^130005.--1300052->true
^130019.|^130005.--1300053->true
^130019.|^130005.--1300054->true
^130019.|^130005.--1300055->true
^130019.|^130005.--1300056->true
^130019.|^130005.--1300057->true
^130019.|^130005.--1300058->true
^130019.|^130005.--1300059->true
^130019.|^130005.--1300190->true
^130019.|^130005.--1300191->true
^130019.|^130005.--1300192->true
^130019.|^130005.--1300193->true
^130019.|^130005.--1300194->true
^130019.|^130005.--1300195->true
^130019.|^130005.--1300196->true
^130019.|^130005.--1300197->true
^130019.|^130005.--1300198->true
^130019.|^130005.--1300199->true

我试图使用这里的代码将那些不匹配的数组放入,但我的代码不起作用:

public static void get7HdQh(List<String> AllHdList){

        List <String> tempList  = new ArrayList<String>();

        for (int i=0; i<AllHdList.size(); i++){

                Pattern p2 = Pattern.compile("^130019.|^130005.");
                Matcher m2 = p2.matcher(AllHdList.get(i));


                if (!m2.find()) {
                    tempList.add(AllHdList.get(i));
                }

        } 
    System.out.println( tempList );        
 }

共 (2) 个答案

  1. # 1 楼答案

    你可以这样做

    String[] value = {"1300199", "1300058", "1300013","1300014"};
    ArrayList<String> list = new ArrayList<String>();
    for(String i: value)
    {
    Matcher m = Pattern.compile("^(?:130019|130005).").matcher(i);
    if(!m.find())
    {
        list.add(i);
    }
    }
    System.out.println(list);
    

    输出:

    [1300013, 1300014]
    
  2. # 2 楼答案

    代码太多了。改用String#matches()

    List <String> tempList  = new ArrayList<String>();
    for (String s : AllHdList)
        if (!s.matches("130019.|130005."))
            tempList.add(s);
    

    或者使用java 8在一行中使用整个方法:

    public static void get7HdQh(List<String> list) {
        list.stream().filter(s -> !s.matches("130019.|130005.")).foreach(System.out::println);
    }
    

    或者如果你真的想收集它们:

    List<String> tempList = list.stream().filter(s -> !s.matches("130019.|130005.")).collect(Collectors.toList());
    

    请注意(对于所有java版本),在正则表达式中不需要锚定"^",这是隐含的,因为matches()必须匹配整个字符串才能为真